Search in sources :

Example 1 with CommentInstanceImpl

use of org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl in project jbpm by kiegroup.

the class CaseCommentCommand method execute.

@Override
public String execute(Context context) {
    KieSession ksession = ((RegistryContext) context).lookup(KieSession.class);
    Collection<? extends Object> caseFiles = ksession.getObjects(new ClassObjectFilter(CaseFileInstance.class));
    if (caseFiles.size() != 1) {
        throw new IllegalStateException("Not able to find distinct case file - found case files " + caseFiles.size());
    }
    CaseFileInstance caseFile = (CaseFileInstance) caseFiles.iterator().next();
    FactHandle factHandle = ksession.getFactHandle(caseFile);
    CaseEventSupport caseEventSupport = getCaseEventSupport(context);
    String commentIdentifier = null;
    if (add) {
        CommentInstance commentInstance = new CommentInstanceImpl(author, comment, restrictedTo);
        caseEventSupport.fireBeforeCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
        ((CaseFileInstanceImpl) caseFile).addComment(commentInstance);
        commentIdentifier = commentInstance.getId();
        caseEventSupport.fireAfterCaseCommentAdded(caseFile.getCaseId(), caseFile, commentInstance);
    } else if (update) {
        CommentInstance toUpdate = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
        if (!this.author.equals(toUpdate.getAuthor())) {
            throw new IllegalStateException("Only original author can update comment");
        }
        // apply authorization
        authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toUpdate);
        caseEventSupport.fireBeforeCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
        ((CommentInstanceImpl) toUpdate).setComment(updatedText);
        if (restrictedTo != null) {
            ((CommentInstanceImpl) toUpdate).setRestrictedTo(restrictedTo);
        }
        commentIdentifier = toUpdate.getId();
        caseEventSupport.fireAfterCaseCommentUpdated(caseFile.getCaseId(), caseFile, toUpdate);
    } else if (remove) {
        CommentInstance toRemove = ((CaseFileInstanceImpl) caseFile).getComments().stream().filter(c -> c.getId().equals(commentId)).findFirst().orElseThrow(() -> new CaseCommentNotFoundException("Cannot find comment with id " + commentId));
        // apply authorization
        authorizationManager.checkCommentAuthorization(caseFile.getCaseId(), caseFile, toRemove);
        caseEventSupport.fireBeforeCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
        ((CaseFileInstanceImpl) caseFile).removeComment(toRemove);
        commentIdentifier = toRemove.getId();
        caseEventSupport.fireAfterCaseCommentRemoved(caseFile.getCaseId(), caseFile, toRemove);
    }
    ksession.update(factHandle, caseFile);
    triggerRules(ksession);
    return commentIdentifier;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) Collection(java.util.Collection) ClassObjectFilter(org.drools.core.ClassObjectFilter) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) List(java.util.List) Context(org.kie.api.runtime.Context) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) KieSession(org.kie.api.runtime.KieSession) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CaseEventSupport(org.jbpm.casemgmt.impl.event.CaseEventSupport) FactHandle(org.kie.api.runtime.rule.FactHandle) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) RegistryContext(org.drools.core.command.impl.RegistryContext) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) ClassObjectFilter(org.drools.core.ClassObjectFilter) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) KieSession(org.kie.api.runtime.KieSession)

Example 2 with CommentInstanceImpl

use of org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl in project jbpm by kiegroup.

the class AuthorizationManagerImpl method checkCommentAuthorization.

@Override
public void checkCommentAuthorization(String caseId, CaseFileInstance caseFileInstance, CommentInstance commentInstance) {
    CommentInstanceImpl comment = ((CommentInstanceImpl) commentInstance);
    if (comment.getRestrictedTo() == null || comment.getRestrictedTo().isEmpty()) {
        return;
    }
    List<String> callerAuthorization = collectUserAuthInfo();
    logger.debug("Caller {} authorization set is {}", identityProvider.getName(), callerAuthorization);
    List<String> callerCaseRoles = getCallerRoles(caseFileInstance, callerAuthorization);
    logger.debug("Caller {} case role set is {}", identityProvider.getName(), callerCaseRoles);
    List<String> requiredRoles = comment.getRestrictedTo();
    if (requiredRoles.isEmpty() || requiredRoles.stream().anyMatch(role -> callerCaseRoles.contains(role))) {
        logger.debug("Caller has access to comment {}", comment.getId());
        return;
    }
    logger.warn("User {} does not have access to comment {} in case {}, required roles are {} and user has {}", identityProvider.getName(), comment.getId(), caseId, requiredRoles, callerCaseRoles);
    throw new SecurityException(MessageFormat.format(NO_AUTH_TO_COMMENT, identityProvider.getName(), comment.getId(), caseId));
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) Arrays(java.util.Arrays) Properties(java.util.Properties) Logger(org.slf4j.Logger) Collection(java.util.Collection) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) HashMap(java.util.HashMap) QueryNameCommand(org.jbpm.shared.services.impl.commands.QueryNameCommand) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) Entry(java.util.Map.Entry) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) InputStream(java.io.InputStream) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)

Example 3 with CommentInstanceImpl

use of org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl in project jbpm by kiegroup.

the class AuthorizationManagerImpl method filterByCommentAuthorization.

@Override
public List<CommentInstance> filterByCommentAuthorization(String caseId, CaseFileInstance caseFileInstance, List<CommentInstance> comments) {
    if (comments == null || comments.isEmpty()) {
        logger.debug("No comments to be filtered");
        return comments;
    }
    List<String> callerAuthorization = collectUserAuthInfo();
    logger.debug("Caller {} authorization set is {}", identityProvider.getName(), callerAuthorization);
    List<String> callerCaseRoles = getCallerRoles(caseFileInstance, callerAuthorization);
    logger.debug("Caller {} case role set is {}", identityProvider.getName(), callerCaseRoles);
    List<CommentInstance> filteredComments = new ArrayList<>(comments);
    for (CommentInstance commentInstance : comments) {
        CommentInstanceImpl comment = ((CommentInstanceImpl) commentInstance);
        List<String> requiredRoles = comment.getRestrictedTo();
        if (requiredRoles == null || requiredRoles.isEmpty()) {
            continue;
        }
        if (requiredRoles.isEmpty() || requiredRoles.stream().anyMatch(role -> callerCaseRoles.contains(role))) {
            logger.debug("Caller {} has access to comment {}", identityProvider.getName(), comment.getId());
            continue;
        }
        logger.debug("Caller {} does not have access to comment {}", identityProvider.getName(), comment.getId());
        filteredComments.remove(comment);
    }
    return filteredComments;
}
Also used : IdentityProvider(org.kie.internal.identity.IdentityProvider) Arrays(java.util.Arrays) Properties(java.util.Properties) Logger(org.slf4j.Logger) Collection(java.util.Collection) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) HashMap(java.util.HashMap) QueryNameCommand(org.jbpm.shared.services.impl.commands.QueryNameCommand) TransactionalCommandService(org.jbpm.shared.services.impl.TransactionalCommandService) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) List(java.util.List) Stream(java.util.stream.Stream) Map(java.util.Map) Entry(java.util.Map.Entry) CaseFileInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) InputStream(java.io.InputStream) AuthorizationManager(org.jbpm.casemgmt.api.auth.AuthorizationManager) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) ArrayList(java.util.ArrayList)

Example 4 with CommentInstanceImpl

use of org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl in project jbpm by kiegroup.

the class CommentNotificationEventListenerTest method testCollectOrgEntitiesByRole.

@Test
public void testCollectOrgEntitiesByRole() {
    CommentNotificationEventListener listener = new CommentNotificationEventListener();
    List<String> mentionedRoles = new ArrayList<>();
    mentionedRoles.add("owner");
    mentionedRoles.add("manager");
    CommentInstanceImpl comment = new CommentInstanceImpl("john", "simple comment for @owner and @manager", new ArrayList<>());
    CaseFileInstance caseFile = buildCaseFile(mentionedRoles);
    CaseCommentEvent event = new CaseCommentEvent("john", caseFile.getCaseId(), caseFile, comment);
    StringBuilder commentContent = new StringBuilder(comment.getComment());
    Set<OrganizationalEntity> collected = listener.collectOrgEntitiesByRole(mentionedRoles, event, commentContent);
    assertThat(collected).hasSize(2);
    assertThat(collected).allMatch(item -> item instanceof User);
    assertThat(commentContent.toString()).isEqualTo("simple comment for john and mary");
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) CaseCommentEvent(org.jbpm.casemgmt.api.event.CaseCommentEvent) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with CommentInstanceImpl

use of org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl in project jbpm by kiegroup.

the class CommentNotificationEventListenerTest method testNotificationOnCommentAddedWithRawBody.

@Test
public void testNotificationOnCommentAddedWithRawBody() {
    CommentNotificationEventListener listener = new CommentNotificationEventListener();
    List<String> mentionedRoles = new ArrayList<>();
    mentionedRoles.add("owner");
    mentionedRoles.add("manager");
    CaseFileInstance caseFile = buildCaseFile(mentionedRoles);
    CommentInstanceImpl comment = new CommentInstanceImpl("john", "simple comment for @owner and @manager", new ArrayList<>());
    CaseCommentEvent event = new CaseCommentEvent("john", caseFile.getCaseId(), caseFile, comment);
    TestNotificationPublisher publisher = new TestNotificationPublisher(true);
    listener.addPublisher(publisher);
    listener.afterCaseCommentAdded(event);
    String expectedNotification = "Publishing notification from cases@jbpm.org, with subject You have been mentioned in case (CASE-00001) comment to [[UserImpl:'mary'], [UserImpl:'john']] with body simple comment for john and mary";
    List<String> published = publisher.get();
    assertThat(published).hasSize(1);
    assertThat(published.get(0)).isEqualTo(expectedNotification);
}
Also used : CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CommentInstanceImpl(org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl) CaseCommentEvent(org.jbpm.casemgmt.api.event.CaseCommentEvent) TestNotificationPublisher(org.jbpm.casemgmt.impl.util.TestNotificationPublisher) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Aggregations

CommentInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)8 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)7 ArrayList (java.util.ArrayList)6 CaseCommentEvent (org.jbpm.casemgmt.api.event.CaseCommentEvent)5 Test (org.junit.Test)5 Collection (java.util.Collection)3 List (java.util.List)3 AuthorizationManager (org.jbpm.casemgmt.api.auth.AuthorizationManager)3 CommentInstance (org.jbpm.casemgmt.api.model.instance.CommentInstance)3 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)3 IdentityProvider (org.kie.internal.identity.IdentityProvider)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 MessageFormat (java.text.MessageFormat)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Properties (java.util.Properties)2 Stream (java.util.stream.Stream)2