Merge pull request #107 from hhu-propra2/entadminisierung
Entadminisierung
This commit is contained in:
@ -6,6 +6,7 @@ import mops.gruppen2.domain.Role;
|
||||
import mops.gruppen2.domain.User;
|
||||
import mops.gruppen2.domain.exception.EventException;
|
||||
import mops.gruppen2.domain.exception.GroupNotFoundException;
|
||||
import mops.gruppen2.domain.exception.NoAdminAfterActionException;
|
||||
import mops.gruppen2.security.Account;
|
||||
import mops.gruppen2.service.ControllerService;
|
||||
import mops.gruppen2.service.CsvService;
|
||||
@ -205,6 +206,7 @@ public class Gruppen2Controller {
|
||||
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId) throws EventException {
|
||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
|
||||
controllerService.passIfLastAdmin(account, groupId);
|
||||
controllerService.deleteUser(user.getId(), groupId);
|
||||
return "redirect:/gruppen2/";
|
||||
}
|
||||
@ -229,15 +231,22 @@ public class Gruppen2Controller {
|
||||
@PostMapping("/details/members/changeRole")
|
||||
public String changeRole(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId,
|
||||
@RequestParam("user_id") String userId) throws EventException {
|
||||
|
||||
|
||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||
if (userId.equals(account.getName())) {
|
||||
if (controllerService.passIfLastAdmin(account, groupId)){
|
||||
throw new NoAdminAfterActionException("Du otto bist letzter Admin");
|
||||
}
|
||||
controllerService.updateRole(userId, groupId);
|
||||
return "redirect:/gruppen2/details/" + groupId;
|
||||
}
|
||||
controllerService.updateRole(userId, groupId);
|
||||
if(userId.equals(account.getName())) return "redirect:/gruppen2/details/" + groupId;
|
||||
return "redirect:/gruppen2/details/members/" + groupId;
|
||||
}
|
||||
|
||||
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
|
||||
@PostMapping("/details/members/deleteUser")
|
||||
|
||||
public String deleteUser(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId,
|
||||
@RequestParam("user_id") String userId) throws EventException {
|
||||
controllerService.deleteUser(userId, groupId);
|
||||
|
||||
@ -0,0 +1,10 @@
|
||||
package mops.gruppen2.domain.exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
public class NoAdminAfterActionException extends EventException {
|
||||
|
||||
public NoAdminAfterActionException(String info) {
|
||||
super(HttpStatus.INTERNAL_SERVER_ERROR, "Nach dieser Aktion hätte die Gruppe keinen Admin mehr", info);
|
||||
}
|
||||
}
|
||||
@ -18,8 +18,11 @@ 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 ControllerService {
|
||||
@ -104,10 +107,10 @@ public class ControllerService {
|
||||
throw new UserNotFoundException(this.getClass().toString());
|
||||
}
|
||||
|
||||
if (group.getRoles().get(user.getId()) == Role.ADMIN) {
|
||||
if (group.getRoles().get(user.getId()) == ADMIN) {
|
||||
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), Role.MEMBER);
|
||||
} else {
|
||||
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), Role.ADMIN);
|
||||
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), ADMIN);
|
||||
}
|
||||
eventService.saveEvent(updateRoleEvent);
|
||||
}
|
||||
@ -153,4 +156,37 @@ public class ControllerService {
|
||||
updateRole(account.getName(), groupId);
|
||||
addUserList(users, groupId);
|
||||
}
|
||||
|
||||
public boolean passIfLastAdmin(Account account, Long groupId){
|
||||
Group group = userService.getGroupById(groupId);
|
||||
if (group.getMembers().size() <= 1){
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isLastAdmin(account, group)){
|
||||
String newAdminId = getVeteranMember(account, group);
|
||||
updateRole(newAdminId, groupId);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isLastAdmin(Account account, Group group){
|
||||
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()){
|
||||
if (entry.getValue().equals(ADMIN)){
|
||||
if (!(entry.getKey().equals(account.getName()))){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
return mitglieder.get(0).getId();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user