1

Merge remote-tracking branch 'origin/master' into csv-error-handling

# Conflicts:
#	src/main/java/mops/gruppen2/controller/Gruppen2Controller.java
#	src/main/java/mops/gruppen2/domain/User.java
#	src/main/java/mops/gruppen2/service/ControllerService.java
#	src/main/resources/templates/createStudent.html
This commit is contained in:
XXNitram
2020-03-19 17:01:32 +01:00
21 changed files with 185 additions and 48 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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<User> 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<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();
}
}

View File

@ -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<Long> removeUserGroups(List<Long> groupIds, List<Long> userGroups) {
for (Long groupId : userGroups) {
groupIds.remove(groupId);
private List<Group> removeUserGroups(List<Group> visibleGroups, List<Group> userGroups) {
for (Group group : userGroups) {
visibleGroups.remove(group);
}
return groupIds;
return visibleGroups;
}
/**
@ -81,10 +82,18 @@ public class GroupService {
* @throws EventException Projektionsfehler
*/
public List<Group> getAllGroupWithVisibilityPublic(String userId) throws EventException {
List<Long> groupIds = removeUserGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE), eventRepository.findGroup_idsWhereUser_id(userId));
List<EventDTO> eventDTOS = eventRepository.findAllEventsOfGroups(groupIds);
List<Event> events = eventService.translateEventDTOs(eventDTOS);
return projectEventList(events);
User user = new User(userId,null, null, null);
List<Event> eventsVisible = eventService.translateEventDTOs(eventRepository.findAllEventsOfGroups(eventRepository.findGroup_idsWhereVisibility(Boolean.TRUE)));
List<Group> visibleGroups = projectEventList(eventsVisible);
List<Event> eventsUser = getGroupEvents(eventRepository.findGroup_idsWhereUser_id(userId));
List<Group> groups = projectEventList(eventsUser);
List<Group> newGroups = new ArrayList<>();
for (Group group : groups) {
if (group.getMembers().contains(user)) {
newGroups.add(group);
}
}
return removeUserGroups(visibleGroups, newGroups);
}