implemented ability of deleting comments by admin

master
Vyacheslav N. Boyko 2017-12-12 01:35:01 +03:00
parent ad77b46d26
commit 2405116185
18 changed files with 183 additions and 67 deletions

View File

@ -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);
}
}

View File

@ -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";
}

View File

@ -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";
}
}

View File

@ -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";

View File

@ -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();
}
}

View File

@ -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()) {

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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));
}
}

View File

@ -11,27 +11,32 @@
</appender>
<jmxConfigurator />
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
<logger name="ru.bvn13.voidforum" level="ALL">
<logger name="ru.bvn13.voidforum" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="org.hibernate.SQL" level="WARN">
<appender-ref ref="STDOUT" />
</logger>
<!--<logger name="org.hibernate.type" level="TRACE">-->
<!--<appender-ref ref="STDOUT" />-->
<!--</logger>-->
<logger name="org.springframework.web" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="ru.bvn13.voidforum.controllers" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<logger name="ru.bvn13.voidforum.admin.controllers" level="DEBUG">
<appender-ref ref="STDOUT" />
</logger>
<root level="WARN">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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 @@

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
</head>
<body>
<div th:include="tests/3 :: test3"></div>
<div th:include="tests/2"></div>
</body>
</html>

View File

@ -1,2 +0,0 @@
// Created by bvn13 on 09.12.2017.
h2 HELLO

View File

@ -1,12 +0,0 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
</head>
<body>
<div th:fragment="test3">
<h1>TEST 3</h1>
</div>
</body>
</html>