1

Merge pull request #128 from hhu-propra2/refactor-controller

Refactor controller
This commit is contained in:
Christoph
2020-03-25 13:08:01 +01:00
committed by GitHub
8 changed files with 74 additions and 68 deletions

View File

@ -1,16 +1,18 @@
package mops.gruppen2.controller; package mops.gruppen2.controller;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.config.Gruppen2Config;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role; import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User; import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility; import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupFullException;
import mops.gruppen2.domain.exception.GroupNotFoundException; import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.domain.exception.NoAccessException;
import mops.gruppen2.domain.exception.NoAdminAfterActionException; 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.domain.exception.WrongFileException;
import mops.gruppen2.domain.exception.*;
import mops.gruppen2.security.Account; import mops.gruppen2.security.Account;
import mops.gruppen2.service.ControllerService; import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.CsvService; import mops.gruppen2.service.CsvService;
@ -18,8 +20,6 @@ import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.KeyCloakService; import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService; import mops.gruppen2.service.UserService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -41,22 +41,18 @@ import java.util.UUID;
@Controller @Controller
@SessionScope @SessionScope
@RequestMapping("/gruppen2") @RequestMapping("/gruppen2")
public class Gruppen2Controller { public class WebController {
private final KeyCloakService keyCloakService; private final KeyCloakService keyCloakService;
private final GroupService groupService; private final GroupService groupService;
private final UserService userService; private final UserService userService;
private final ControllerService controllerService; private final ControllerService controllerService;
private final Gruppen2Config gruppen2Config;
private final Logger logger = LoggerFactory.getLogger("Gruppen2ControllerLogger");
;
public Gruppen2Controller(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, Gruppen2Config gruppen2Config) { public WebController(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService) {
this.keyCloakService = keyCloakService; this.keyCloakService = keyCloakService;
this.groupService = groupService; this.groupService = groupService;
this.userService = userService; this.userService = userService;
this.controllerService = controllerService; this.controllerService = controllerService;
this.gruppen2Config = gruppen2Config;
} }
/** /**
@ -99,29 +95,8 @@ public class Gruppen2Controller {
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException { @RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException {
Account account = keyCloakService.createAccountFromPrincipal(token); Account account = keyCloakService.createAccountFromPrincipal(token);
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.warn("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
visibility = visibility == null;
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
UUID parentUUID = controllerService.getUUID(parent); UUID parentUUID = controllerService.getUUID(parent);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, userList);
return "redirect:/gruppen2/"; return "redirect:/gruppen2/";
} }
@ -173,7 +148,6 @@ public class Gruppen2Controller {
} }
UUID groupUUID = controllerService.getUUID(groupId); UUID groupUUID = controllerService.getUUID(groupId);
controllerService.addUserList(userList, groupUUID); controllerService.addUserList(userList, groupUUID);
return "redirect:/gruppen2/details/members/" + groupId; return "redirect:/gruppen2/details/members/" + groupId;
} }
@ -229,7 +203,7 @@ public class Gruppen2Controller {
if (search != null) { if (search != null) {
groupse = groupService.findGroupWith(search, account); groupse = groupService.findGroupWith(search, account);
} }
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("account", account);
model.addAttribute("gruppen", groupse); model.addAttribute("gruppen", groupse);
return "search"; return "search";
} }
@ -238,6 +212,7 @@ public class Gruppen2Controller {
@GetMapping("/details/{id}") @GetMapping("/details/{id}")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, HttpServletRequest request, @PathVariable("id") String groupId) throws EventException { public String showGroupDetails(KeycloakAuthenticationToken token, Model model, HttpServletRequest request, @PathVariable("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = userService.getGroupById(UUID.fromString(groupId));
Account account = keyCloakService.createAccountFromPrincipal(token); Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
@ -283,7 +258,6 @@ public class Gruppen2Controller {
public String joinGroup(KeycloakAuthenticationToken token, public String joinGroup(KeycloakAuthenticationToken token,
Model model, @RequestParam("id") String groupId) throws EventException { Model model, @RequestParam("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Account account = keyCloakService.createAccountFromPrincipal(token); Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId)); Group group = userService.getGroupById(UUID.fromString(groupId));
@ -302,9 +276,9 @@ public class Gruppen2Controller {
@GetMapping("/detailsSearch") @GetMapping("/detailsSearch")
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, public String showGroupDetailsNoMember(KeycloakAuthenticationToken token,
Model model, Model model,
@RequestParam("id") String id) throws EventException { @RequestParam("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(id)); Group group = userService.getGroupById(UUID.fromString(groupId));
UUID parentId = group.getParent(); UUID parentId = group.getParent();
Group parent = new Group(); Group parent = new Group();
@ -393,8 +367,6 @@ public class Gruppen2Controller {
public String changeRole(KeycloakAuthenticationToken token, public String changeRole(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId, @RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException { @RequestParam("user_id") String userId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token); Account account = keyCloakService.createAccountFromPrincipal(token);
if (userId.equals(account.getName())) { if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) { if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {

View File

@ -3,6 +3,7 @@ package mops.gruppen2.domain.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
public class NoAccessException extends EventException { public class NoAccessException extends EventException {
public NoAccessException(String info) { public NoAccessException(String info) {
super(HttpStatus.FORBIDDEN, "Hier hast du leider keinen Zugriff!", info); super(HttpStatus.FORBIDDEN, "Hier hast du leider keinen Zugriff!", info);
} }

View File

@ -3,6 +3,7 @@ package mops.gruppen2.domain.exception;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
public class PageNotFoundException extends EventException { public class PageNotFoundException extends EventException {
public PageNotFoundException(String info) { public PageNotFoundException(String info) {
super(HttpStatus.NOT_FOUND, "Die Seite wurde nicht gefunden!", info); super(HttpStatus.NOT_FOUND, "Die Seite wurde nicht gefunden!", info);
} }

View File

@ -1,5 +1,6 @@
package mops.gruppen2.service; package mops.gruppen2.service;
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.GroupType; import mops.gruppen2.domain.GroupType;
import mops.gruppen2.domain.Role; import mops.gruppen2.domain.Role;
@ -15,9 +16,13 @@ import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.domain.event.UpdateUserMaxEvent; import mops.gruppen2.domain.event.UpdateUserMaxEvent;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.UserNotFoundException; import mops.gruppen2.domain.exception.UserNotFoundException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.security.Account; import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.CharConversionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
@ -71,10 +76,27 @@ public class ControllerService {
updateRole(account.getName(), groupId); updateRole(account.getName(), groupId);
} }
public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent, List<User> users) throws EventException { public void createOrga(Account account, String title, String description, Boolean visibility, Boolean lecture, Boolean maxInfiniteUsers, Long userMaximum, UUID parent, MultipartFile file) 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;
Visibility visibility1; Visibility visibility1;
UUID groupId = eventService.checkGroup(); UUID groupId = eventService.checkGroup();
if (visibility) { if (visibility) {
visibility1 = Visibility.PUBLIC; visibility1 = Visibility.PUBLIC;
} else { } else {
@ -100,7 +122,7 @@ public class ControllerService {
updateTitle(account, groupId, title); updateTitle(account, groupId, title);
updateDescription(account, groupId, description); updateDescription(account, groupId, description);
updateRole(account.getName(), groupId); updateRole(account.getName(), groupId);
addUserList(users, groupId); addUserList(userList, groupId);
} }

View File

@ -5,7 +5,6 @@ import mops.gruppen2.domain.User;
import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.event.Event;
import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.GroupNotFoundException; import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.repository.EventRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
@ -16,12 +15,10 @@ import java.util.UUID;
@Service @Service
public class UserService { public class UserService {
private final EventRepository eventRepository;
private final GroupService groupService; private final GroupService groupService;
private final EventService eventService; private final EventService eventService;
public UserService(EventRepository eventRepository, GroupService groupService, EventService eventService) { public UserService(GroupService groupService, EventService eventService) {
this.eventRepository = eventRepository;
this.groupService = groupService; this.groupService = groupService;
this.eventService = eventService; this.eventService = eventService;
} }

View File

@ -1,7 +1,8 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" <html lang="en"
th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}" th:replace="~{mopslayout :: html(name='Gruppenbildung', headcontent=~{:: headcontent}, navigation=~{:: navigation}, bodycontent=~{:: bodycontent})}"
xmlns="http://www.w3.org/1999/html"> xmlns="http://www.w3.org/1999/html"
xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Gruppenerstellung</title> <title>Gruppenerstellung</title>
@ -16,10 +17,11 @@
</head> </head>
<body> <body>
<header> <header>
<nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation" th:switch="${account.getRoles().contains('orga')}"> <nav class="navigation navigation-secondary" is="mops-navigation" th:fragment="navigation"
th:switch="${account.getRoles().contains('orga')}">
<ul> <ul>
<li class="active"> <li class="active">
<a th:href="@{/gruppen2}" href="/">Gruppen</a> <a href="/" th:href="@{/gruppen2}">Gruppen</a>
</li> </li>
<li th:case="${true}"> <li th:case="${true}">
<a href="/createOrga" th:href="@{/gruppen2/createOrga}">Erstellen</a> <a href="/createOrga" th:href="@{/gruppen2/createOrga}">Erstellen</a>
@ -28,7 +30,7 @@
<a href="/createStudent" th:href="@{/gruppen2/createStudent}">Erstellen</a> <a href="/createStudent" th:href="@{/gruppen2/createStudent}">Erstellen</a>
</li> </li>
<li> <li>
<a th:href="@{/gruppen2/findGroup}" href="/findGroup">Suche</a> <a href="/findGroup" th:href="@{/gruppen2/findGroup}">Suche</a>
</li> </li>
</ul> </ul>
</nav> </nav>
@ -38,13 +40,13 @@
<div class="row"> <div class="row">
<div class="col-10"> <div class="col-10">
<h1>Metadaten ändern</h1> <h1>Metadaten ändern</h1>
<form method="post" action="/gruppen2/details/changeMetadata"> <form action="/gruppen2/details/changeMetadata" method="post">
<div class="shadow-sm p-2" <div class="shadow-sm p-2"
style=" border: 10px solid aliceblue; background: aliceblue"> style=" border: 10px solid aliceblue; background: aliceblue">
<div class="form-group"> <div class="form-group">
<label for="title">Titel</label> <label for="title">Titel</label>
<input class="form-control" id="title" required <input class="form-control" id="title" required
type="text" th:name="title" th:value="${title}"> th:name="title" th:value="${title}" type="text">
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="description">Beschreibung</label> <label for="description">Beschreibung</label>
@ -55,9 +57,9 @@
<div class="form-group pt-4"> <div class="form-group pt-4">
<button class="btn btn-primary" <button class="btn btn-primary"
style="background: #52a1eb; border-style: none" style="background: #52a1eb; border-style: none"
th:if="${roles.get(user.getId()) == admin}"
th:name="groupId" th:name="groupId"
th:value="${groupId}" th:value="${groupId}"
th:if="${roles.get(user.getId()) == admin}"
type="submit">Speichern type="submit">Speichern
</button> </button>
</div> </div>

View File

@ -6,7 +6,8 @@
<meta charset="utf-8"> <meta charset="utf-8">
<title>Gruppendetails</title> <title>Gruppendetails</title>
<th:block th:fragment="headcontent"> <th:block th:fragment="headcontent">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"
rel="stylesheet">
</th:block> </th:block>
</head> </head>
<body> <body>
@ -31,11 +32,14 @@
<main th:fragment="bodycontent"> <main th:fragment="bodycontent">
<div class="container-fluid"> <div class="container-fluid">
<div> <div>
<div class="shadow-sm p-4 col-8" style="border: 10px solid aliceblue; display: inline-block; border-radius: 5px; background: aliceblue"> <div class="shadow-sm p-4 col-8"
style="border: 10px solid aliceblue; display: inline-block; border-radius: 5px; background: aliceblue">
<div class="row"> <div class="row">
<h1 style="color: black; font-weight: bold; font-optical-sizing: auto; overflow-wrap: break-word; width: 95%" th:text="${group.getTitle()}"></h1> <h1 style="color: black; font-weight: bold; font-optical-sizing: auto; overflow-wrap: break-word; width: 95%"
<a th:href="@{/gruppen2/details/changeMetadata/{id}(id=${group.getId()})}" th:text="${group.getTitle()}"></h1>
class="fa fa-pencil" style="font-size:30px; width: 5%" <a class="fa fa-pencil"
style="font-size:30px; width: 5%"
th:href="@{/gruppen2/details/changeMetadata/{id}(id=${group.getId()})}"
th:if="${roles.get(user.getId()) == admin}"></a> th:if="${roles.get(user.getId()) == admin}"></a>
</div> </div>
<h3> <h3>
@ -48,14 +52,20 @@
<span class="badge badge-pill badge-info" style="background: mediumorchid" <span class="badge badge-pill badge-info" style="background: mediumorchid"
th:text="${parent.getTitle()}">Parent</span> th:text="${parent.getTitle()}">Parent</span>
<div class="input-group mb-3" style="margin-top: 10px" th:if="${group.getVisibility() == group.getVisibility().PRIVATE}"> <div class="input-group mb-3" style="margin-top: 10px"
th:if="${group.getVisibility() == group.getVisibility().PRIVATE}">
<div class="input-group-prepend"> <div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default" style="background: #52a1eb">Einladungslink:</span> <span class="input-group-text" id="inputGroup-sizing-default"
style="background: #52a1eb">Einladungslink:</span>
</div> </div>
<input type="text" class="form-control" th:value="${link}" <input aria-describedby="basic-addon2" aria-label="Recipient's username"
aria-label="Recipient's username" aria-describedby="basic-addon2" id="groupLink" style="background: white" readonly> class="form-control"
id="groupLink" readonly style="background: white" th:value="${link}"
type="text">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-outline-secondary" type="button" onclick="copyLink()">Link kopieren</button> <button class="btn btn-outline-secondary" onclick="copyLink()"
type="button">Link kopieren
</button>
</div> </div>
</div> </div>
</h3> </h3>

View File

@ -29,7 +29,8 @@
<button class="btn btn-primary" <button class="btn btn-primary"
style="background: #52a1eb; border-style: none;"> style="background: #52a1eb; border-style: none;">
<a style="color: white" href="#" onclick="window.history.back(-1);return false;" role="button">Zurück</a> <a href="#" onclick="window.history.back(-1);return false;" role="button"
style="color: white">Zurück</a>
</button> </button>
</div> </div>
</div> </div>