@Override public Comment getComment(CoreSession session, String commentId) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(commentId); if (!session.exists(commentRef)) { throw new CommentNotFoundException("The document " + commentId + " does not exist."); } DocumentModel commentModel = session.getDocument(commentRef); return Comments.newComment(commentModel); }
@Override public Comment getExternalComment(CoreSession session, String entityId) throws CommentNotFoundException { DocumentModel commentModel = getExternalCommentModel(session, entityId); if (commentModel == null) { throw new CommentNotFoundException("The external comment " + entityId + " does not exist."); } String parentId = (String) commentModel.getPropertyValue(COMMENT_PARENT_ID); if (!session.hasPermission(getAncestorRef(session, commentModel), SecurityConstants.READ)) { throw new CommentSecurityException("The user " + session.getPrincipal().getName() + " does not have access to the comments of document " + parentId); } return Framework.doPrivileged(() -> Comments.newComment(commentModel)); }
@Override public Comment createComment(CoreSession session, Comment comment) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(comment.getParentId()); if (!session.exists(commentRef)) { throw new CommentNotFoundException("The document " + comment.getParentId() + " does not exist."); } DocumentModel docToComment = session.getDocument(commentRef); DocumentModel commentModel = session.createDocumentModel(COMMENT_DOC_TYPE); commentModel.setPropertyValue("dc:created", Calendar.getInstance()); Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { commentModel.addFacet(EXTERNAL_ENTITY_FACET); Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } DocumentModel createdCommentModel = createComment(docToComment, commentModel); return Comments.newComment(createdCommentModel); }
@Override public Comment updateExternalComment(CoreSession session, String entityId, Comment comment) throws CommentNotFoundException { DocumentModel commentModel = getExternalCommentModel(session, entityId); if (commentModel == null) { throw new CommentNotFoundException("The external comment " + entityId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !comment.getAuthor().equals(principal.getName())) { throw new CommentSecurityException( "The user " + principal.getName() + " can not edit comments of document " + comment.getParentId()); } return CoreInstance.doPrivileged(session, s -> { Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } s.saveDocument(commentModel); return Comments.newComment(commentModel); }); }
@Override public Comment updateComment(CoreSession session, String commentId, Comment comment) throws CommentNotFoundException { IdRef commentRef = new IdRef(commentId); if (!CoreInstance.doPrivileged(session, s -> {return s.exists(commentRef);})) { throw new CommentNotFoundException("The comment " + commentId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); if (!principal.isAdministrator() && !comment.getAuthor().equals(principal.getName())) { throw new CommentSecurityException( "The user " + principal.getName() + " can not edit comments of document " + comment.getParentId()); } return CoreInstance.doPrivileged(session, s -> { // Initiate Modification Date if it is not done yet if (comment.getModificationDate() == null) { comment.setModificationDate(Instant.now()); } DocumentModel commentModel = s.getDocument(commentRef); Comments.commentToDocumentModel(comment, commentModel); if (comment instanceof ExternalEntity) { Comments.externalEntityToDocumentModel((ExternalEntity) comment, commentModel); } s.saveDocument(commentModel); return Comments.newComment(commentModel); }); }
@Override public Comment getComment(CoreSession session, String commentId) throws CommentNotFoundException, CommentSecurityException { DocumentRef commentRef = new IdRef(commentId); // Parent document can be a comment, check existence as a privileged user if (!CoreInstance.doPrivileged(session, s -> {return s.exists(commentRef);})) { throw new CommentNotFoundException("The comment " + commentId + " does not exist."); } NuxeoPrincipal principal = session.getPrincipal(); return CoreInstance.doPrivileged(session, s -> { DocumentModel commentModel = s.getDocument(commentRef); DocumentRef documentRef = getAncestorRef(s, commentModel); if (!s.hasPermission(principal, documentRef, SecurityConstants.READ)) { throw new CommentSecurityException("The user " + principal.getName() + " does not have access to the comments of document " + documentRef.reference()); } return Comments.newComment(commentModel); }); }
commentModel = s.createDocument(commentModel); notifyEvent(s, CommentEvents.COMMENT_ADDED, s.getDocument(docRef), commentModel); return Comments.newComment(commentModel); });