1

Refactor WebController and ValidationService

Co-Authored-By: Talha Caliskan <killerber4t@users.noreply.github.com>
This commit is contained in:
XXNitram
2020-03-25 15:43:41 +01:00
parent 1dd3f9b6fe
commit 0c43050166
3 changed files with 120 additions and 115 deletions

View File

@ -1,34 +1,21 @@
package mops.gruppen2.controller;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupFullException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.domain.exception.NoAccessException;
import mops.gruppen2.domain.exception.NoAdminAfterActionException;
import mops.gruppen2.domain.exception.PageNotFoundException;
import mops.gruppen2.domain.exception.UserAlreadyExistsException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.*;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.security.RolesAllowed;
import javax.servlet.http.HttpServletRequest;
import java.io.CharConversionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -94,7 +81,10 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
UUID parentUUID = controllerService.getUUID(parent);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
List<User> userList = new ArrayList<>();
Group group = userService.getGroupById(controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID));
userList = validationService.checkFile(file, userList, group.getId().toString(), group, account);
controllerService.addUserList(userList, group.getId());
return "redirect:/gruppen2/";
}
@ -134,17 +124,7 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
Group group = userService.getGroupById(UUID.fromString(groupId));
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() + group.getMembers().size() > group.getUserMaximum()) {
controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size());
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
throw new WrongFileException(file.getOriginalFilename());
}
}
userList = validationService.checkFile(file, userList, groupId, group, account);
UUID groupUUID = controllerService.getUUID(groupId);
controllerService.addUserList(userList, groupUUID);
return "redirect:/gruppen2/details/members/" + groupId;
@ -156,13 +136,11 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
model.addAttribute("account", account);
UUID parentId = group.getParent();
Group parent = new Group();
if (!group.getMembers().contains(user)) {
if (group.getVisibility() == Visibility.PRIVATE) {
return "privateGroupNoMember";
}
if (!validationService.checkIfUserInGroup(group, user)) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
@ -185,10 +163,10 @@ public class WebController {
@RequestParam("groupId") String groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
if(validationService.checkTitleAndDescription(title, description)) {
controllerService.updateTitle(account, UUID.fromString(groupId), title);
controllerService.updateDescription(account, UUID.fromString(groupId), description);
}
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
validationService.checkTitleAndDescription(title, description, account, groupId);
return "redirect:/gruppen2/details/" + groupId;
}
@ -198,12 +176,10 @@ public class WebController {
Model model,
@RequestParam(value = "suchbegriff", required = false) String search) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<Group> groupse = new ArrayList<>();
if (!validationService.checkSearch(search)) {
groupse = groupService.findGroupWith(search, account);
}
List<Group> groups = new ArrayList<>();
groups = validationService.checkSearch(search, groups, account);
model.addAttribute("account", account);
model.addAttribute("gruppen", groupse);
model.addAttribute("gruppen", groups);
return "search";
}
@ -216,13 +192,11 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
UUID parentId = group.getParent();
String URL = request.getRequestURL().toString();
String serverURL = URL.substring(0, URL.indexOf("gruppen2/"));
validationService.checkGroup(group.getTitle());
Group parent = validationService.checkParent(parentId);
if (!validationService.checkIfUserInGroup(Group group, User user)) {
if (!validationService.checkIfUserInGroup(group, user)) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
@ -235,6 +209,9 @@ public class WebController {
model.addAttribute("roles", group.getRoles());
model.addAttribute("user", user);
model.addAttribute("admin", Role.ADMIN);
String URL = request.getRequestURL().toString();
String serverURL = URL.substring(0, URL.indexOf("gruppen2/"));
model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId);
return "detailsMember";
@ -249,9 +226,7 @@ public class WebController {
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfUserInGroupJoin(group, user);
if (group.getUserMaximum() < group.getMembers().size()+1) {
throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten.");
}
validationService.checkIfGroupFull(group);
controllerService.addUser(account, UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@ -266,14 +241,12 @@ public class WebController {
UUID parentId = group.getParent();
Group parent = validationService.checkParent(parentId);
if (group.getUserMaximum() > group.getMembers().size()) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
validationService.checkIfGroupFull(group);
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
throw new GroupNotFoundException("@search");
return "detailsNoMember";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -282,11 +255,9 @@ public class WebController {
Model model, @PathVariable String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId));
if (group != null) {
model.addAttribute("group", group);
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
}
throw new GroupNotFoundException("@accept");
validationService.checkGroup(group.getTitle());
model.addAttribute("group", group);
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@ -297,11 +268,7 @@ public class WebController {
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
controllerService.passIfLastAdmin(account, UUID.fromString(groupId));
controllerService.deleteUser(user.getId(), UUID.fromString(groupId));
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
}
validationService.checkIfGroupEmpty(groupId, user);
return "redirect:/gruppen2/";
}
@ -312,9 +279,7 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
if (group.getRoles().get(user.getId()) != Role.ADMIN) {
throw new NoAccessException("");
}
validationService.checkIfAdmin(group, user);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@ -327,19 +292,12 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId));
User user = new User(account.getName(), "", "", "");
if (group.getMembers().contains(user)) {
if (group.getRoles().get(account.getName()) == Role.ADMIN) {
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
model.addAttribute("admin", Role.ADMIN);
return "editMembers";
} else {
return "redirect:/details/";
}
} else {
return "privateGroupNoMember";
}
validationService.checkIfAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
model.addAttribute("admin", Role.ADMIN);
return "editMembers";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@ -348,14 +306,9 @@ public class WebController {
@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
throw new NoAdminAfterActionException("Du otto bist letzter Admin");
}
controllerService.updateRole(userId, UUID.fromString(groupId));
if (validationService.checkIfDemotingSelf(userId, groupId, account)) {
return "redirect:/gruppen2/details/" + groupId;
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -373,10 +326,9 @@ public class WebController {
@PostMapping("/details/members/deleteUser")
public String deleteUser(@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException {
User user = new User(userId, "", "", "");
controllerService.deleteUser(userId, UUID.fromString(groupId));
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(userId, UUID.fromString(groupId));
}
validationService.checkIfGroupEmpty(groupId, user);
return "redirect:/gruppen2/details/members/" + groupId;
}

View File

@ -76,22 +76,11 @@ public class ControllerService {
updateRole(account.getName(), groupId);
}
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent, MultipartFile file) throws EventException, IOException {
public UUID createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent) throws EventException, IOException {
List<User> userList = new ArrayList<>();
if (userMaximum == null) {
userMaximum = 100000L;
}
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() > userMaximum) {
userMaximum = (long) userList.size() + userMaximum;
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
logger.warning("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
visibility = visibility == null;
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
@ -122,7 +111,8 @@ public class ControllerService {
updateTitle(account, groupId, title);
updateDescription(account, groupId, description);
updateRole(account.getName(), groupId);
addUserList(userList, groupId);
return groupId;
}

View File

@ -1,45 +1,62 @@
package mops.gruppen2.service;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.domain.exception.NoValueException;
import mops.gruppen2.domain.exception.UserAlreadyExistsException;
import mops.gruppen2.domain.exception.*;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.ValidationException;
import java.io.CharConversionException;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@Service
public class ValidationService {
private final ControllerService controllerService;
private final UserService userService;
private final GroupService groupService;
public ValidationService(ControllerService controllerService, UserService userService) {
public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) {
this.controllerService = controllerService;
this.userService = userService;
this.groupService = groupService;
}
public boolean checkTitleAndDescription(String title, String description) {
if(title != null && description != null) return true;
throw new NoValueException("Titel und Beschreibung müssen ausgefüllt werden");
public void checkTitleAndDescription(String title, String description, Account account, String groupId) {
if (title == null || description == null) {
throw new NoValueException("Titel und Beschreibung müssen ausgefüllt werden");
}
controllerService.updateTitle(account, UUID.fromString(groupId), title);
controllerService.updateDescription(account, UUID.fromString(groupId), description);
}
public boolean checkSearch(String search) {
return search != null;
public List<Group> checkSearch(String search, List<Group> groups, Account account) {
if (search != null) {
groups = groupService.findGroupWith(search, account);
}
return groups;
}
public void checkGroup(String title){
public void checkGroup(String title) {
if(title == null) throw new GroupNotFoundException("@details");
}
public boolean checkIfUserInGroup(Group group, User user) {
if (group.getVisibility() == Visibility.PRIVATE) {
throw new GroupNotFoundException("Du hast keine Zugriffsberechtigung");
}
return group.getMembers().contains(user);
if (!group.getMembers().contains(user) && group.getVisibility() == Visibility.PRIVATE) {
throw new NoAccessException("");
} else return group.getMembers().contains(user);
}
public Group checkParent(UUID parentId) {
Group parent = null;
Group parent = new Group();
if (!controllerService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
@ -51,4 +68,50 @@ public class ValidationService {
throw new UserAlreadyExistsException("@details");
}
}
public void checkIfGroupFull(Group group) {
if (group.getUserMaximum() < group.getMembers().size() + 1) {
throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten.");
}
}
public void checkIfGroupEmpty(String groupId, User user) {
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
}
}
public void checkIfAdmin(Group group, User user) {
checkIfUserInGroup(group, user);
if (group.getRoles().get(user.getId()) != Role.ADMIN) {
throw new NoAccessException("");
}
}
public boolean checkIfDemotingSelf(String userId, String groupId, Account account) {
if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
throw new NoAdminAfterActionException("Du Otto bist letzter Admin");
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return true;
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return false;
}
public List<User> checkFile(MultipartFile file, List<User> userList, String groupId, Group group, Account account) {
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() + group.getMembers().size() > group.getUserMaximum()) {
controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size());
}
} catch (IOException ex) {
throw new WrongFileException(file.getOriginalFilename());
}
}
return userList;
}
}