From 3fc4d82dbe1e64e1196c8afb94662e5e713679c0 Mon Sep 17 00:00:00 2001 From: Lukas Ettel <34522828+LukasEttel@users.noreply.github.com> Date: Thu, 26 Mar 2020 16:57:46 +0100 Subject: [PATCH] Futher further refactor Co-authored-by: AndiBuls <49692239+andibuls@users.noreply.github.com> Co-authored-by: XXNitram <17749825+xxnitram@users.noreply.github.com> --- .../gruppen2/controller/WebController.java | 30 ++++++++++++++----- .../gruppen2/service/ValidationService.java | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/main/java/mops/gruppen2/controller/WebController.java b/src/main/java/mops/gruppen2/controller/WebController.java index 77b2cd3..8edeec0 100644 --- a/src/main/java/mops/gruppen2/controller/WebController.java +++ b/src/main/java/mops/gruppen2/controller/WebController.java @@ -201,11 +201,10 @@ public class WebController { UUID parentId = group.getParent(); String actualURL = request.getRequestURL().toString(); String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/")); + Group parent = controllerService.getParent(parentId); validationService.throwIfGroupNotExisting(group.getTitle()); - Group parent = controllerService.getParent(parentId); - model.addAttribute("account", account); if (!validationService.checkIfUserInGroup(group, user)) { validationService.throwIfNoAccessToPrivate(group, user); @@ -232,13 +231,16 @@ public class WebController { @PostMapping("/detailsBeitreten") public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam("id") String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); 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.throwIfUserAlreadyInGroup(group, user); - validationService.checkIfGroupFull(group); + validationService.throwIfGroupFull(group); + controllerService.addUser(account, UUID.fromString(groupId)); + + model.addAttribute("account", account); return "redirect:/gruppen2/"; } @@ -247,12 +249,16 @@ public class WebController { public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam("id") String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); + Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); - validationService.checkIfGroupFull(group); - UUID parentId = group.getParent(); Group parent = controllerService.getParent(parentId); + User user = new User(account.getName(), "", "", ""); + + model.addAttribute("account", account); + if (validationService.checkIfUserInGroup(group, user)){ + return "redirect:/gruppen2/details/" + groupId; + } model.addAttribute("group", group); model.addAttribute("parentId", parentId); @@ -265,13 +271,18 @@ public class WebController { @GetMapping("/acceptinvite/{groupId}") public String acceptInvite(KeycloakAuthenticationToken token, Model model, @PathVariable String groupId) throws EventException { - model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); + Account account = keyCloakService.createAccountFromPrincipal(token); Group group = userService.getGroupById(UUID.fromString(groupId)); + validationService.throwIfGroupNotExisting(group.getTitle()); + + model.addAttribute("account", account); model.addAttribute("group", group); return "redirect:/gruppen2/detailsSearch?id=" + group.getId(); } + //Bis hier gekommen + @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) @PostMapping("/leaveGroup") public String pLeaveGroup(KeycloakAuthenticationToken token, @@ -318,6 +329,9 @@ public class WebController { @RequestParam("group_id") String groupId, @RequestParam("user_id") String userId) throws EventException { Account account = keyCloakService.createAccountFromPrincipal(token); + Group group = userService.getGroupById(UUID.fromString(groupId)); + User user = new User(account.getName(), "", "", ""); + validationService.throwIfNoAdmin(group, user); if (validationService.checkIfDemotingSelf(userId, groupId, account)) { return "redirect:/gruppen2/details/" + groupId; } diff --git a/src/main/java/mops/gruppen2/service/ValidationService.java b/src/main/java/mops/gruppen2/service/ValidationService.java index 6de3fa4..6d67175 100644 --- a/src/main/java/mops/gruppen2/service/ValidationService.java +++ b/src/main/java/mops/gruppen2/service/ValidationService.java @@ -58,7 +58,7 @@ public class ValidationService { } } - public void checkIfGroupFull(Group group) { + public void throwIfGroupFull(Group group) { if (group.getUserMaximum() < group.getMembers().size() + 1) { throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten."); }