1

Futher refactor

Co-authored-by: AndiBuls <49692239+andibuls@users.noreply.github.com>
Co-authored-by: XXNitram <17749825+xxnitram@users.noreply.github.com>
This commit is contained in:
Lukas Ettel
2020-03-26 16:31:17 +01:00
parent b5d688311d
commit d58cde597b
6 changed files with 85 additions and 60 deletions

View File

@ -61,6 +61,7 @@ public class WebController {
public String index(KeycloakAuthenticationToken token, Model model) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen", userService.getUserGroups(user));
model.addAttribute("user", user);
@ -71,6 +72,7 @@ public class WebController {
@GetMapping("/createOrga")
public String createGroupAsOrga(KeycloakAuthenticationToken token, Model model) {
Account account = keyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic());
return "createOrga";
@ -92,8 +94,8 @@ public class WebController {
UUID parentUUID = controllerService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
return "redirect:/gruppen2/";
}
@ -101,6 +103,7 @@ public class WebController {
@GetMapping("/createStudent")
public String createGroupAsStudent(KeycloakAuthenticationToken token, Model model) {
Account account = keyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic());
return "createStudent";
@ -118,7 +121,9 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
UUID parentUUID = controllerService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
controllerService.createGroup(account, title, description, visibility, null, maxInfiniteUsers, userMaximum, parentUUID);
return "redirect:/gruppen2/";
}
@ -129,6 +134,7 @@ public class WebController {
@RequestParam("group_id") String groupId,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
Account account = keyCloakService.createAccountFromPrincipal(token);
controllerService.addUsersFromCsv(account, file, groupId);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -139,16 +145,12 @@ 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 (!validationService.checkIfUserInGroup(group, user)) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
validationService.throwIfNoAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("title", group.getTitle());
model.addAttribute("description", group.getDescription());
model.addAttribute("admin", Role.ADMIN);
@ -160,16 +162,19 @@ public class WebController {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/details/changeMetadata")
public String pChangeMetadata(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("groupId") String groupId) throws EventException {
public String postChangeMetadata(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("groupId") String groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
User user = new User(account.getName(), "", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
validationService.checkTitleAndDescription(title, description, account, groupId);
validationService.throwIfNoAdmin(group, user);
validationService.checkFields(title, description);
controllerService.changeMetaData(account, group, title, description);
return "redirect:/gruppen2/details/" + groupId;
}
@ -181,6 +186,7 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<Group> groups = new ArrayList<>();
groups = validationService.checkSearch(search, groups, account);
model.addAttribute("account", account);
model.addAttribute("gruppen", groups);
return "search";
@ -189,17 +195,20 @@ public class WebController {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details/{id}")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, HttpServletRequest request, @PathVariable("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId));
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
UUID parentId = group.getParent();
String actualURL = request.getRequestURL().toString();
String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/"));
validationService.checkGroup(group.getTitle());
Group parent = validationService.checkParent(parentId);
validationService.throwIfGroupNotExisting(group.getTitle());
Group parent = controllerService.getParent(parentId);
model.addAttribute("account", account);
if (!validationService.checkIfUserInGroup(group, user)) {
validationService.throwIfNoAccessToPrivate(group, user);
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
@ -213,10 +222,9 @@ public class WebController {
model.addAttribute("user", user);
model.addAttribute("admin", Role.ADMIN);
String actualURL = request.getRequestURL().toString();
String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/"));
model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId);
if (validationService.checkIfAdmin(group, user)) {
model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + groupId);
}
return "detailsMember";
}
@ -228,7 +236,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));
validationService.checkIfUserInGroupJoin(group, user);
validationService.throwIfUserAlreadyInGroup(group, user);
validationService.checkIfGroupFull(group);
controllerService.addUser(account, UUID.fromString(groupId));
return "redirect:/gruppen2/";
@ -244,7 +252,7 @@ public class WebController {
validationService.checkIfGroupFull(group);
UUID parentId = group.getParent();
Group parent = validationService.checkParent(parentId);
Group parent = controllerService.getParent(parentId);
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
@ -259,7 +267,7 @@ public class WebController {
Model model, @PathVariable String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkGroup(group.getTitle());
validationService.throwIfGroupNotExisting(group.getTitle());
model.addAttribute("group", group);
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
}
@ -283,7 +291,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));
validationService.checkIfAdmin(group, user);
validationService.throwIfNoAdmin(group, user);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@ -296,7 +304,7 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId));
User user = new User(account.getName(), "", "", "");
validationService.checkIfAdmin(group, user);
validationService.throwIfNoAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);

View File

@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus;
public class BadParameterException extends EventException {
public BadParameterException(String info) {
super(HttpStatus.INTERNAL_SERVER_ERROR, "Fehlerhafter Parameter angegeben!", info);
super(HttpStatus.BAD_REQUEST, "Fehlerhafter Parameter angegeben!", info);
}
}

View File

@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus;
public class NoValueException extends EventException {
public NoValueException(String info) {
super(HttpStatus.NO_CONTENT, "Eine Information fehlt.", info);
super(HttpStatus.BAD_REQUEST, "Eine Information fehlt.", info);
}
}

View File

@ -108,6 +108,24 @@ public class ControllerService {
addUserList(newUserList, groupUUID);
}
public void changeMetaData(Account account, Group group, String title, String description) {
if (!title.equals(group.getTitle())){
updateTitle(account, group.getId(), title);
}
if (!description.equals(group.getDescription())) {
updateDescription(account, group.getId(), description);
}
}
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
return parent;
}
private void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);

View File

@ -9,14 +9,10 @@ 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.NoValueException;
import mops.gruppen2.domain.exception.UserAlreadyExistsException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
@ -33,14 +29,6 @@ public class ValidationService {
this.groupService = groupService;
}
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 List<Group> checkSearch(String search, List<Group> groups, Account account) {
if (search != null) {
groups = groupService.findGroupWith(search, account);
@ -48,30 +36,24 @@ public class ValidationService {
return groups;
}
public void checkGroup(String title) {
public void throwIfGroupNotExisting(String title) {
if (title == null) {
throw new GroupNotFoundException("@details");
}
}
public boolean checkIfUserInGroup(Group group, User user) {
if (!group.getMembers().contains(user) && group.getVisibility() == Visibility.PRIVATE) {
public void throwIfNoAccessToPrivate(Group group, User user) {
if (!checkIfUserInGroup(group, user) && group.getVisibility() == Visibility.PRIVATE) {
throw new NoAccessException("");
} else {
return group.getMembers().contains(user);
}
}
public Group checkParent(UUID parentId) {
Group parent = new Group();
if (!controllerService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
return parent;
public boolean checkIfUserInGroup(Group group, User user) {
return group.getMembers().contains(user);
}
public void checkIfUserInGroupJoin(Group group, User user) {
if (group.getMembers().contains(user)) {
public void throwIfUserAlreadyInGroup(Group group, User user) {
if (checkIfUserInGroup(group, user)) {
throw new UserAlreadyExistsException("@details");
}
}
@ -89,13 +71,20 @@ public class ValidationService {
}
}
public void checkIfAdmin(Group group, User user) {
checkIfUserInGroup(group, user);
public void throwIfNoAdmin(Group group, User user) {
throwIfNoAccessToPrivate(group, user);
if (group.getRoles().get(user.getId()) != Role.ADMIN) {
throw new NoAccessException("");
}
}
public boolean checkIfAdmin(Group group, User user) {
if (checkIfUserInGroup(group, user)) {
return group.getRoles().get(user.getId()) == Role.ADMIN;
}
return false;
}
public boolean checkIfDemotingSelf(String userId, String groupId, Account account) {
if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
@ -115,7 +104,7 @@ public class ValidationService {
* @param title Der Titel der Gruppe
* @param userMaximum Das user Limit der Gruppe
*/
public void checkFields(String description, String title, Long userMaximum, Boolean maxInfiniteUsers) {
public void checkFields(String title, String description, Long userMaximum, Boolean maxInfiniteUsers) {
if (description == null || description.trim().length() == 0) {
throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben");
}
@ -135,6 +124,16 @@ public class ValidationService {
}
}
public void checkFields(String title, String description) {
if (description == null || description.trim().length() == 0) {
throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben");
}
if (title == null || title.trim().length() == 0) {
throw new BadParameterException("Der Titel wurde nicht korrekt angegeben");
}
}
public void checkIfNewMaximumIsValid(Long newUserMaximum, String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId));
if (newUserMaximum == null) {

View File

@ -57,7 +57,7 @@
th:text="${parent.getTitle()}">Parent</span>
<div class="input-group mb-3" style="margin-top: 10px"
th:if="${group.getVisibility() == group.getVisibility().PRIVATE}">
th:if="${roles.get(user.getId()) == admin}">
<div class="input-group-prepend">
<span class="input-group-text" id="inputGroup-sizing-default"
style="background: #52a1eb">Einladungslink:</span>