diff --git a/src/main/java/mops/gruppen2/controller/Gruppen2Controller.java b/src/main/java/mops/gruppen2/controller/Gruppen2Controller.java index c3a2eff..9416047 100644 --- a/src/main/java/mops/gruppen2/controller/Gruppen2Controller.java +++ b/src/main/java/mops/gruppen2/controller/Gruppen2Controller.java @@ -8,6 +8,7 @@ import mops.gruppen2.domain.User; import mops.gruppen2.domain.exception.EventException; import mops.gruppen2.domain.exception.GroupNotFoundException; import mops.gruppen2.domain.exception.WrongFileException; +import mops.gruppen2.domain.exception.NoAdminAfterActionException; import mops.gruppen2.security.Account; import mops.gruppen2.service.ControllerService; import mops.gruppen2.service.CsvService; @@ -158,6 +159,21 @@ public class Gruppen2Controller { return "search"; } + @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"}) + @PostMapping("/createGroup") + public String pCreateGroup(KeycloakAuthenticationToken token, + @RequestParam("title") String title, + @RequestParam("description") String description, + @RequestParam(value = "visibility", required = false) Boolean visibility, + @RequestParam("userMaximum") Long userMaximum) throws EventException { + + Account account = keyCloakService.createAccountFromPrincipal(token); + visibility = visibility == null; + controllerService.createGroup(account, title, description, visibility, userMaximum); + + return "redirect:/gruppen2/"; + } + @RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"}) @GetMapping("/details/{id}") public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @PathVariable("id") Long groupId) throws EventException { @@ -180,12 +196,14 @@ public class Gruppen2Controller { @PostMapping("/detailsBeitreten") public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException { model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); + Account account = keyCloakService.createAccountFromPrincipal(token); User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()); Group group = userService.getGroupById(groupId); if (group.getMembers().contains(user)) { return "error"; //hier soll eigentlich auf die bereits beigetretene Gruppe weitergeleitet werden } + if (group.getUserMaximum() < group.getMembers().size()) return "error"; controllerService.addUser(account, groupId); return "redirect:/gruppen2/"; } @@ -195,7 +213,7 @@ public class Gruppen2Controller { public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException { model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token)); Group group = userService.getGroupById(groupId); - if (group != null) { + if (group != null && group.getUserMaximum() > group.getMembers().size()) { model.addAttribute("group", group); return "detailsNoMember"; } @@ -219,6 +237,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/"; } @@ -243,6 +262,16 @@ 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); return "redirect:/gruppen2/details/members/" + groupId; } diff --git a/src/main/java/mops/gruppen2/domain/Group.java b/src/main/java/mops/gruppen2/domain/Group.java index 89acc65..71b2eeb 100644 --- a/src/main/java/mops/gruppen2/domain/Group.java +++ b/src/main/java/mops/gruppen2/domain/Group.java @@ -20,6 +20,7 @@ public class Group { private Long id; private String title; private String description; + private Long userMaximum; private GroupType type; private Visibility visibility; private Long parent; diff --git a/src/main/java/mops/gruppen2/domain/User.java b/src/main/java/mops/gruppen2/domain/User.java index bf6c172..7b961c4 100644 --- a/src/main/java/mops/gruppen2/domain/User.java +++ b/src/main/java/mops/gruppen2/domain/User.java @@ -11,8 +11,8 @@ import lombok.NoArgsConstructor; @EqualsAndHashCode(exclude = {"givenname", "familyname", "email"}) public class User { - private String id; - private String givenname; - private String familyname; - private String email; + private String id; + private String givenname; + private String familyname; + private String email; } diff --git a/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java b/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java index 09ca712..9491355 100644 --- a/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java +++ b/src/main/java/mops/gruppen2/domain/event/AddUserEvent.java @@ -7,6 +7,7 @@ import mops.gruppen2.domain.Group; import mops.gruppen2.domain.Role; import mops.gruppen2.domain.User; import mops.gruppen2.domain.exception.EventException; +import mops.gruppen2.domain.exception.GroupFullException; import mops.gruppen2.domain.exception.UserAlreadyExistsException; /** @@ -35,6 +36,10 @@ public class AddUserEvent extends Event { if (group.getMembers().contains(user)) { throw new UserAlreadyExistsException(this.getClass().toString()); } + //andere exception + if (group.getMembers().size() == group.getUserMaximum()){ + throw new GroupFullException(this.getClass().toString()); + } group.getMembers().add(user); group.getRoles().put(userId, Role.MEMBER); diff --git a/src/main/java/mops/gruppen2/domain/event/CreateGroupEvent.java b/src/main/java/mops/gruppen2/domain/event/CreateGroupEvent.java index ee750c9..f2e23f3 100644 --- a/src/main/java/mops/gruppen2/domain/event/CreateGroupEvent.java +++ b/src/main/java/mops/gruppen2/domain/event/CreateGroupEvent.java @@ -15,12 +15,14 @@ public class CreateGroupEvent extends Event { private Visibility groupVisibility; private Long groupParent; private GroupType groupType; + private Long groupUserMaximum; - public CreateGroupEvent(Long groupId, String userId, Long parent, GroupType type, Visibility visibility) { + public CreateGroupEvent(Long groupId, String userId, Long parent, GroupType type, Visibility visibility, Long userMaximum) { super(groupId, userId); this.groupParent = parent; this.groupType = type; this.groupVisibility = visibility; + this.groupUserMaximum = userMaximum; } @Override @@ -29,5 +31,6 @@ public class CreateGroupEvent extends Event { group.setParent(this.groupParent); group.setType(this.groupType); group.setVisibility(this.groupVisibility); + group.setUserMaximum(this.groupUserMaximum); } } diff --git a/src/main/java/mops/gruppen2/domain/exception/GroupFullException.java b/src/main/java/mops/gruppen2/domain/exception/GroupFullException.java new file mode 100644 index 0000000..1fb1509 --- /dev/null +++ b/src/main/java/mops/gruppen2/domain/exception/GroupFullException.java @@ -0,0 +1,11 @@ +package mops.gruppen2.domain.exception; + +import org.springframework.http.HttpStatus; + +public class GroupFullException extends EventException { + + public GroupFullException(String info) { + super(HttpStatus.INTERNAL_SERVER_ERROR, "Der User existiert bereits.", info); + } +} + diff --git a/src/main/java/mops/gruppen2/domain/exception/NoAdminAfterActionException.java b/src/main/java/mops/gruppen2/domain/exception/NoAdminAfterActionException.java new file mode 100644 index 0000000..f0b170b --- /dev/null +++ b/src/main/java/mops/gruppen2/domain/exception/NoAdminAfterActionException.java @@ -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); + } +} diff --git a/src/main/java/mops/gruppen2/service/ControllerService.java b/src/main/java/mops/gruppen2/service/ControllerService.java index 18fcd0f..b069319 100644 --- a/src/main/java/mops/gruppen2/service/ControllerService.java +++ b/src/main/java/mops/gruppen2/service/ControllerService.java @@ -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 { @@ -43,7 +46,7 @@ public class ControllerService { * @param title Gruppentitel * @param description Gruppenbeschreibung */ - public void createGroup(Account account, String title, String description, Boolean visibility) throws EventException { + public void createGroup(Account account, String title, String description, Boolean visibility, Long userMaximum) throws EventException { Visibility visibility1; Long groupId = eventService.checkGroup(); @@ -54,7 +57,7 @@ public class ControllerService { createInviteLink(groupId); } - CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), null, GroupType.SIMPLE, visibility1); + CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), null, GroupType.SIMPLE, visibility1, userMaximum); eventService.saveEvent(createGroupEvent); addUser(account, groupId); @@ -130,10 +133,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); } @@ -160,4 +163,56 @@ public class ControllerService { eventService.saveEvent(deleteGroupEvent); } + public void createLecture(Account account, String title, String description, Boolean visibility, List users) throws EventException { + Visibility visibility1; + Long groupId = eventService.checkGroup(); + + if (visibility) { + visibility1 = Visibility.PUBLIC; + } else { + visibility1 = Visibility.PRIVATE; + } + + CreateGroupEvent createGroupEvent = new CreateGroupEvent(groupId, account.getName(), null, GroupType.LECTURE, visibility1, 1000L); //this has to be changed also Usermaximum + eventService.saveEvent(createGroupEvent); + + addUser(account, groupId); + updateTitle(account, groupId, title); + updateDescription(account, groupId, description); + 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 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 mitglieder = group.getMembers(); + if (mitglieder.get(0).getId().equals(account.getName())){ + return mitglieder.get(1).getId(); + } + return mitglieder.get(0).getId(); + } + } diff --git a/src/main/java/mops/gruppen2/service/GroupService.java b/src/main/java/mops/gruppen2/service/GroupService.java index 3b3ff54..f7b5d62 100644 --- a/src/main/java/mops/gruppen2/service/GroupService.java +++ b/src/main/java/mops/gruppen2/service/GroupService.java @@ -1,6 +1,7 @@ package mops.gruppen2.service; import mops.gruppen2.domain.Group; +import mops.gruppen2.domain.User; import mops.gruppen2.domain.dto.EventDTO; import mops.gruppen2.domain.event.Event; import mops.gruppen2.domain.exception.EventException; @@ -66,11 +67,11 @@ public class GroupService { return groups.get(groupId); } - private List removeUserGroups(List groupIds, List userGroups) { - for (Long groupId : userGroups) { - groupIds.remove(groupId); + private List removeUserGroups(List visibleGroups, List userGroups) { + for (Group group : userGroups) { + visibleGroups.remove(group); } - return groupIds; + return visibleGroups; } /** @@ -81,10 +82,18 @@ public class GroupService { * @throws EventException Projektionsfehler */ public List getAllGroupWithVisibilityPublic(String userId) throws EventException { - List groupIds = removeUserGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE), eventRepository.findGroup_idsWhereUser_id(userId)); - List eventDTOS = eventRepository.findAllEventsOfGroups(groupIds); - List events = eventService.translateEventDTOs(eventDTOS); - return projectEventList(events); + User user = new User(userId,null, null, null); + List eventsVisible = eventService.translateEventDTOs(eventRepository.findAllEventsOfGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE))); + List visibleGroups = projectEventList(eventsVisible); + List eventsUser = getGroupEvents(eventRepository.findGroup_idsWhereUser_id(userId)); + List groups = projectEventList(eventsUser); + List newGroups = new ArrayList<>(); + for (Group group : groups) { + if (group.getMembers().contains(user)) { + newGroups.add(group); + } + } + return removeUserGroups(visibleGroups, newGroups); } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000..edf2d94 --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,6 @@ +insert into event values +(1,1,'orga','{"type":"CreateGroupEvent","groupId":1,"userId":"orga","groupVisibility":"PUBLIC","groupParent":null,"groupType":"SIMPLE","groupUserMaximum":2}', TRUE), +(2,1,'orga','{"type":"AddUserEvent","groupId":1,"userId":"orga","givenname":"orga","familyname":"orga","email":"blorga@orga.org"}', FALSE), +(3,1,'orga','{"type":"UpdateGroupTitleEvent","groupId":1,"userId":"orga","newGroupTitle":"sdsad"}', FALSE), +(4,1,'orga','{"type":"UpdateGroupDescriptionEvent","groupId":1,"userId":"orga","newGroupDescription":"sadsad"}', FALSE), +(5,1,'orga','{"type":"UpdateRoleEvent","groupId":1,"userId":"orga","newRole":"ADMIN"}', FALSE); \ No newline at end of file diff --git a/src/main/resources/templates/createStudent.html b/src/main/resources/templates/createStudent.html index 43b4abd..0ea0233 100644 --- a/src/main/resources/templates/createStudent.html +++ b/src/main/resources/templates/createStudent.html @@ -33,18 +33,23 @@

Gruppenerstellung

-
-
+ +
+
- - + + +
+
+ +
-
+

Mitglieder

+
+

+ + von maximal + + Benutzern. +

+
- +

diff --git a/src/main/resources/templates/detailsNoMember.html b/src/main/resources/templates/detailsNoMember.html index 20d4f74..ece315e 100644 --- a/src/main/resources/templates/detailsNoMember.html +++ b/src/main/resources/templates/detailsNoMember.html @@ -31,8 +31,7 @@
-
+

Mitglied Rolle - Optionen + Optionen - @@ -83,7 +82,7 @@ type="hidden"> - @@ -92,17 +91,16 @@ type="hidden"> - - +
+ +

diff --git a/src/main/resources/templates/error.html b/src/main/resources/templates/error.html index 5ea8655..87383ce 100644 --- a/src/main/resources/templates/error.html +++ b/src/main/resources/templates/error.html @@ -10,7 +10,7 @@ Seite nicht gefunden -
+

UPSI

@@ -31,4 +31,4 @@
- + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index 01edfac..36e7704 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -44,7 +44,7 @@


-
+

diff --git a/src/main/resources/templates/search.html b/src/main/resources/templates/search.html index 74dfe2b..ef093d7 100644 --- a/src/main/resources/templates/search.html +++ b/src/main/resources/templates/search.html @@ -33,7 +33,7 @@

Gruppensuche

-
+
eventList = new ArrayList<>(); - eventList.add(new CreateGroupEvent(1L, "Prof", null, GroupType.LECTURE, Visibility.PRIVATE)); + eventList.add(new CreateGroupEvent(1L, "Prof", null, GroupType.LECTURE, Visibility.PRIVATE,1000L)); eventList.add(new AddUserEvent(1L, "Ulli", "Ulli", "Honnis", "FC@B.de")); List groups = groupService.projectEventList(eventList);