Search in sources :

Example 6 with CommentInstance

use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.

the class CommentNotificationEventListener method collectOrgEntitiesByRole.

protected Set<OrganizationalEntity> collectOrgEntitiesByRole(List<String> mentionedRoles, CaseCommentEvent event, StringBuilder commentContent) {
    Set<OrganizationalEntity> recipients = new HashSet<>();
    CommentInstance comment = event.getComment();
    for (String roleName : mentionedRoles) {
        if (comment.getRestrictedTo() != null && !comment.getRestrictedTo().isEmpty() && !comment.getRestrictedTo().contains(roleName)) {
            // mentioned role is not allowed to see this comment so remove it from the list
            continue;
        }
        try {
            Collection<OrganizationalEntity> assignments = ((CaseAssignment) event.getCaseFile()).getAssignments(roleName);
            recipients.addAll(assignments);
            String assignmnetsFlatten = assignments.stream().map(oe -> oe.getId()).collect(Collectors.joining(","));
            String updatedCommentContent = commentContent.toString().replaceAll("@" + roleName, assignmnetsFlatten);
            commentContent.setLength(0);
            commentContent.append(updatedCommentContent);
        } catch (IllegalArgumentException e) {
            logger.debug("Role {} does not exist in case {}", roleName, event.getCaseId());
        }
    }
    return recipients;
}
Also used : Cacheable(org.kie.internal.runtime.Cacheable) Logger(org.slf4j.Logger) Collection(java.util.Collection) CaseEventListener(org.jbpm.casemgmt.api.event.CaseEventListener) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) ServiceLoader(java.util.ServiceLoader) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) Collectors(java.util.stream.Collectors) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Matcher(java.util.regex.Matcher) Map(java.util.Map) NotificationPublisher(org.kie.internal.utils.NotificationPublisher) CaseCommentEvent(org.jbpm.casemgmt.api.event.CaseCommentEvent) Pattern(java.util.regex.Pattern) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) CaseAssignment(org.kie.api.runtime.process.CaseAssignment) HashSet(java.util.HashSet)

Example 7 with CommentInstance

use of org.jbpm.casemgmt.api.model.instance.CommentInstance 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 8 with CommentInstance

use of org.jbpm.casemgmt.api.model.instance.CommentInstance 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 9 with CommentInstance

use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.

the class CaseServiceImplTest method testCaseWithComments.

@Test
public void testCaseWithComments() {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    Map<String, Object> data = new HashMap<>();
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data, roleAssignments);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        Collection<CommentInstance> caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(0, caseComments.size());
        String commentId = caseService.addCaseComment(FIRST_CASE_ID, "poul", "just a tiny comment");
        assertNotNull(commentId);
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(1, caseComments.size());
        CommentInstance comment = caseComments.iterator().next();
        assertComment(comment, "poul", "just a tiny comment");
        assertEquals(commentId, comment.getId());
        caseService.updateCaseComment(FIRST_CASE_ID, comment.getId(), comment.getAuthor(), "Updated " + comment.getComment());
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(1, caseComments.size());
        comment = caseComments.iterator().next();
        assertComment(comment, "poul", "Updated just a tiny comment");
        caseService.addCaseComment(FIRST_CASE_ID, "mary", "another comment");
        caseService.addCaseComment(FIRST_CASE_ID, "john", "third comment");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(3, caseComments.size());
        Iterator<CommentInstance> it = caseComments.iterator();
        assertComment(it.next(), "poul", "Updated just a tiny comment");
        assertComment(it.next(), "mary", "another comment");
        assertComment(it.next(), "john", "third comment");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(3, caseComments.size());
        it = caseComments.iterator();
        assertComment(it.next(), "john", "third comment");
        assertComment(it.next(), "mary", "another comment");
        assertComment(it.next(), "poul", "Updated just a tiny comment");
        caseService.removeCaseComment(FIRST_CASE_ID, comment.getId());
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertEquals(2, caseComments.size());
        it = caseComments.iterator();
        assertComment(it.next(), "john", "third comment");
        assertComment(it.next(), "mary", "another comment");
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Example 10 with CommentInstance

use of org.jbpm.casemgmt.api.model.instance.CommentInstance in project jbpm by kiegroup.

the class CaseServiceImplTest method testCaseWithCommentsWithRestrictions.

@Test
public void testCaseWithCommentsWithRestrictions() {
    Map<String, OrganizationalEntity> roleAssignments = new HashMap<>();
    roleAssignments.put("owner", new UserImpl("john"));
    roleAssignments.put("participant", new UserImpl("mary"));
    Map<String, Object> data = new HashMap<>();
    CaseFileInstance caseFile = caseService.newCaseFileInstance(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, data, roleAssignments);
    String caseId = caseService.startCase(deploymentUnit.getIdentifier(), USER_TASK_STAGE_AUTO_START_CASE_P_ID, caseFile);
    assertNotNull(caseId);
    assertEquals(FIRST_CASE_ID, caseId);
    try {
        CaseInstance cInstance = caseService.getCaseInstance(caseId);
        assertNotNull(cInstance);
        assertEquals(FIRST_CASE_ID, cInstance.getCaseId());
        assertEquals(deploymentUnit.getIdentifier(), cInstance.getDeploymentId());
        Collection<CommentInstance> caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(0, caseComments.size());
        caseService.addCaseComment(FIRST_CASE_ID, "poul", "just a tiny comment", "owner");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(1, caseComments.size());
        CommentInstance comment = caseComments.iterator().next();
        assertComment(comment, "poul", "just a tiny comment");
        // mary is not the owner so should not see the comment that is only for role owner role
        identityProvider.setName("mary");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(0, caseComments.size());
        try {
            caseService.updateCaseComment(FIRST_CASE_ID, comment.getId(), comment.getAuthor(), "Updated " + comment.getComment(), "participant", "owner");
            fail("mary should not be able to update comment that she has no access to");
        } catch (SecurityException e) {
            // mary is not allowed to update comments that she has no access to
            assertTrue(e.getMessage().contains("User mary does not have access to comment"));
        }
        identityProvider.setName("john");
        caseService.updateCaseComment(FIRST_CASE_ID, comment.getId(), comment.getAuthor(), "Updated " + comment.getComment(), "participant", "owner");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(1, caseComments.size());
        comment = caseComments.iterator().next();
        assertComment(comment, "poul", "Updated just a tiny comment");
        // now mary as participant should see the updated comment
        identityProvider.setName("mary");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(1, caseComments.size());
        identityProvider.setName("john");
        // no restrictions
        caseService.addCaseComment(FIRST_CASE_ID, "mary", "another comment");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(2, caseComments.size());
        Iterator<CommentInstance> it = caseComments.iterator();
        assertComment(it.next(), "poul", "Updated just a tiny comment");
        assertComment(it.next(), "mary", "another comment");
        // second comment has no restrictions so should be seen by anyone
        identityProvider.setName("mary");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(2, caseComments.size());
        identityProvider.setName("john");
        caseService.addCaseComment(FIRST_CASE_ID, "john", "private comment", "owner");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertNotNull(caseComments);
        assertEquals(3, caseComments.size());
        comment = caseComments.iterator().next();
        assertComment(comment, "john", "private comment");
        identityProvider.setName("mary");
        try {
            caseService.removeCaseComment(FIRST_CASE_ID, comment.getId());
            fail("mary should not be able to remove comment that she has no access to");
        } catch (SecurityException e) {
            // mary is not allowed to removed comments that she has no access to
            assertTrue(e.getMessage().contains("User mary does not have access to comment"));
        }
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertEquals(2, caseComments.size());
        identityProvider.setName("john");
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertEquals(3, caseComments.size());
        caseService.removeCaseComment(FIRST_CASE_ID, comment.getId());
        caseComments = caseService.getCaseComments(FIRST_CASE_ID, CommentSortBy.Author, new QueryContext());
        assertEquals(2, caseComments.size());
    } catch (Exception e) {
        logger.error("Unexpected error {}", e.getMessage(), e);
        fail("Unexpected exception " + e.getMessage());
    } finally {
        if (caseId != null) {
            caseService.cancelCase(caseId);
        }
    }
}
Also used : OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) HashMap(java.util.HashMap) QueryContext(org.kie.api.runtime.query.QueryContext) CaseCommentNotFoundException(org.jbpm.casemgmt.api.CaseCommentNotFoundException) AdHocFragmentNotFoundException(org.jbpm.casemgmt.api.AdHocFragmentNotFoundException) CaseNotFoundException(org.jbpm.casemgmt.api.CaseNotFoundException) TaskNotFoundException(org.jbpm.services.api.TaskNotFoundException) CaseActiveException(org.jbpm.casemgmt.api.CaseActiveException) CaseFileInstance(org.jbpm.casemgmt.api.model.instance.CaseFileInstance) CaseInstance(org.jbpm.casemgmt.api.model.instance.CaseInstance) CommentInstance(org.jbpm.casemgmt.api.model.instance.CommentInstance) UserImpl(org.jbpm.services.task.impl.model.UserImpl) AbstractCaseServicesBaseTest(org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest) Test(org.junit.Test)

Aggregations

CommentInstance (org.jbpm.casemgmt.api.model.instance.CommentInstance)12 HashMap (java.util.HashMap)9 CaseFileInstance (org.jbpm.casemgmt.api.model.instance.CaseFileInstance)9 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)7 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CaseInstance (org.jbpm.casemgmt.api.model.instance.CaseInstance)5 AbstractCaseServicesBaseTest (org.jbpm.casemgmt.impl.util.AbstractCaseServicesBaseTest)5 UserImpl (org.jbpm.services.task.impl.model.UserImpl)5 Test (org.junit.Test)5 QueryContext (org.kie.api.runtime.query.QueryContext)5 Collection (java.util.Collection)4 Map (java.util.Map)4 CaseCommentNotFoundException (org.jbpm.casemgmt.api.CaseCommentNotFoundException)4 CaseFileInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CaseFileInstanceImpl)4 MessageFormat (java.text.MessageFormat)3 AdHocFragmentNotFoundException (org.jbpm.casemgmt.api.AdHocFragmentNotFoundException)3 AuthorizationManager (org.jbpm.casemgmt.api.auth.AuthorizationManager)3 CommentInstanceImpl (org.jbpm.casemgmt.impl.model.instance.CommentInstanceImpl)3 IdentityProvider (org.kie.internal.identity.IdentityProvider)3