1

Further further further refactor

Co-Authored-By: andibuls <andibuls@users.noreply.github.com>
Co-Authored-By: Lukas Ettel <lukasettel@users.noreply.github.com>
This commit is contained in:
XXNitram
2020-03-27 15:02:33 +01:00
parent 3fc4d82dbe
commit 289299f6e8
4 changed files with 107 additions and 87 deletions

View File

@ -281,17 +281,15 @@ public class WebController {
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
}
//Bis hier gekommen
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/leaveGroup")
public String pLeaveGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
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));
validationService.checkIfGroupEmpty(groupId, user);
User user = new User(account.getName(), "", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId));
controllerService.deleteUser(account, user, group);
return "redirect:/gruppen2/";
}
@ -302,7 +300,9 @@ 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.throwIfNoAdmin(group, user);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@ -315,7 +315,9 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId));
User user = new User(account.getName(), "", "", "");
validationService.throwIfNoAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
@ -330,9 +332,15 @@ public class WebController {
@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)) {
User principle = new User(account.getName(), "", "", "");
User user = new User(userId, "", "", "");
validationService.throwIfNoAdmin(group, principle);
controllerService.changeRole(account, user, group);
group = userService.getGroupById(UUID.fromString(groupId));
if (!validationService.checkIfAdmin(group, principle)) {
return "redirect:/gruppen2/details/" + groupId;
}
return "redirect:/gruppen2/details/members/" + groupId;
@ -344,7 +352,10 @@ public class WebController {
@RequestParam("group_id") String groupId,
KeycloakAuthenticationToken token) {
Account account = keyCloakService.createAccountFromPrincipal(token);
validationService.checkIfNewMaximumIsValid(maximum, groupId);
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNewMaximumIsValid(maximum, group);
controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -352,10 +363,20 @@ public class WebController {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/deleteUser")
public String deleteUser(@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException {
@RequestParam("user_id") String userId,
KeycloakAuthenticationToken token) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
User principle = new User(account.getName(), "", "", "");
User user = new User(userId, "", "", "");
controllerService.deleteUser(userId, UUID.fromString(groupId));
validationService.checkIfGroupEmpty(groupId, user);
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, principle);
controllerService.deleteUser(account, user, group);
if (!validationService.checkIfUserInGroup(group, principle)) {
return "redirect:/gruppen2/";
}
return "redirect:/gruppen2/details/members/" + groupId;
}

View File

@ -15,7 +15,6 @@ import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
import mops.gruppen2.domain.event.UpdateRoleEvent;
import mops.gruppen2.domain.event.UpdateUserMaxEvent;
import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.domain.exception.UserNotFoundException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
@ -25,7 +24,6 @@ import java.io.CharConversionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Logger;
import java.util.stream.Collectors;
@ -38,11 +36,13 @@ public class ControllerService {
private final EventService eventService;
private final UserService userService;
private final ValidationService validationService;
private final Logger logger;
public ControllerService(EventService eventService, UserService userService) {
public ControllerService(EventService eventService, UserService userService, ValidationService validationService) {
this.eventService = eventService;
this.userService = userService;
this.validationService = validationService;
this.logger = Logger.getLogger("controllerServiceLogger");
}
@ -66,10 +66,12 @@ public class ControllerService {
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, groupType, groupVisibility, userMaximum);
eventService.saveEvent(createGroupEvent);
User user = new User(account.getName(), "", "", "");
addUser(account, groupId);
updateTitle(account, groupId, title);
updateDescription(account, groupId, description);
updateRole(account.getName(), groupId);
updateRole(user, groupId);
return groupId;
}
@ -214,41 +216,32 @@ public class ControllerService {
eventService.saveEvent(updateUserMaxEvent);
}
public void updateRole(String userId, UUID groupId) throws EventException {
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = userService.getGroupById(groupId);
User user = null;
for (User member : group.getMembers()) {
if (member.getId().equals(userId)) {
user = member;
}
}
if (user == null) {
throw new UserNotFoundException(this.getClass().toString());
}
validationService.throwIfNotInGroup(group, user);
if (group.getRoles().get(user.getId()) == ADMIN) {
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), Role.MEMBER);
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), Role.MEMBER);
} else {
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), ADMIN);
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
}
eventService.saveEvent(updateRoleEvent);
}
public void deleteUser(String userId, UUID groupId) throws EventException {
Group group = userService.getGroupById(groupId);
User user = null;
for (User member : group.getMembers()) {
if (member.getId().equals(userId)) {
user = member;
}
}
public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group);
if (user == null) {
throw new UserNotFoundException(this.getClass().toString());
}
validationService.throwIfNotInGroup(group, user);
deleteUserEvent(user, group.getId());
if (validationService.checkIfGroupEmpty(group.getId())) {
deleteGroupEvent(user.getId(), group.getId());
}
}
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventService.saveEvent(deleteUserEvent);
}
@ -258,36 +251,39 @@ public class ControllerService {
eventService.saveEvent(deleteGroupEvent);
}
public boolean passIfLastAdmin(Account account, UUID groupId) {
Group group = userService.getGroupById(groupId);
private void changeRoleIfLastAdmin(Account account, Group group) {
if (group.getMembers().size() <= 1) {
return true;
return;
}
if (isLastAdmin(account, group)) {
String newAdminId = getVeteranMember(account, group);
updateRole(newAdminId, groupId);
}
return false;
promoteVeteranMember(account, group);
}
private boolean isLastAdmin(Account account, Group group) {
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
if (entry.getValue() == ADMIN) {
if (!(entry.getKey().equals(account.getName()))) {
return false;
}
private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) {
User newAdmin = getVeteranMember(account, group);
updateRole(newAdmin, group.getId());
}
}
public void changeRole(Account account, User user, Group group) {
if (user.getId().equals(account.getName())) {
if (group.getMembers().size() <= 1) {
validationService.throwIfLastAdmin(account, group);
}
promoteVeteranMember(account, group);
}
return true;
updateRole(user, group.getId());
}
private String getVeteranMember(Account account, Group group) {
List<User> mitglieder = group.getMembers();
if (mitglieder.get(0).getId().equals(account.getName())) {
return mitglieder.get(1).getId();
private User getVeteranMember(Account account, Group group) {
List<User> members = group.getMembers();
String newAdminId;
if (members.get(0).getId().equals(account.getName())) {
newAdminId = members.get(1).getId();
} else {
newAdminId = members.get(0).getId();
}
return mitglieder.get(0).getId();
return new User(newAdminId, "", "", "");
}
public UUID getUUID(String id) {

View File

@ -4,27 +4,23 @@ 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.BadParameterException;
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.UserAlreadyExistsException;
import mops.gruppen2.domain.exception.*;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static mops.gruppen2.domain.Role.ADMIN;
@Service
public class ValidationService {
private final ControllerService controllerService;
private final UserService userService;
private final GroupService groupService;
public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) {
this.controllerService = controllerService;
public ValidationService(UserService userService, GroupService groupService) {
this.userService = userService;
this.groupService = groupService;
}
@ -58,17 +54,20 @@ public class ValidationService {
}
}
public void throwIfNotInGroup(Group group, User user) {
if (!checkIfUserInGroup(group, user)) {
throw new UserNotFoundException(this.getClass().toString());
}
}
public void throwIfGroupFull(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 boolean checkIfGroupEmpty(UUID groupId) {
return userService.getGroupById(groupId).getMembers().isEmpty();
}
public void throwIfNoAdmin(Group group, User user) {
@ -85,16 +84,21 @@ public class ValidationService {
return false;
}
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");
public boolean checkIfLastAdmin(Account account, Group group) {
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
if (entry.getValue() == ADMIN) {
if (!(entry.getKey().equals(account.getName()))) {
return false;
}
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return true;
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return false;
return true;
}
public void throwIfLastAdmin(Account account, Group group) {
if (checkIfLastAdmin(account, group)) {
throw new NoAdminAfterActionException("Du Otto bist letzter Admin!");
}
}
/**
@ -134,8 +138,7 @@ public class ValidationService {
}
}
public void checkIfNewMaximumIsValid(Long newUserMaximum, String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId));
public void throwIfNewMaximumIsValid(Long newUserMaximum, Group group) {
if (newUserMaximum == null) {
throw new BadParameterException("Es wurde keine neue maximale Teilnehmeranzahl angegeben!");
}

View File

@ -30,7 +30,7 @@ class ControllerServiceTest {
eventService = new EventService(jsonService, eventRepository);
groupService = new GroupService(eventService, eventRepository);
userService = new UserService(groupService, eventService);
controllerService = new ControllerService(eventService, userService);
controllerService = new ControllerService(eventService, userService, validationService);
Set<String> roles = new HashSet<>();
roles.add("l");
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);