From 240511618526c5c5aaffebde9120173e508be7c7 Mon Sep 17 00:00:00 2001 From: "Vyacheslav N. Boyko" Date: Tue, 12 Dec 2017 01:35:01 +0300 Subject: [PATCH] implemented ability of deleting comments by admin --- .../java/ru/bvn13/voidforum/WebConfig.java | 2 +- .../voidforum/controllers/PostController.java | 2 + .../voidforum/controllers/TestController.java | 22 -------- .../account/StoredFileController.java | 10 ++-- .../controllers/admin/CommentController.java | 52 +++++++++++++++++++ .../controllers/admin/PostController.java | 4 ++ .../voidforum/forms/CommentDeletionForm.java | 16 ++++++ .../java/ru/bvn13/voidforum/models/Post.java | 3 ++ .../voidforum/services/CommentService.java | 6 +++ .../voidforum/support/web/MessageHelper.java | 40 ++++++++++++++ src/main/resources/logback.xml | 13 +++-- src/main/resources/resources/css/mystyle.css | 15 ++++++ .../templates/account/posts/index.jade | 2 +- .../resources/templates/comments/list.jade | 10 +++- .../resources/templates/comments/one.jade | 27 +++++++--- src/main/resources/templates/tests/1.html | 12 ----- src/main/resources/templates/tests/2.jade | 2 - src/main/resources/templates/tests/3.html | 12 ----- 18 files changed, 183 insertions(+), 67 deletions(-) delete mode 100644 src/main/java/ru/bvn13/voidforum/controllers/TestController.java create mode 100644 src/main/java/ru/bvn13/voidforum/controllers/admin/CommentController.java create mode 100644 src/main/java/ru/bvn13/voidforum/forms/CommentDeletionForm.java delete mode 100644 src/main/resources/templates/tests/1.html delete mode 100644 src/main/resources/templates/tests/2.jade delete mode 100644 src/main/resources/templates/tests/3.html diff --git a/src/main/java/ru/bvn13/voidforum/WebConfig.java b/src/main/java/ru/bvn13/voidforum/WebConfig.java index be4df18..7169ad2 100644 --- a/src/main/java/ru/bvn13/voidforum/WebConfig.java +++ b/src/main/java/ru/bvn13/voidforum/WebConfig.java @@ -53,7 +53,7 @@ public class WebConfig extends WebMvcConfigurerAdapter { @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView view) { CsrfToken token = (CsrfToken) request.getAttribute(CsrfToken.class.getName()); - if (token != null) { + if (token != null && view != null) { view.addObject(token.getParameterName(), token); } } diff --git a/src/main/java/ru/bvn13/voidforum/controllers/PostController.java b/src/main/java/ru/bvn13/voidforum/controllers/PostController.java index 8f99725..64f9959 100644 --- a/src/main/java/ru/bvn13/voidforum/controllers/PostController.java +++ b/src/main/java/ru/bvn13/voidforum/controllers/PostController.java @@ -67,6 +67,7 @@ public class PostController { @RequestMapping(value = "{permalink}", method = GET) public String show(@PathVariable String permalink, Model model, @RequestParam(defaultValue = "0") int page, HttpServletRequest request){ Post post = this.postService.findPostByPermalink(permalink); + User user = userService.currentUser(); logger.debug(String.format("ACCESS %s from IP: %s", permalink, this.requestProcessorService.getRealIp(request))); @@ -107,6 +108,7 @@ public class PostController { model.addAttribute("comments", comments); model.addAttribute("commentForm", commentForm); model.addAttribute("commentFormats", commentService.getAvailableCommentFormats()); + model.addAttribute("disableCommenting", userService.hasPrivilege(user, PrivilegeService.PRIVILEGE_OWNER) || post.getUser().getId().equals(user.getId()) ? false : post.getDisableCommenting()); return "posts/show"; } diff --git a/src/main/java/ru/bvn13/voidforum/controllers/TestController.java b/src/main/java/ru/bvn13/voidforum/controllers/TestController.java deleted file mode 100644 index 4748ce7..0000000 --- a/src/main/java/ru/bvn13/voidforum/controllers/TestController.java +++ /dev/null @@ -1,22 +0,0 @@ -package ru.bvn13.voidforum.controllers; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; - -/** - * Created by bvn13 on 09.12.2017. - */ -@Controller -@RequestMapping("tests") -public class TestController { - - @GetMapping(value = "/1") - public String test1(Model model) { - - - return "tests/1"; - } - -} diff --git a/src/main/java/ru/bvn13/voidforum/controllers/account/StoredFileController.java b/src/main/java/ru/bvn13/voidforum/controllers/account/StoredFileController.java index db7f2da..3886790 100644 --- a/src/main/java/ru/bvn13/voidforum/controllers/account/StoredFileController.java +++ b/src/main/java/ru/bvn13/voidforum/controllers/account/StoredFileController.java @@ -17,6 +17,7 @@ import ru.bvn13.voidforum.models.StoredFile; import ru.bvn13.voidforum.repositories.StoredFileRepository; import ru.bvn13.voidforum.services.FileStorageService; import ru.bvn13.voidforum.services.UserService; +import ru.bvn13.voidforum.support.web.MessageHelper; import ru.bvn13.voidforum.utils.DTOUtil; import javax.validation.Valid; @@ -53,9 +54,10 @@ public class StoredFileController { } @PostMapping("/upload") //new annotation since 4.3 - public String upload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { + public String upload(@RequestParam("file") MultipartFile file, RedirectAttributes ra) { if (file.isEmpty()) { - redirectAttributes.addFlashAttribute("uploadStatus", "Please select a file to upload"); + MessageHelper.addErrorAttribute(ra, "Please select a file to upload"); + ra.addFlashAttribute("uploadStatus", "Please select a file to upload"); return "redirect:/account/files/status"; } @@ -69,12 +71,12 @@ public class StoredFileController { this.storageService.storeFile(userService.currentUser(), file.getOriginalFilename(), bytes); message = "You successfully uploaded '" + file.getOriginalFilename() + "'"; - redirectAttributes.addFlashAttribute("uploadStatus", message); + ra.addFlashAttribute("uploadStatus", message); } catch (Exception e) { e.printStackTrace(); message = "Internal server error occured"; - redirectAttributes.addFlashAttribute("uploadStatus", message); + ra.addFlashAttribute("uploadStatus", message); } return "redirect:/account/files/status"; diff --git a/src/main/java/ru/bvn13/voidforum/controllers/admin/CommentController.java b/src/main/java/ru/bvn13/voidforum/controllers/admin/CommentController.java new file mode 100644 index 0000000..9162b9d --- /dev/null +++ b/src/main/java/ru/bvn13/voidforum/controllers/admin/CommentController.java @@ -0,0 +1,52 @@ +package ru.bvn13.voidforum.controllers.admin; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.Errors; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import ru.bvn13.voidforum.forms.CommentDeletionForm; +import ru.bvn13.voidforum.models.Comment; +import ru.bvn13.voidforum.models.Post; +import ru.bvn13.voidforum.services.CommentService; +import ru.bvn13.voidforum.services.PostService; + +import javax.validation.Valid; + +import java.nio.file.AccessDeniedException; + +import static org.springframework.web.bind.annotation.RequestMethod.DELETE; +import static org.springframework.web.bind.annotation.RequestMethod.POST; + +/** + * Created by bvn13 on 11.12.2017. + */ +@Controller("adminCommentController") +@RequestMapping("/admin/comments") +public class CommentController { + + @Autowired + private CommentService commentService; + + @Autowired + private PostService postService; + + + @RequestMapping(value = "/{commentId:[\\d]+}/delete", method = {POST}) + public String deleteComment(@PathVariable Long commentId, @Valid CommentDeletionForm form, Errors errors, Model model) throws Exception { + if (errors.hasErrors()) { + throw new Exception("Wrong request"); + } + Comment comment = commentService.getCommentById(commentId); + + if (!comment.getPost().getId().equals(form.getPostId())) { + throw new AccessDeniedException("Comment "+commentId+" does not belong to post "+form.getPostId()); + } + + commentService.deleteComment(commentId); + return "redirect:/posts/"+form.getPostId(); + } + +} diff --git a/src/main/java/ru/bvn13/voidforum/controllers/admin/PostController.java b/src/main/java/ru/bvn13/voidforum/controllers/admin/PostController.java index 54c527c..2697d7c 100644 --- a/src/main/java/ru/bvn13/voidforum/controllers/admin/PostController.java +++ b/src/main/java/ru/bvn13/voidforum/controllers/admin/PostController.java @@ -6,6 +6,7 @@ import ru.bvn13.voidforum.models.User; import ru.bvn13.voidforum.models.support.*; import ru.bvn13.voidforum.repositories.PostRepository; import ru.bvn13.voidforum.repositories.UserRepository; +import ru.bvn13.voidforum.services.CommentService; import ru.bvn13.voidforum.services.PostService; import ru.bvn13.voidforum.services.PrivilegeService; import ru.bvn13.voidforum.services.UserService; @@ -51,6 +52,7 @@ public class PostController { private UserService userService; + private static final int PAGE_SIZE = 20; @RequestMapping(value = "") @@ -134,6 +136,8 @@ public class PostController { return "redirect:/admin/posts"; } + + @RequestMapping(value = "", method = POST) public String create(Principal principal, @Valid PostForm postForm, Errors errors, Model model){ if (errors.hasErrors()) { diff --git a/src/main/java/ru/bvn13/voidforum/forms/CommentDeletionForm.java b/src/main/java/ru/bvn13/voidforum/forms/CommentDeletionForm.java new file mode 100644 index 0000000..90f4698 --- /dev/null +++ b/src/main/java/ru/bvn13/voidforum/forms/CommentDeletionForm.java @@ -0,0 +1,16 @@ +package ru.bvn13.voidforum.forms; + +import lombok.Data; + +import javax.validation.constraints.NotNull; + +/** + * Created by bvn13 on 11.12.2017. + */ +@Data +public class CommentDeletionForm { + + @NotNull + private Long postId; + +} diff --git a/src/main/java/ru/bvn13/voidforum/models/Post.java b/src/main/java/ru/bvn13/voidforum/models/Post.java index d657f12..b764311 100644 --- a/src/main/java/ru/bvn13/voidforum/models/Post.java +++ b/src/main/java/ru/bvn13/voidforum/models/Post.java @@ -98,4 +98,7 @@ public class Post extends BaseModel { @Column(nullable = false, columnDefinition = "boolean DEFAULT false") private Boolean censored; + + @Column(nullable = false, columnDefinition = "boolean DEFAULT false") + private Boolean disableCommenting; } diff --git a/src/main/java/ru/bvn13/voidforum/services/CommentService.java b/src/main/java/ru/bvn13/voidforum/services/CommentService.java index e6ff2fe..b8535d2 100644 --- a/src/main/java/ru/bvn13/voidforum/services/CommentService.java +++ b/src/main/java/ru/bvn13/voidforum/services/CommentService.java @@ -111,4 +111,10 @@ public class CommentService { return commentRepository.save(comment); } + public void deleteComment(Long commentId) { + Comment comment = this.getCommentById(commentId); + comment.setDeletedMark(!comment.getDeletedMark()); + commentRepository.save(comment); + } + } diff --git a/src/main/java/ru/bvn13/voidforum/support/web/MessageHelper.java b/src/main/java/ru/bvn13/voidforum/support/web/MessageHelper.java index 1247617..2736cbe 100644 --- a/src/main/java/ru/bvn13/voidforum/support/web/MessageHelper.java +++ b/src/main/java/ru/bvn13/voidforum/support/web/MessageHelper.java @@ -15,39 +15,79 @@ public final class MessageHelper { addAttribute(ra, message, Message.Type.SUCCESS, args); } + public static void addNamedSuccessAttribute(RedirectAttributes ra, String name, String message, Object... args) { + addNamedAttribute(ra, name, message, Message.Type.SUCCESS, args); + } + public static void addErrorAttribute(RedirectAttributes ra, String message, Object... args) { addAttribute(ra, message, Message.Type.DANGER, args); } + public static void addNamedErrorAttribute(RedirectAttributes ra, String name, String message, Object... args) { + addNamedAttribute(ra, name, message, Message.Type.DANGER, args); + } + public static void addInfoAttribute(RedirectAttributes ra, String message, Object... args) { addAttribute(ra, message, Message.Type.INFO, args); } + public static void addInfoAttribute(RedirectAttributes ra, String name, String message, Object... args) { + addNamedAttribute(ra, name, message, Message.Type.INFO, args); + } + public static void addWarningAttribute(RedirectAttributes ra, String message, Object... args) { addAttribute(ra, message, Message.Type.WARNING, args); } + public static void addNamedWarningAttribute(RedirectAttributes ra, String name, String message, Object... args) { + addNamedAttribute(ra, name, message, Message.Type.WARNING, args); + } + private static void addAttribute(RedirectAttributes ra, String message, Message.Type type, Object... args) { ra.addFlashAttribute(MESSAGE_ATTRIBUTE, new Message(message, type, args)); } + private static void addNamedAttribute(RedirectAttributes ra, String name, String message, Message.Type type, Object... args) { + ra.addFlashAttribute(name, new Message(message, type, args)); + } + public static void addSuccessAttribute(Model model, String message, Object... args) { addAttribute(model, message, Message.Type.SUCCESS, args); } + public static void addNamedSuccessAttribute(Model model, String name, String message, Object... args) { + addNamedAttribute(model, name, message, Message.Type.SUCCESS, args); + } + public static void addErrorAttribute(Model model, String message, Object... args) { addAttribute(model, message, Message.Type.DANGER, args); } + public static void addNamedErrorAttribute(Model model, String name, String message, Object... args) { + addNamedAttribute(model, name, message, Message.Type.DANGER, args); + } + public static void addInfoAttribute(Model model, String message, Object... args) { addAttribute(model, message, Message.Type.INFO, args); } + public static void addNamedInfoAttribute(Model model, String name, String message, Object... args) { + addNamedAttribute(model, name, message, Message.Type.INFO, args); + } + public static void addWarningAttribute(Model model, String message, Object... args) { addAttribute(model, message, Message.Type.WARNING, args); } + public static void addNamedWarningAttribute(Model model, String name, String message, Object... args) { + addNamedAttribute(model, name, message, Message.Type.WARNING, args); + } + private static void addAttribute(Model model, String message, Message.Type type, Object... args) { model.addAttribute(MESSAGE_ATTRIBUTE, new Message(message, type, args)); } + + private static void addNamedAttribute(Model model, String name, String message, Message.Type type, Object... args) { + model.addAttribute(name, new Message(message, type, args)); + } } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 5688450..16b250d 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -11,27 +11,32 @@ - - - - + + + + + + + + + diff --git a/src/main/resources/resources/css/mystyle.css b/src/main/resources/resources/css/mystyle.css index eecb672..80fe39b 100644 --- a/src/main/resources/resources/css/mystyle.css +++ b/src/main/resources/resources/css/mystyle.css @@ -196,4 +196,19 @@ h6 { } body { font: 300 14px "Helvetica Neue",Helvetica, Microsoft Yahei, Hiragino Sans GB, Microsoft Sans Serif, WenQuanYi Micro Hei, sans; +} + +.comment-header ul li { + float: left; + padding-right: 10px; + display: inline; +} + +.comment-header ul li.admin { + float: right; + display: inline; +} + +.btn { + color: #fff !important; } \ No newline at end of file diff --git a/src/main/resources/templates/account/posts/index.jade b/src/main/resources/templates/account/posts/index.jade index 4f69ecb..f5b65b4 100644 --- a/src/main/resources/templates/account/posts/index.jade +++ b/src/main/resources/templates/account/posts/index.jade @@ -52,7 +52,7 @@ block content script :javascript function deletePost(postId){ - if (confirm("Are you sure to delete post @"+postId)) { + if (confirm("Are you sure to delete post #"+postId)) { $('#form-'+postId).submit(); } } diff --git a/src/main/resources/templates/comments/list.jade b/src/main/resources/templates/comments/list.jade index 5341bf4..8579757 100644 --- a/src/main/resources/templates/comments/list.jade +++ b/src/main/resources/templates/comments/list.jade @@ -10,7 +10,15 @@ for comment in comments include fragments/pagination -if userService.currentUserCanWrite() +if userService.currentUserCanWrite() && !disableCommenting include fragments/commentCreationForm +script + :javascript + function deleteComment(postId, commentId) { + if (confirm("Are you sure to delete comment #" + commentId)) { + $('#form-' + postId + '-comment-' + commentId + '-delete').submit(); + } + } + diff --git a/src/main/resources/templates/comments/one.jade b/src/main/resources/templates/comments/one.jade index 65fb6a9..bd86766 100644 --- a/src/main/resources/templates/comments/one.jade +++ b/src/main/resources/templates/comments/one.jade @@ -1,13 +1,21 @@ .panel.panel-default - .panel-heading - span - b #{comment.getUser().getNickname()} - |, #{viewHelper.getFormattedDate(comment.getCreatedAt())} - div - if userService.isCurrentUserAdmin() - .td - | admin + .panel-heading.comment-header + ul + li + b #{comment.getUser().getNickname()} + |, #{viewHelper.getFormattedDate(comment.getCreatedAt())} + li.admin + if userService.isCurrentUserAdmin() + .td + a.btn.btn-xs.btn-danger.btn-delete(href="javascript:deleteComment(#{post.id}, #{comment.getId()})", postId="#{post.id}") + i.fa.fa-trash-o + + form(id="form-#{post.getId()}-comment-#{comment.getId()}-delete",style="visibility: hidden", method="post", action="/admin/comments/#{comment.getId()}/delete") + input(type="hidden", name='_csrf', value='#{_csrf.token}') + input(type="hidden", name='postId', value='#{post.getId()}') + + div(class="clearfix") .panel-body !{comment.getRenderedContent()} @@ -15,3 +23,6 @@ + + + diff --git a/src/main/resources/templates/tests/1.html b/src/main/resources/templates/tests/1.html deleted file mode 100644 index 44d70de..0000000 --- a/src/main/resources/templates/tests/1.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - Hello - - -
-
- - \ No newline at end of file diff --git a/src/main/resources/templates/tests/2.jade b/src/main/resources/templates/tests/2.jade deleted file mode 100644 index 70e01d6..0000000 --- a/src/main/resources/templates/tests/2.jade +++ /dev/null @@ -1,2 +0,0 @@ -// Created by bvn13 on 09.12.2017. -h2 HELLO \ No newline at end of file diff --git a/src/main/resources/templates/tests/3.html b/src/main/resources/templates/tests/3.html deleted file mode 100644 index 5afb260..0000000 --- a/src/main/resources/templates/tests/3.html +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - -
-

TEST 3

-
- - \ No newline at end of file