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;
}
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));
}
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;
}
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");
}
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);
}
Aggregations