add changeRole and refactor Event-Methods in ControllerService
Co-Authored-By: xxnitram <xxnitram@users.noreply.github.com> Co-Authored-By: tomvahl <tomvahl@users.noreply.github.com> Co-Authored-By: andibuls <andibuls@users.noreply.github.com> Co-Authored-By: Lukas Ettel <lukasettel@users.noreply.github.com>
This commit is contained in:
@ -7,6 +7,7 @@ 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.event.CreateGroupEvent;
|
import mops.gruppen2.domain.event.CreateGroupEvent;
|
||||||
|
import mops.gruppen2.domain.event.UpdateRoleEvent;
|
||||||
import mops.gruppen2.security.Account;
|
import mops.gruppen2.security.Account;
|
||||||
import mops.gruppen2.service.*;
|
import mops.gruppen2.service.*;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
@ -88,12 +89,12 @@ public class Gruppen2Controller {
|
|||||||
public String pCreateGroup(KeycloakAuthenticationToken token,
|
public String pCreateGroup(KeycloakAuthenticationToken token,
|
||||||
@RequestParam(value = "title") String title,
|
@RequestParam(value = "title") String title,
|
||||||
@RequestParam(value = "beschreibung") String beschreibung,
|
@RequestParam(value = "beschreibung") String beschreibung,
|
||||||
@RequestParam(value = "visibility", required = false) Boolean visibility) {
|
@RequestParam(value = "visibility", required = false) Boolean visibility) throws EventException {
|
||||||
|
|
||||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||||
if (visibility == null) {
|
if (visibility == null) {
|
||||||
visibility = true;
|
visibility = true;
|
||||||
}else{
|
} else {
|
||||||
visibility = false;
|
visibility = false;
|
||||||
}
|
}
|
||||||
controllerService.createGroup(account, title, beschreibung, visibility);
|
controllerService.createGroup(account, title, beschreibung, visibility);
|
||||||
@ -132,7 +133,8 @@ public class Gruppen2Controller {
|
|||||||
@PostMapping("/leaveGroup")
|
@PostMapping("/leaveGroup")
|
||||||
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam (value="group_id") Long id) {
|
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam (value="group_id") Long id) {
|
||||||
Account account = keyCloakService.createAccountFromPrincipal(token);
|
Account account = keyCloakService.createAccountFromPrincipal(token);
|
||||||
controllerService.deleteUser(account, id);
|
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
|
||||||
|
controllerService.deleteUser(user, id);
|
||||||
return "redirect:/gruppen2/";
|
return "redirect:/gruppen2/";
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,4 +152,12 @@ public class Gruppen2Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
|
||||||
|
@PostMapping("/changeRole")
|
||||||
|
public String changeRole(KeycloakAuthenticationToken token, @RequestParam (value = "group_id") Long id,
|
||||||
|
@RequestParam (value = "user") User user) throws EventException {
|
||||||
|
controllerService.updateRole(user, id);
|
||||||
|
return "redirect:/details/members/";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,21 +1,20 @@
|
|||||||
package mops.gruppen2.service;
|
package mops.gruppen2.service;
|
||||||
|
|
||||||
import mops.gruppen2.domain.Group;
|
import mops.gruppen2.domain.*;
|
||||||
import mops.gruppen2.domain.GroupType;
|
import mops.gruppen2.domain.Exceptions.EventException;
|
||||||
import mops.gruppen2.domain.Role;
|
|
||||||
import mops.gruppen2.domain.Visibility;
|
|
||||||
import mops.gruppen2.domain.event.*;
|
import mops.gruppen2.domain.event.*;
|
||||||
import mops.gruppen2.security.Account;
|
import mops.gruppen2.security.Account;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class ControllerService {
|
public class ControllerService {
|
||||||
|
|
||||||
private final EventService eventService;
|
private final EventService eventService;
|
||||||
|
private final UserService userService;
|
||||||
|
|
||||||
public ControllerService(EventService eventService) {
|
public ControllerService(EventService eventService, UserService userService) {
|
||||||
this.eventService = eventService;
|
this.eventService = eventService;
|
||||||
|
this.userService = userService;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,23 +26,24 @@ public class ControllerService {
|
|||||||
* @param title Gruppentitel
|
* @param title Gruppentitel
|
||||||
* @param description Gruppenbeschreibung
|
* @param description Gruppenbeschreibung
|
||||||
*/
|
*/
|
||||||
public void createGroup(Account account, String title, String description, Boolean visibility) {
|
public void createGroup(Account account, String title, String description, Boolean visibility) throws EventException {
|
||||||
Visibility visibility1;
|
Visibility visibility1;
|
||||||
Long group_id = eventService.checkGroup();
|
Long group_id = eventService.checkGroup();
|
||||||
|
|
||||||
if (visibility){
|
if(visibility) {
|
||||||
visibility1 = Visibility.PUBLIC;
|
visibility1 = Visibility.PUBLIC;
|
||||||
}else{
|
} else {
|
||||||
visibility1 = Visibility.PRIVATE;
|
visibility1 = Visibility.PRIVATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
CreateGroupEvent createGroupEvent = new CreateGroupEvent(group_id, account.getName(), null , GroupType.LECTURE, visibility1);
|
CreateGroupEvent createGroupEvent = new CreateGroupEvent(group_id, account.getName(), null , GroupType.LECTURE, visibility1);
|
||||||
eventService.saveEvent(createGroupEvent);
|
eventService.saveEvent(createGroupEvent);
|
||||||
|
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
|
||||||
|
|
||||||
addUser(account, group_id);
|
addUser(account, group_id);
|
||||||
updateTitle(account, group_id, title);
|
updateTitle(account, group_id, title);
|
||||||
updateDescription(account, group_id, description);
|
updateDescription(account, group_id, description);
|
||||||
updateRole(account, group_id);
|
updateRole(user, group_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addUser(Account account, Long group_id){
|
public void addUser(Account account, Long group_id){
|
||||||
@ -61,13 +61,24 @@ public class ControllerService {
|
|||||||
eventService.saveEvent(updateGroupDescriptionEvent);
|
eventService.saveEvent(updateGroupDescriptionEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateRole(Account account,Long group_id){
|
public void updateRole(User user, Long group_id) throws EventException {
|
||||||
UpdateRoleEvent updateRoleEvent = new UpdateRoleEvent(group_id,account.getName(),Role.ADMIN);
|
UpdateRoleEvent updateRoleEvent;
|
||||||
|
Group group = userService.getGroupById(group_id);
|
||||||
|
if(group.getRoles().get(user.getUser_id()) == Role.ADMIN) {
|
||||||
|
updateRoleEvent = new UpdateRoleEvent(group_id, user.getUser_id(), Role.MEMBER);
|
||||||
|
} else {
|
||||||
|
updateRoleEvent = new UpdateRoleEvent(group_id, user.getUser_id(), Role.ADMIN);
|
||||||
|
}
|
||||||
eventService.saveEvent(updateRoleEvent);
|
eventService.saveEvent(updateRoleEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteUser(Account account, Long group_id){
|
public void deleteUser(User user, Long group_id){
|
||||||
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(group_id,account.getName());
|
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(group_id, user.getUser_id());
|
||||||
eventService.saveEvent(deleteUserEvent);
|
eventService.saveEvent(deleteUserEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteGroupEvent(User user, Long group_id) {
|
||||||
|
DeleteGroupEvent deleteGroupEvent = new DeleteGroupEvent(group_id, user.getUser_id());
|
||||||
|
eventService.saveEvent(deleteGroupEvent);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user