Fix comment creation bug when admin is logging in

pull/137/head
johnniang 2019-03-31 00:01:30 +08:00
parent fd9fe59144
commit 6f9aac5aad
2 changed files with 16 additions and 13 deletions

View File

@ -111,11 +111,22 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent()))); comment.setContent(OwoUtil.parseOwo(formatContent(comment.getContent())));
comment.setIpAddress(ServletUtil.getClientIP(request)); comment.setIpAddress(ServletUtil.getClientIP(request));
comment.setUserAgent(ServletUtil.getHeaderIgnoreCase(request, HttpHeaders.USER_AGENT)); comment.setUserAgent(ServletUtil.getHeaderIgnoreCase(request, HttpHeaders.USER_AGENT));
// TODO Check user login status and set this field
// Check user login status and set this field
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) { if (authentication != null) {
// If the user is login // If the user is login
comment.setIsAdmin(true); comment.setIsAdmin(true);
comment.setStatus(CommentStatus.PUBLISHED);
} else {
// Handle comment status
Boolean needAudit = optionService.getByPropertyOrDefault(BlogProperties.NEW_COMMENT_NEED_CHECK, Boolean.class, true);
if (needAudit) {
comment.setStatus(CommentStatus.AUDITING);
} else {
comment.setStatus(CommentStatus.PUBLISHED);
}
} }
comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor())); comment.setAuthor(HtmlUtils.htmlEscape(comment.getAuthor()));
@ -126,14 +137,6 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl())); comment.setAuthorUrl(URLUtil.normalize(comment.getAuthorUrl()));
} }
// Handle comment status
Boolean needAudit = optionService.getByPropertyOrDefault(BlogProperties.NEW_COMMENT_NEED_CHECK, Boolean.class, true);
if (needAudit) {
comment.setStatus(CommentStatus.AUDITING);
} else {
comment.setStatus(CommentStatus.PUBLISHED);
}
Comment createdComment = create(comment); Comment createdComment = create(comment);
// TODO Handle email sending // TODO Handle email sending

View File

@ -14,13 +14,13 @@ import cc.ryanc.halo.service.OptionService;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.ValidationUtils; import cc.ryanc.halo.utils.ValidationUtils;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault; import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List; import java.util.List;
import static org.springframework.data.domain.Sort.Direction.DESC; import static org.springframework.data.domain.Sort.Direction.DESC;
@ -62,15 +62,15 @@ public class CommentController {
} }
@PostMapping @PostMapping
public CommentOutputDTO createBy(@Valid @RequestBody CommentParam commentParam, HttpServletRequest request) { public CommentOutputDTO createBy(@RequestBody CommentParam commentParam, HttpServletRequest request) {
// Get authentication // Get authentication
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null) { if (authentication != null) {
User user = authentication.getDetail().getUser(); User user = authentication.getDetail().getUser();
// If the admin is login // If the admin is login
commentParam.setAuthor(user.getNickname()); commentParam.setAuthor(StringUtils.isEmpty(user.getNickname()) ? user.getUsername() : user.getNickname());
commentParam.setEmail(user.getEmail()); commentParam.setEmail(user.getEmail());
commentParam.setAuthor(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL)); commentParam.setAuthorUrl(optionService.getByPropertyOfNullable(BlogProperties.BLOG_URL));
} }
// Validate the comment param manually // Validate the comment param manually