diff --git a/src/main/java/run/halo/app/model/entity/BaseComment.java b/src/main/java/run/halo/app/model/entity/BaseComment.java index 4f876a94d..df25c6f7c 100644 --- a/src/main/java/run/halo/app/model/entity/BaseComment.java +++ b/src/main/java/run/halo/app/model/entity/BaseComment.java @@ -132,7 +132,6 @@ public class BaseComment extends BaseEntity { if (isAdmin == null) { isAdmin = false; } - } } diff --git a/src/main/java/run/halo/app/model/entity/Journal.java b/src/main/java/run/halo/app/model/entity/Journal.java index dbc27de4e..9f8ad7942 100644 --- a/src/main/java/run/halo/app/model/entity/Journal.java +++ b/src/main/java/run/halo/app/model/entity/Journal.java @@ -13,4 +13,13 @@ import javax.persistence.Entity; @DiscriminatorValue("2") public class Journal extends BaseComment { + + @Override + public void prePersist() { + super.prePersist(); + + if (getPostId() == null) { + setPostId(0); + } + } } diff --git a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java index e2f40c5d2..9109e56fc 100644 --- a/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/BaseCommentServiceImpl.java @@ -207,41 +207,40 @@ public abstract class BaseCommentServiceImpl extend } @Override - public COMMENT createBy(COMMENT COMMENT) { - Assert.notNull(COMMENT, "Domain must not be null"); + public COMMENT createBy(COMMENT comment) { + Assert.notNull(comment, "Domain must not be null"); // Check post id - boolean isPostExist = postRepository.existsById(COMMENT.getPostId()); - if (!isPostExist) { - throw new NotFoundException("The post with id " + COMMENT.getPostId() + " was not found"); + if (!ServiceUtils.isEmptyId(comment.getPostId())) { + postMustExist(comment.getPostId()); } // Check parent id - if (!ServiceUtils.isEmptyId(COMMENT.getParentId())) { - mustExistById(COMMENT.getParentId()); + if (!ServiceUtils.isEmptyId(comment.getParentId())) { + mustExistById(comment.getParentId()); } // Check user login status and set this field Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); // Set some default values - COMMENT.setIpAddress(ServletUtils.getRequestIp()); - COMMENT.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT)); - COMMENT.setGavatarMd5(DigestUtils.md5Hex(COMMENT.getEmail())); + comment.setIpAddress(ServletUtils.getRequestIp()); + comment.setUserAgent(ServletUtils.getHeaderIgnoreCase(HttpHeaders.USER_AGENT)); + comment.setGavatarMd5(DigestUtils.md5Hex(comment.getEmail())); if (authentication != null) { // Comment of blogger - COMMENT.setIsAdmin(true); - COMMENT.setStatus(CommentStatus.PUBLISHED); + comment.setIsAdmin(true); + comment.setStatus(CommentStatus.PUBLISHED); } else { // Comment of guest // Handle comment status Boolean needAudit = optionService.getByPropertyOrDefault(CommentProperties.NEW_NEED_CHECK, Boolean.class, true); - COMMENT.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED); + comment.setStatus(needAudit ? CommentStatus.AUDITING : CommentStatus.PUBLISHED); } // Create comment - COMMENT createdComment = create(COMMENT); + COMMENT createdComment = create(comment); if (ServiceUtils.isEmptyId(createdComment.getParentId())) { if (authentication == null) { @@ -302,6 +301,18 @@ public abstract class BaseCommentServiceImpl extend return new BaseCommentDTO().convertFrom(comment); } + /** + * Post must exist. + * + * @param postId post id must not be null + */ + protected void postMustExist(@NonNull Integer postId) { + boolean isPostExist = postRepository.existsById(postId); + if (!isPostExist) { + throw new NotFoundException("The post with id " + postId + " was not found"); + } + } + @NonNull protected Specification buildSpecByQuery(@NonNull CommentQuery commentQuery) { Assert.notNull(commentQuery, "Comment query must not be null"); diff --git a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java index f4b564dc0..977415922 100644 --- a/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java +++ b/src/main/java/run/halo/app/service/impl/JournalServiceImpl.java @@ -6,6 +6,7 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import run.halo.app.exception.ForbiddenException; import run.halo.app.model.entity.Journal; import run.halo.app.model.entity.User; import run.halo.app.model.params.JournalParam; @@ -16,6 +17,7 @@ import run.halo.app.security.authentication.Authentication; import run.halo.app.security.context.SecurityContextHolder; import run.halo.app.service.JournalService; import run.halo.app.service.OptionService; +import run.halo.app.utils.ServiceUtils; import run.halo.app.utils.ValidationUtils; /** @@ -51,6 +53,11 @@ public class JournalServiceImpl extends BaseCommentServiceImpl implemen journalParam.setAuthor(StringUtils.isBlank(user.getNickname()) ? user.getUsername() : user.getNickname()); journalParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL)); journalParam.setEmail(user.getEmail()); + } else { + // Guest comment + if (ServiceUtils.isEmptyId(journalParam.getParentId())) { + throw new ForbiddenException("You have no right to create a journal"); + } } // Validate the journal param