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:
@ -281,17 +281,15 @@ public class WebController {
|
|||||||
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
|
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bis hier gekommen
|
|
||||||
|
|
||||||
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
|
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
|
||||||
@PostMapping("/leaveGroup")
|
@PostMapping("/leaveGroup")
|
||||||
public String pLeaveGroup(KeycloakAuthenticationToken token,
|
public String pLeaveGroup(KeycloakAuthenticationToken token,
|
||||||
@RequestParam("group_id") String groupId) throws EventException {
|
@RequestParam("group_id") String groupId) throws EventException {
|
||||||
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(), "", "", "");
|
||||||
controllerService.passIfLastAdmin(account, UUID.fromString(groupId));
|
Group group = userService.getGroupById(UUID.fromString(groupId));
|
||||||
controllerService.deleteUser(user.getId(), UUID.fromString(groupId));
|
|
||||||
validationService.checkIfGroupEmpty(groupId, user);
|
controllerService.deleteUser(account, user, group);
|
||||||
return "redirect:/gruppen2/";
|
return "redirect:/gruppen2/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,7 +300,9 @@ public class WebController {
|
|||||||
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));
|
||||||
|
|
||||||
validationService.throwIfNoAdmin(group, user);
|
validationService.throwIfNoAdmin(group, user);
|
||||||
|
|
||||||
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
|
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
|
||||||
return "redirect:/gruppen2/";
|
return "redirect:/gruppen2/";
|
||||||
}
|
}
|
||||||
@ -315,7 +315,9 @@ public class WebController {
|
|||||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||||
Group group = userService.getGroupById(UUID.fromString(groupId));
|
Group group = userService.getGroupById(UUID.fromString(groupId));
|
||||||
User user = new User(account.getName(), "", "", "");
|
User user = new User(account.getName(), "", "", "");
|
||||||
|
|
||||||
validationService.throwIfNoAdmin(group, user);
|
validationService.throwIfNoAdmin(group, user);
|
||||||
|
|
||||||
model.addAttribute("account", account);
|
model.addAttribute("account", account);
|
||||||
model.addAttribute("members", group.getMembers());
|
model.addAttribute("members", group.getMembers());
|
||||||
model.addAttribute("group", group);
|
model.addAttribute("group", group);
|
||||||
@ -330,9 +332,15 @@ public class WebController {
|
|||||||
@RequestParam("user_id") String userId) throws EventException {
|
@RequestParam("user_id") String userId) throws EventException {
|
||||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||||
Group group = userService.getGroupById(UUID.fromString(groupId));
|
Group group = userService.getGroupById(UUID.fromString(groupId));
|
||||||
User user = new User(account.getName(), "", "", "");
|
User principle = new User(account.getName(), "", "", "");
|
||||||
validationService.throwIfNoAdmin(group, user);
|
User user = new User(userId, "", "", "");
|
||||||
if (validationService.checkIfDemotingSelf(userId, groupId, account)) {
|
|
||||||
|
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/" + groupId;
|
||||||
}
|
}
|
||||||
return "redirect:/gruppen2/details/members/" + groupId;
|
return "redirect:/gruppen2/details/members/" + groupId;
|
||||||
@ -344,7 +352,10 @@ public class WebController {
|
|||||||
@RequestParam("group_id") String groupId,
|
@RequestParam("group_id") String groupId,
|
||||||
KeycloakAuthenticationToken token) {
|
KeycloakAuthenticationToken token) {
|
||||||
Account account = keyCloakService.createAccountFromPrincipal(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);
|
controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum);
|
||||||
return "redirect:/gruppen2/details/members/" + groupId;
|
return "redirect:/gruppen2/details/members/" + groupId;
|
||||||
}
|
}
|
||||||
@ -352,10 +363,20 @@ public class WebController {
|
|||||||
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
|
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
|
||||||
@PostMapping("/details/members/deleteUser")
|
@PostMapping("/details/members/deleteUser")
|
||||||
public String deleteUser(@RequestParam("group_id") String groupId,
|
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, "", "", "");
|
User user = new User(userId, "", "", "");
|
||||||
controllerService.deleteUser(userId, UUID.fromString(groupId));
|
Group group = userService.getGroupById(UUID.fromString(groupId));
|
||||||
validationService.checkIfGroupEmpty(groupId, user);
|
|
||||||
|
validationService.throwIfNoAdmin(group, principle);
|
||||||
|
|
||||||
|
controllerService.deleteUser(account, user, group);
|
||||||
|
|
||||||
|
if (!validationService.checkIfUserInGroup(group, principle)) {
|
||||||
|
return "redirect:/gruppen2/";
|
||||||
|
}
|
||||||
return "redirect:/gruppen2/details/members/" + groupId;
|
return "redirect:/gruppen2/details/members/" + groupId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -15,7 +15,6 @@ import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
|
|||||||
import mops.gruppen2.domain.event.UpdateRoleEvent;
|
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.WrongFileException;
|
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;
|
||||||
@ -25,7 +24,6 @@ import java.io.CharConversionException;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -38,11 +36,13 @@ public class ControllerService {
|
|||||||
|
|
||||||
private final EventService eventService;
|
private final EventService eventService;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
|
private final ValidationService validationService;
|
||||||
private final Logger logger;
|
private final Logger logger;
|
||||||
|
|
||||||
public ControllerService(EventService eventService, UserService userService) {
|
public ControllerService(EventService eventService, UserService userService, ValidationService validationService) {
|
||||||
this.eventService = eventService;
|
this.eventService = eventService;
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
|
this.validationService = validationService;
|
||||||
this.logger = Logger.getLogger("controllerServiceLogger");
|
this.logger = Logger.getLogger("controllerServiceLogger");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,10 +66,12 @@ public class ControllerService {
|
|||||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, groupType, groupVisibility, userMaximum);
|
CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), parent, groupType, groupVisibility, userMaximum);
|
||||||
eventService.saveEvent(createGroupEvent);
|
eventService.saveEvent(createGroupEvent);
|
||||||
|
|
||||||
|
User user = new User(account.getName(), "", "", "");
|
||||||
|
|
||||||
addUser(account, groupId);
|
addUser(account, groupId);
|
||||||
updateTitle(account, groupId, title);
|
updateTitle(account, groupId, title);
|
||||||
updateDescription(account, groupId, description);
|
updateDescription(account, groupId, description);
|
||||||
updateRole(account.getName(), groupId);
|
updateRole(user, groupId);
|
||||||
|
|
||||||
return groupId;
|
return groupId;
|
||||||
}
|
}
|
||||||
@ -214,41 +216,32 @@ public class ControllerService {
|
|||||||
eventService.saveEvent(updateUserMaxEvent);
|
eventService.saveEvent(updateUserMaxEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateRole(String userId, UUID groupId) throws EventException {
|
public void updateRole(User user, UUID groupId) throws EventException {
|
||||||
UpdateRoleEvent updateRoleEvent;
|
UpdateRoleEvent updateRoleEvent;
|
||||||
Group group = userService.getGroupById(groupId);
|
Group group = userService.getGroupById(groupId);
|
||||||
User user = null;
|
validationService.throwIfNotInGroup(group, user);
|
||||||
for (User member : group.getMembers()) {
|
|
||||||
if (member.getId().equals(userId)) {
|
|
||||||
user = member;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user == null) {
|
|
||||||
throw new UserNotFoundException(this.getClass().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (group.getRoles().get(user.getId()) == ADMIN) {
|
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 {
|
} else {
|
||||||
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), ADMIN);
|
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
|
||||||
}
|
}
|
||||||
eventService.saveEvent(updateRoleEvent);
|
eventService.saveEvent(updateRoleEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(String userId, UUID groupId) throws EventException {
|
public void deleteUser(Account account, User user, Group group) throws EventException {
|
||||||
Group group = userService.getGroupById(groupId);
|
changeRoleIfLastAdmin(account, group);
|
||||||
User user = null;
|
|
||||||
for (User member : group.getMembers()) {
|
|
||||||
if (member.getId().equals(userId)) {
|
|
||||||
user = member;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (user == null) {
|
validationService.throwIfNotInGroup(group, user);
|
||||||
throw new UserNotFoundException(this.getClass().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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());
|
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
|
||||||
eventService.saveEvent(deleteUserEvent);
|
eventService.saveEvent(deleteUserEvent);
|
||||||
}
|
}
|
||||||
@ -258,36 +251,39 @@ public class ControllerService {
|
|||||||
eventService.saveEvent(deleteGroupEvent);
|
eventService.saveEvent(deleteGroupEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean passIfLastAdmin(Account account, UUID groupId) {
|
private void changeRoleIfLastAdmin(Account account, Group group) {
|
||||||
Group group = userService.getGroupById(groupId);
|
|
||||||
if (group.getMembers().size() <= 1) {
|
if (group.getMembers().size() <= 1) {
|
||||||
return true;
|
return;
|
||||||
}
|
}
|
||||||
|
promoteVeteranMember(account, group);
|
||||||
if (isLastAdmin(account, group)) {
|
|
||||||
String newAdminId = getVeteranMember(account, group);
|
|
||||||
updateRole(newAdminId, groupId);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isLastAdmin(Account account, Group group) {
|
private void promoteVeteranMember(Account account, Group group) {
|
||||||
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
|
if (validationService.checkIfLastAdmin(account, group)) {
|
||||||
if (entry.getValue() == ADMIN) {
|
User newAdmin = getVeteranMember(account, group);
|
||||||
if (!(entry.getKey().equals(account.getName()))) {
|
updateRole(newAdmin, group.getId());
|
||||||
return false;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
private User getVeteranMember(Account account, Group group) {
|
||||||
List<User> mitglieder = group.getMembers();
|
List<User> members = group.getMembers();
|
||||||
if (mitglieder.get(0).getId().equals(account.getName())) {
|
String newAdminId;
|
||||||
return mitglieder.get(1).getId();
|
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) {
|
public UUID getUUID(String id) {
|
||||||
|
|||||||
@ -4,27 +4,23 @@ 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.BadParameterException;
|
import mops.gruppen2.domain.exception.*;
|
||||||
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.security.Account;
|
import mops.gruppen2.security.Account;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static mops.gruppen2.domain.Role.ADMIN;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ValidationService {
|
public class ValidationService {
|
||||||
|
|
||||||
private final ControllerService controllerService;
|
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final GroupService groupService;
|
private final GroupService groupService;
|
||||||
|
|
||||||
public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) {
|
public ValidationService(UserService userService, GroupService groupService) {
|
||||||
this.controllerService = controllerService;
|
|
||||||
this.userService = userService;
|
this.userService = userService;
|
||||||
this.groupService = groupService;
|
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) {
|
public void throwIfGroupFull(Group group) {
|
||||||
if (group.getUserMaximum() < group.getMembers().size() + 1) {
|
if (group.getUserMaximum() < group.getMembers().size() + 1) {
|
||||||
throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten.");
|
throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean checkIfGroupEmpty(UUID groupId) {
|
||||||
public void checkIfGroupEmpty(String groupId, User user) {
|
return userService.getGroupById(groupId).getMembers().isEmpty();
|
||||||
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
|
|
||||||
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void throwIfNoAdmin(Group group, User user) {
|
public void throwIfNoAdmin(Group group, User user) {
|
||||||
@ -85,16 +84,21 @@ public class ValidationService {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean checkIfDemotingSelf(String userId, String groupId, Account account) {
|
public boolean checkIfLastAdmin(Account account, Group group) {
|
||||||
if (userId.equals(account.getName())) {
|
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
|
||||||
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
|
if (entry.getValue() == ADMIN) {
|
||||||
throw new NoAdminAfterActionException("Du Otto bist letzter Admin");
|
if (!(entry.getKey().equals(account.getName()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
controllerService.updateRole(userId, UUID.fromString(groupId));
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
controllerService.updateRole(userId, UUID.fromString(groupId));
|
return true;
|
||||||
return false;
|
}
|
||||||
|
|
||||||
|
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) {
|
public void throwIfNewMaximumIsValid(Long newUserMaximum, Group group) {
|
||||||
Group group = userService.getGroupById(UUID.fromString(groupId));
|
|
||||||
if (newUserMaximum == null) {
|
if (newUserMaximum == null) {
|
||||||
throw new BadParameterException("Es wurde keine neue maximale Teilnehmeranzahl angegeben!");
|
throw new BadParameterException("Es wurde keine neue maximale Teilnehmeranzahl angegeben!");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,7 +30,7 @@ class ControllerServiceTest {
|
|||||||
eventService = new EventService(jsonService, eventRepository);
|
eventService = new EventService(jsonService, eventRepository);
|
||||||
groupService = new GroupService(eventService, eventRepository);
|
groupService = new GroupService(eventService, eventRepository);
|
||||||
userService = new UserService(groupService, eventService);
|
userService = new UserService(groupService, eventService);
|
||||||
controllerService = new ControllerService(eventService, userService);
|
controllerService = new ControllerService(eventService, userService, validationService);
|
||||||
Set<String> roles = new HashSet<>();
|
Set<String> roles = new HashSet<>();
|
||||||
roles.add("l");
|
roles.add("l");
|
||||||
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);
|
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);
|
||||||
|
|||||||
Reference in New Issue
Block a user