1

Merge remote-tracking branch 'origin/master' into feature-change-title-description

This commit is contained in:
XXNitram
2020-03-24 15:09:50 +01:00
30 changed files with 256 additions and 438 deletions

View File

@ -10,13 +10,14 @@ import mops.gruppen2.domain.exception.EventException;
import mops.gruppen2.service.APIFormatterService;
import mops.gruppen2.service.EventService;
import mops.gruppen2.service.GroupService;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* Ein Beispiel für eine API mit Swagger.
@ -34,7 +35,7 @@ public class APIController {
}
@GetMapping("/updateGroups/{status}")
@Secured("ROLE_api_user")
//@Secured("ROLE_api_user")
@ApiOperation("Gibt alle Gruppen zurück in denen sich etwas geändert hat")
public GroupRequestWrapper updateGroup(@ApiParam("Letzter Status des Anfragestellers") @PathVariable Long status) throws EventException {
List<Event> events = eventService.getNewEvents(status);
@ -43,17 +44,19 @@ public class APIController {
}
@GetMapping("/getGroupIdsOfUser/{teilnehmer}")
@Secured("ROLE_api_user")
//@Secured("ROLE_api_user")
@ApiOperation("Gibt alle Gruppen zurück in denen sich ein Teilnehmer befindet")
public List<Long> getGroupsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String teilnehmer) {
return eventService.getGroupsOfUser(teilnehmer);
public List<String> getGroupsOfUser(@ApiParam("Teilnehmer dessen groupIds zurückgegeben werden sollen") @PathVariable String teilnehmer) {
return eventService.findGroupIdsByUser(teilnehmer).stream()
.map(UUID::toString)
.collect(Collectors.toList());
}
@GetMapping("/getGroup/{groupId}")
@Secured("ROLE_api_user")
//@Secured("ROLE_api_user")
@ApiOperation("Gibt die Gruppe mit der als Parameter mitgegebenden groupId zurück")
public Group getGroupFromId(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable Long groupId) throws EventException {
List<Event> eventList = eventService.getEventsOfGroup(groupId);
public Group getGroupFromId(@ApiParam("GruppenId der gefordeten Gruppe") @PathVariable String groupId) throws EventException {
List<Event> eventList = eventService.getEventsOfGroup(UUID.fromString(groupId));
List<Group> groups = groupService.projectEventList(eventList);
return groups.get(0);

View File

@ -9,17 +9,17 @@ import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.event.UpdateGroupTitleEvent;
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.domain.exception.WrongFileException;
import mops.gruppen2.security.Account;
import mops.gruppen2.service.ControllerService;
import mops.gruppen2.service.CsvService;
import mops.gruppen2.service.GroupService;
import mops.gruppen2.service.InviteLinkRepositoryService;
import mops.gruppen2.service.KeyCloakService;
import mops.gruppen2.service.UserService;
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@ -29,12 +29,13 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.annotation.SessionScope;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.security.RolesAllowed;
import java.io.CharConversionException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.UUID;
@Controller
@SessionScope
@ -45,17 +46,15 @@ public class Gruppen2Controller {
private final GroupService groupService;
private final UserService userService;
private final ControllerService controllerService;
private final InviteLinkRepositoryService inviteLinkRepositoryService;
private final Gruppen2Config gruppen2Config;
private final Logger logger;
private final Logger logger = LoggerFactory.getLogger("Gruppen2ControllerLogger");
;
public Gruppen2Controller(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, InviteLinkRepositoryService inviteLinkRepositoryService, Gruppen2Config gruppen2Config) {
public Gruppen2Controller(KeyCloakService keyCloakService, GroupService groupService, UserService userService, ControllerService controllerService, Gruppen2Config gruppen2Config) {
this.keyCloakService = keyCloakService;
this.groupService = groupService;
this.userService = userService;
this.controllerService = controllerService;
this.inviteLinkRepositoryService = inviteLinkRepositoryService;
logger = Logger.getLogger("Gruppen2ControllerLogger");
this.gruppen2Config = gruppen2Config;
}
@ -93,24 +92,24 @@ public class Gruppen2Controller {
@RequestParam("description") String description,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "lecture", required = false) Boolean lecture,
@RequestParam(value = "userMaximum", required = false) Long userMaximum,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam(value = "parent", required = false) Long parent,
@RequestParam(value = "parent", required = false) String parent,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException, EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
if(userMaximum == null){
if (userMaximum == null) {
userMaximum = 100000L;
}
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if(userList.size() > userMaximum){
userMaximum = Long.valueOf(userList.size()) + userMaximum;
if (userList.size() > userMaximum) {
userMaximum = (long) userList.size() + userMaximum;
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
logger.warning("File konnte nicht gelesen werden");
logger.warn("File konnte nicht gelesen werden");
throw new WrongFileException(file.getOriginalFilename());
}
}
@ -118,9 +117,9 @@ public class Gruppen2Controller {
lecture = lecture != null;
maxInfiniteUsers = maxInfiniteUsers != null;
if (lecture) parent = null;
UUID parentUUID = controllerService.getUUID(parent);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parent, userList);
controllerService.createOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, userList);
return "redirect:/gruppen2/";
}
@ -140,14 +139,15 @@ public class Gruppen2Controller {
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam(value = "visibility", required = false) Boolean visibility,
@RequestParam(value = "userMaximum", required = false) Long userMaximum,
@RequestParam("userMaximum") Long userMaximum,
@RequestParam(value = "maxInfiniteUsers", required = false) Boolean maxInfiniteUsers,
@RequestParam(value = "parent", required = false) Long parent) throws EventException {
@RequestParam(value = "parent", required = false) String parent) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
visibility = visibility == null;
maxInfiniteUsers = maxInfiniteUsers != null;
controllerService.createGroup(account, title, description, visibility, maxInfiniteUsers, userMaximum, parent);
UUID parentUUID = controllerService.getUUID(parent);
controllerService.createGroup(account, title, description, visibility, maxInfiniteUsers, userMaximum, parentUUID);
return "redirect:/gruppen2/";
}
@ -155,22 +155,25 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_actuator)"})
@PostMapping("/details/members/addUsersFromCsv")
public String addUsersFromCsv(KeycloakAuthenticationToken token,
@RequestParam("group_id") Long groupId,
@RequestParam("group_id") String groupId,
@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
Group group = userService.getGroupById(groupId);
Group group = userService.getGroupById(UUID.fromString(groupId));
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if(userList.size()+group.getMembers().size()>group.getUserMaximum()){
controllerService.updateMaxUser(account, groupId, Long.valueOf(userList.size()) + group.getMembers().size());
if (userList.size() + group.getMembers().size() > group.getUserMaximum()) {
controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size());
}
} catch (UnrecognizedPropertyException | CharConversionException ex) {
throw new WrongFileException(file.getOriginalFilename());
}
}
controllerService.addUserList(userList, groupId);
UUID groupUUID = controllerService.getUUID(groupId);
controllerService.addUserList(userList, groupUUID);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -217,7 +220,9 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/findGroup")
public String findGroup(KeycloakAuthenticationToken token, Model model, @RequestParam(value = "suchbegriff", required = false) String search) throws EventException {
public String findGroup(KeycloakAuthenticationToken token,
Model model,
@RequestParam(value = "suchbegriff", required = false) String search) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<Group> groupse = new ArrayList<>();
if (search != null) {
@ -230,74 +235,82 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details/{id}")
public String showGroupDetails(KeycloakAuthenticationToken token, Model model, @PathVariable("id") Long groupId) throws EventException {
public String showGroupDetails(KeycloakAuthenticationToken token,
Model model,
@PathVariable("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(groupId);
Group group = userService.getGroupById(UUID.fromString(groupId));
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Long parentId = group.getParent();
UUID parentId = group.getParent();
Group parent = new Group();
if(group.getTitle() == null){
if (group.getTitle() == null) {
throw new GroupNotFoundException(this.getClass().toString());
}
if (!group.getMembers().contains(user)){
if (group.getVisibility() == Visibility.PRIVATE){
if (!group.getMembers().contains(user)) {
if (group.getVisibility() == Visibility.PRIVATE) {
return "privateGroupNoMember";
}
if (group != null) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
return "detailsNoMember";
}
if (parentId != null) {
parent = userService.getGroupById(parentId);
}
if (group != null) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
model.addAttribute("group", group);
model.addAttribute("roles", group.getRoles());
model.addAttribute("user", user);
model.addAttribute("admin", Role.ADMIN);
return "detailsMember";
return "detailsNoMember";
}
throw new GroupNotFoundException(this.getClass().toString());
if (!controllerService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
model.addAttribute("group", group);
model.addAttribute("roles", group.getRoles());
model.addAttribute("user", user);
model.addAttribute("admin", Role.ADMIN);
return "detailsMember";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/detailsBeitreten")
public String joinGroup(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException {
public String joinGroup(KeycloakAuthenticationToken token,
Model model, @RequestParam("id") String 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);
Group group = userService.getGroupById(UUID.fromString(groupId));
if (group.getMembers().contains(user)) {
return "error"; //hier soll eigentlich auf die bereits beigetretene Gruppe weitergeleitet werden
return "error"; //TODO: hier soll eigentlich auf die bereits beigetretene Gruppe weitergeleitet werden
}
if (group.getUserMaximum() < group.getMembers().size()) return "error";
controllerService.addUser(account, groupId);
if (group.getUserMaximum() < group.getMembers().size()) {
return "error";
}
controllerService.addUser(account, UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/detailsSearch")
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token, Model model, @RequestParam("id") Long groupId) throws EventException {
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token,
Model model,
@RequestParam("id") String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(groupId);
Long parentId = group.getParent();
Group group = userService.getGroupById(UUID.fromString(groupId));
UUID parentId = group.getParent();
Group parent = new Group();
if (parentId != null) {
parent = userService.getGroupById(parentId);
}
if (group != null && group.getUserMaximum() > group.getMembers().size()) {
if (group.getUserMaximum() > group.getMembers().size()) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
throw new GroupNotFoundException(this.getClass().toString());
@ -305,9 +318,10 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@GetMapping("/acceptinvite/{link}")
public String acceptInvite(KeycloakAuthenticationToken token, Model model, @PathVariable String link) throws EventException {
public String acceptInvite(KeycloakAuthenticationToken token,
Model model, @PathVariable String groupId) throws EventException {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(inviteLinkRepositoryService.findGroupIdByInvite(link));
Group group = userService.getGroupById(UUID.fromString(groupId));
if (group != null) {
model.addAttribute("group", group);
return "redirect:/gruppen2/detailsSearch?id=" + group.getId();
@ -317,36 +331,42 @@ public class Gruppen2Controller {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/leaveGroup")
public String pLeaveGroup(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId) throws EventException {
public String pLeaveGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String 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);
if(userService.getGroupById(groupId).getMembers().size() == 0){
controllerService.deleteGroupEvent(user.getId(), groupId);
controllerService.passIfLastAdmin(account, UUID.fromString(groupId));
controllerService.deleteUser(user.getId(), UUID.fromString(groupId));
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
}
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/deleteGroup")
public String pDeleteGroup(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId){
public String pDeleteGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId) {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(groupId);
if(group.getRoles().get(user.getId()) != Role.ADMIN ){
Group group = userService.getGroupById(UUID.fromString(groupId));
if (group.getRoles().get(user.getId()) != Role.ADMIN) {
return "error";
}
controllerService.deleteGroupEvent(user.getId(), groupId);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@GetMapping("/details/members/{id}")
public String editMembers(Model model, KeycloakAuthenticationToken token, @PathVariable("id") Long groupId) throws EventException {
public String editMembers(Model model,
KeycloakAuthenticationToken token,
@PathVariable("id") String groupId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(groupId);
User user = new User(account.getName(),"", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId));
User user = new User(account.getName(), "", "", "");
if (group.getMembers().contains(user)) {
if (group.getRoles().get(account.getName()) == Role.ADMIN) {
model.addAttribute("account", account);
@ -357,46 +377,47 @@ public class Gruppen2Controller {
} else {
return "redirect:/details/";
}
}else {
} else {
return "privateGroupNoMember";
}
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/changeRole")
public String changeRole(KeycloakAuthenticationToken token, @RequestParam("group_id") Long groupId,
public String changeRole(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException {
Account account = keyCloakService.createAccountFromPrincipal(token);
if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, groupId)){
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
throw new NoAdminAfterActionException("Du otto bist letzter Admin");
}
controllerService.updateRole(userId, groupId);
controllerService.updateRole(userId, UUID.fromString(groupId));
return "redirect:/gruppen2/details/" + groupId;
}
controllerService.updateRole(userId, groupId);
controllerService.updateRole(userId, UUID.fromString(groupId));
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/changeMaximum")
public String changeMaxSize(@RequestParam("maximum") Long maximum,
@RequestParam("group_id") Long groupId,
KeycloakAuthenticationToken token){
@RequestParam("group_id") String groupId,
KeycloakAuthenticationToken token) {
Account account = keyCloakService.createAccountFromPrincipal(token);
controllerService.updateMaxUser(account, groupId, maximum);
controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum);
return "redirect:/gruppen2/details/members/" + groupId;
}
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator)"})
@PostMapping("/details/members/deleteUser")
public String deleteUser(@RequestParam("group_id") Long groupId,
public String deleteUser(@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) throws EventException {
controllerService.deleteUser(userId, groupId);
if(userService.getGroupById(groupId).getMembers().size() == 0){
controllerService.deleteGroupEvent(userId ,groupId);
controllerService.deleteUser(userId, UUID.fromString(groupId));
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(userId, UUID.fromString(groupId));
}
return "redirect:/gruppen2/details/members/" + groupId;
}

View File

@ -17,10 +17,10 @@ public class MopsController {
public String logout(HttpServletRequest request) throws Exception {
request.logout();
return "redirect:/gruppen2/";
}
}/*
@GetMapping("*")
public String defaultLink() {
return "error";
}
}*/
}