bstlboard-back/src/main/java/de/bstly/board/controller/ModerationController.java

123 lines
3.0 KiB
Java

/**
*
*/
package de.bstly.board.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.google.common.collect.Lists;
import de.bstly.board.businesslogic.CommentManager;
import de.bstly.board.businesslogic.EntryManager;
import de.bstly.board.businesslogic.UserManager;
import de.bstly.board.controller.support.EntityResponseStatusException;
import de.bstly.board.model.LocalUser;
/**
* The Class ModerationController.
*/
@RestController
@RequestMapping("/moderation")
public class ModerationController {
@Autowired
private CommentManager commentManager;
@Autowired
private EntryManager entryManager;
@Autowired
private UserManager userManager;
/**
* Delete comment.
*
* @param id the id
*/
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
@DeleteMapping("/comment/{id}")
public void deleteComment(@PathVariable("id") Long id) {
if (!commentManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
commentManager.delete(commentManager.get(id));
}
/**
* Delete entry.
*
* @param id the id
*/
@PreAuthorize("hasRole('ROLE_ADMIN') || hasRole('ROLE_MOD')")
@DeleteMapping("/entry/{id}")
public void deleteEntry(@PathVariable("id") Long id) {
if (!entryManager.exists(id)) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
entryManager.delete(entryManager.get(id));
}
/**
* Make mod.
*
* @param username the username
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@PutMapping("/user/{username}")
public void makeMod(@PathVariable("username") String username) {
LocalUser user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (user.getRoles() == null) {
user.setRoles(Lists.newArrayList());
}
if (user.getRoles().contains("ROLE_MOD")) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
user.getRoles().add("ROLE_MOD");
userManager.save(user);
}
/**
* Unmake mode.
*
* @param username the username
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("/user/{username}")
public void unmakeMode(@PathVariable("username") String username) {
LocalUser user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
}
if (user.getRoles() == null) {
user.setRoles(Lists.newArrayList());
}
if (!user.getRoles().contains("ROLE_MOD")) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
user.getRoles().remove("ROLE_MOD");
userManager.save(user);
}
}