1

Merge remote-tracking branch 'origin/master' into JavaDocComments

# Conflicts:
#	src/main/java/mops/gruppen2/controller/WebController.java
This commit is contained in:
[Mahgs]
2020-03-27 16:39:40 +01:00
7 changed files with 254 additions and 216 deletions

View File

@ -64,6 +64,7 @@ public class WebController {
public String index(KeycloakAuthenticationToken token, Model model) {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("gruppen", userService.getUserGroups(user));
model.addAttribute("user", user);
@ -74,6 +75,7 @@ public class WebController {
@GetMapping("/createOrga")
public String createGroupAsOrga(KeycloakAuthenticationToken token, Model model) {
Account account = keyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic());
return "createOrga";
@ -96,8 +98,8 @@ public class WebController {
UUID parentUUID = controllerService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
controllerService.createGroupAsOrga(account, title, description, visibility, lecture, maxInfiniteUsers, userMaximum, parentUUID, file);
return "redirect:/gruppen2/";
}
@ -105,6 +107,7 @@ public class WebController {
@GetMapping("/createStudent")
public String createGroupAsStudent(KeycloakAuthenticationToken token, Model model) {
Account account = keyCloakService.createAccountFromPrincipal(token);
model.addAttribute("account", account);
model.addAttribute("lectures", groupService.getAllLecturesWithVisibilityPublic());
return "createStudent";
@ -123,7 +126,9 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
UUID parentUUID = controllerService.getUUID(parent);
validationService.checkFields(description, title, userMaximum, maxInfiniteUsers);
controllerService.createGroup(account, title, description, visibility, null, maxInfiniteUsers, userMaximum, parentUUID);
return "redirect:/gruppen2/";
}
@ -135,11 +140,8 @@ public class WebController {
@RequestParam("group_id") String groupId,
@RequestParam(value = "file", required = false) MultipartFile file) {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<User> userList = new ArrayList<>();
Group group = userService.getGroupById(UUID.fromString(groupId));
userList = validationService.checkFile(file, userList, groupId, group, account);
UUID groupUUID = controllerService.getUUID(groupId);
controllerService.addUserList(userList, groupUUID);
controllerService.addUsersFromCsv(account, file, groupId);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -149,16 +151,12 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
model.addAttribute("account", account);
UUID parentId = group.getParent();
Group parent = new Group();
if (!validationService.checkIfUserInGroup(group, user)) {
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
return "detailsNoMember";
}
validationService.throwIfNoAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("title", group.getTitle());
model.addAttribute("description", group.getDescription());
model.addAttribute("admin", Role.ADMIN);
@ -171,16 +169,19 @@ public class WebController {
@RolesAllowed({"ROLE_orga", "ROLE_studentin", "ROLE_actuator"})
@PostMapping("/details/changeMetadata")
@CacheEvict(value = "groups", allEntries = true)
public String changeMetadata(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("groupId") String groupId) {
public String postChangeMetadata(KeycloakAuthenticationToken token,
@RequestParam("title") String title,
@RequestParam("description") String description,
@RequestParam("groupId") String groupId) {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
User user = new User(account.getName(), "", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
validationService.checkTitleAndDescription(title, description, account, groupId);
validationService.throwIfNoAdmin(group, user);
validationService.checkFields(title, description);
controllerService.changeMetaData(account, group, title, description);
return "redirect:/gruppen2/details/" + groupId;
}
@ -192,6 +193,7 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
List<Group> groups = new ArrayList<>();
groups = validationService.checkSearch(search, groups, account);
model.addAttribute("account", account);
model.addAttribute("gruppen", groups);
model.addAttribute("inviteService", inviteService);
@ -204,17 +206,20 @@ public class WebController {
Model model,
HttpServletRequest request,
@PathVariable("id") String groupId) {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(UUID.fromString(groupId));
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
UUID parentId = group.getParent();
String actualURL = request.getRequestURL().toString();
String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/"));
Group parent = controllerService.getParent(parentId);
validationService.checkGroup(group.getTitle());
Group parent = validationService.checkParent(parentId);
validationService.throwIfGroupNotExisting(group.getTitle());
model.addAttribute("account", account);
if (!validationService.checkIfUserInGroup(group, user)) {
validationService.throwIfNoAccessToPrivate(group, user);
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
model.addAttribute("parent", parent);
@ -228,10 +233,9 @@ public class WebController {
model.addAttribute("user", user);
model.addAttribute("admin", Role.ADMIN);
String actualURL = request.getRequestURL().toString();
String serverURL = actualURL.substring(0, actualURL.indexOf("gruppen2/"));
model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + inviteService.getLinkByGroupId(group.getId()));
if (validationService.checkIfAdmin(group, user)) {
model.addAttribute("link", serverURL + "gruppen2/acceptinvite/" + inviteService.getLinkByGroupId(group.getId()));
}
return "detailsMember";
}
@ -240,13 +244,16 @@ public class WebController {
@CacheEvict(value = "groups", allEntries = true)
public String joinGroup(KeycloakAuthenticationToken token,
Model model, @RequestParam("id") String groupId) {
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(UUID.fromString(groupId));
validationService.checkIfUserInGroupJoin(group, user);
validationService.checkIfGroupFull(group);
controllerService.addUser(account, group.getId());
validationService.throwIfUserAlreadyInGroup(group, user);
validationService.throwIfGroupFull(group);
controllerService.addUser(account, UUID.fromString(groupId));
model.addAttribute("account", account);
return "redirect:/gruppen2/";
}
@ -255,12 +262,16 @@ public class WebController {
public String showGroupDetailsNoMember(KeycloakAuthenticationToken token,
Model model,
@RequestParam("id") String groupId) {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfGroupFull(group);
UUID parentId = group.getParent();
Group parent = validationService.checkParent(parentId);
Group parent = controllerService.getParent(parentId);
User user = new User(account.getName(), "", "", "");
model.addAttribute("account", account);
if (validationService.checkIfUserInGroup(group, user)){
return "redirect:/gruppen2/details/" + groupId;
}
model.addAttribute("group", group);
model.addAttribute("parentId", parentId);
@ -275,9 +286,11 @@ public class WebController {
public String acceptInvite(KeycloakAuthenticationToken token,
Model model,
@PathVariable("link") String link) {
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
Group group = userService.getGroupById(inviteService.getGroupIdFromLink(link));
validationService.checkGroup(group.getTitle());
validationService.throwIfGroupNotExisting(group.getTitle());
model.addAttribute("account", keyCloakService.createAccountFromPrincipal(token));
model.addAttribute("group", group);
if (group.getVisibility() == Visibility.PUBLIC) {
@ -297,7 +310,7 @@ public class WebController {
User user = new User(acc.getName(), acc.getGivenname(), acc.getFamilyname(), acc.getEmail());
if (!validationService.checkIfUserInGroupWithoutNoAccessAcception(userService.getGroupById(UUID.fromString(groupId)), user)) {
if (!validationService.checkIfUserInGroup(userService.getGroupById(UUID.fromString(groupId)), user)) {
controllerService.addUser(keyCloakService.createAccountFromPrincipal(token), UUID.fromString(groupId));
}
@ -310,10 +323,10 @@ public class WebController {
public String pLeaveGroup(KeycloakAuthenticationToken token,
@RequestParam("group_id") String groupId) {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
controllerService.passIfLastAdmin(account, UUID.fromString(groupId));
controllerService.deleteUser(user.getId(), UUID.fromString(groupId));
validationService.checkIfGroupEmpty(groupId, user);
User user = new User(account.getName(), "", "", "");
Group group = userService.getGroupById(UUID.fromString(groupId));
controllerService.deleteUser(account, user, group);
return "redirect:/gruppen2/";
}
@ -325,7 +338,9 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
User user = new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail());
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.checkIfAdmin(group, user);
validationService.throwIfNoAdmin(group, user);
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
return "redirect:/gruppen2/";
}
@ -338,7 +353,9 @@ public class WebController {
Account account = keyCloakService.createAccountFromPrincipal(token);
Group group = userService.getGroupById(UUID.fromString(groupId));
User user = new User(account.getName(), "", "", "");
validationService.checkIfAdmin(group, user);
validationService.throwIfNoAdmin(group, user);
model.addAttribute("account", account);
model.addAttribute("members", group.getMembers());
model.addAttribute("group", group);
@ -353,7 +370,16 @@ public class WebController {
@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) {
Account account = keyCloakService.createAccountFromPrincipal(token);
if (validationService.checkIfDemotingSelf(userId, groupId, account)) {
Group group = userService.getGroupById(UUID.fromString(groupId));
User principle = new User(account.getName(), "", "", "");
User user = new User(userId, "", "", "");
validationService.throwIfNoAdmin(group, principle);
controllerService.changeRole(account, user, group);
group = userService.getGroupById(UUID.fromString(groupId));
if (!validationService.checkIfAdmin(group, principle)) {
return "redirect:/gruppen2/details/" + groupId;
}
return "redirect:/gruppen2/details/members/" + groupId;
@ -366,7 +392,10 @@ public class WebController {
@RequestParam("group_id") String groupId,
KeycloakAuthenticationToken token) {
Account account = keyCloakService.createAccountFromPrincipal(token);
validationService.checkIfNewMaximumIsValid(maximum, groupId);
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNewMaximumIsValid(maximum, group);
controllerService.updateMaxUser(account, UUID.fromString(groupId), maximum);
return "redirect:/gruppen2/details/members/" + groupId;
}
@ -375,10 +404,20 @@ public class WebController {
@PostMapping("/details/members/deleteUser")
@CacheEvict(value = "groups", allEntries = true)
public String deleteUser(@RequestParam("group_id") String groupId,
@RequestParam("user_id") String userId) {
@RequestParam("user_id") String userId,
KeycloakAuthenticationToken token) {
Account account = keyCloakService.createAccountFromPrincipal(token);
User principle = new User(account.getName(), "", "", "");
User user = new User(userId, "", "", "");
controllerService.deleteUser(userId, UUID.fromString(groupId));
validationService.checkIfGroupEmpty(groupId, user);
Group group = userService.getGroupById(UUID.fromString(groupId));
validationService.throwIfNoAdmin(group, principle);
controllerService.deleteUser(account, user, group);
if (!validationService.checkIfUserInGroup(group, principle)) {
return "redirect:/gruppen2/";
}
return "redirect:/gruppen2/details/members/" + groupId;
}

View File

@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus;
public class BadParameterException extends EventException {
public BadParameterException(String info) {
super(HttpStatus.INTERNAL_SERVER_ERROR, "Fehlerhafter Parameter angegeben!", info);
super(HttpStatus.BAD_REQUEST, "Fehlerhafter Parameter angegeben!", info);
}
}

View File

@ -5,6 +5,6 @@ import org.springframework.http.HttpStatus;
public class NoValueException extends EventException {
public NoValueException(String info) {
super(HttpStatus.NO_CONTENT, "Eine Information fehlt.", info);
super(HttpStatus.BAD_REQUEST, "Eine Information fehlt.", info);
}
}

View File

@ -36,12 +36,14 @@ public class ControllerService {
private final EventService eventService;
private final UserService userService;
private final ValidationService validationService;
private final InviteService inviteService;
private final Logger logger;
public ControllerService(EventService eventService, UserService userService, InviteService inviteService) {
public ControllerService(EventService eventService, UserService userService, ValidationService validationService, InviteService inviteService) {
this.eventService = eventService;
this.userService = userService;
this.validationService = validationService;
this.inviteService = inviteService;
this.logger = Logger.getLogger("controllerServiceLogger");
}
@ -68,10 +70,12 @@ public class ControllerService {
inviteService.createLink(groupId);
User user = new User(account.getName(), "", "", "");
addUser(account, groupId);
updateTitle(account, groupId, title);
updateDescription(account, groupId, description);
updateRole(account.getName(), groupId);
updateRole(user, groupId);
return groupId;
}
@ -107,6 +111,40 @@ public class ControllerService {
addUserList(newUsers, groupId);
}
public void addUsersFromCsv(Account account, MultipartFile file, String groupId) throws IOException{
Group group = userService.getGroupById(UUID.fromString(groupId));
List<User> newUserList = readCsvFile(file);
removeOldUsersFromNewUsers(group.getMembers(), newUserList);
UUID groupUUID = getUUID(groupId);
Long newUserMaximum = adjustUserMaximum((long) newUserList.size(), (long) group.getMembers().size(), group.getUserMaximum());
if (newUserMaximum > group.getUserMaximum()){
updateMaxUser(account, groupUUID, newUserMaximum);
}
addUserList(newUserList, groupUUID);
}
public void changeMetaData(Account account, Group group, String title, String description) {
if (!title.equals(group.getTitle())){
updateTitle(account, group.getId(), title);
}
if (!description.equals(group.getDescription())) {
updateDescription(account, group.getId(), description);
}
}
public Group getParent(UUID parentId) {
Group parent = new Group();
if (!idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
return parent;
}
private void removeOldUsersFromNewUsers(List<User> oldUsers, List<User> newUsers) {
for (User oldUser : oldUsers) {
newUsers.remove(oldUser);
@ -175,8 +213,8 @@ public class ControllerService {
eventService.saveEvent(addUserEvent);
}
public void addUserList(List<User> users, UUID groupId) {
for (User user : users) {
public void addUserList(List<User> newUsers, UUID groupId) {
for (User user : newUsers) {
Group group = userService.getGroupById(groupId);
if (group.getMembers().contains(user)) {
logger.info("Benutzer " + user.getId() + " ist bereits in Gruppe");
@ -202,41 +240,32 @@ public class ControllerService {
eventService.saveEvent(updateUserMaxEvent);
}
public void updateRole(String userId, UUID groupId) throws EventException {
public void updateRole(User user, UUID groupId) throws EventException {
UpdateRoleEvent updateRoleEvent;
Group group = userService.getGroupById(groupId);
User user = null;
for (User member : group.getMembers()) {
if (member.getId().equals(userId)) {
user = member;
}
}
if (user == null) {
throw new UserNotFoundException(this.getClass().toString());
}
validationService.throwIfNotInGroup(group, user);
if (group.getRoles().get(user.getId()) == ADMIN) {
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), Role.MEMBER);
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), Role.MEMBER);
} else {
updateRoleEvent = new UpdateRoleEvent(groupId, user.getId(), ADMIN);
updateRoleEvent = new UpdateRoleEvent(group.getId(), user.getId(), ADMIN);
}
eventService.saveEvent(updateRoleEvent);
}
public void deleteUser(String userId, UUID groupId) throws EventException {
Group group = userService.getGroupById(groupId);
User user = null;
for (User member : group.getMembers()) {
if (member.getId().equals(userId)) {
user = member;
}
}
public void deleteUser(Account account, User user, Group group) throws EventException {
changeRoleIfLastAdmin(account, group);
if (user == null) {
throw new UserNotFoundException(this.getClass().toString());
}
validationService.throwIfNotInGroup(group, user);
deleteUserEvent(user, group.getId());
if (validationService.checkIfGroupEmpty(group.getId())) {
deleteGroupEvent(user.getId(), group.getId());
}
}
private void deleteUserEvent(User user, UUID groupId) {
DeleteUserEvent deleteUserEvent = new DeleteUserEvent(groupId, user.getId());
eventService.saveEvent(deleteUserEvent);
}
@ -247,36 +276,39 @@ public class ControllerService {
eventService.saveEvent(deleteGroupEvent);
}
public boolean passIfLastAdmin(Account account, UUID groupId) {
Group group = userService.getGroupById(groupId);
public void changeRoleIfLastAdmin(Account account, Group group) {
if (group.getMembers().size() <= 1) {
return true;
return;
}
if (isLastAdmin(account, group)) {
String newAdminId = getVeteranMember(account, group);
updateRole(newAdminId, groupId);
}
return false;
promoteVeteranMember(account, group);
}
private boolean isLastAdmin(Account account, Group group) {
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
if (entry.getValue() == ADMIN) {
if (!(entry.getKey().equals(account.getName()))) {
return false;
}
private void promoteVeteranMember(Account account, Group group) {
if (validationService.checkIfLastAdmin(account, group)) {
User newAdmin = getVeteranMember(account, group);
updateRole(newAdmin, group.getId());
}
}
public void changeRole(Account account, User user, Group group) {
if (user.getId().equals(account.getName())) {
if (group.getMembers().size() <= 1) {
validationService.throwIfLastAdmin(account, group);
}
promoteVeteranMember(account, group);
}
return true;
updateRole(user, group.getId());
}
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();
private User getVeteranMember(Account account, Group group) {
List<User> members = group.getMembers();
String newAdminId;
if (members.get(0).getId().equals(account.getName())) {
newAdminId = members.get(1).getId();
} else {
newAdminId = members.get(0).getId();
}
return mitglieder.get(0).getId();
return new User(newAdminId, "", "", "");
}
public UUID getUUID(String id) {

View File

@ -4,43 +4,27 @@ import mops.gruppen2.domain.Group;
import mops.gruppen2.domain.Role;
import mops.gruppen2.domain.User;
import mops.gruppen2.domain.Visibility;
import mops.gruppen2.domain.exception.BadParameterException;
import mops.gruppen2.domain.exception.GroupFullException;
import mops.gruppen2.domain.exception.GroupNotFoundException;
import mops.gruppen2.domain.exception.NoAccessException;
import mops.gruppen2.domain.exception.NoAdminAfterActionException;
import mops.gruppen2.domain.exception.NoValueException;
import mops.gruppen2.domain.exception.UserAlreadyExistsException;
import mops.gruppen2.domain.exception.WrongFileException;
import mops.gruppen2.domain.exception.*;
import mops.gruppen2.security.Account;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static mops.gruppen2.domain.Role.ADMIN;
@Service
public class ValidationService {
private final ControllerService controllerService;
private final UserService userService;
private final GroupService groupService;
public ValidationService(ControllerService controllerService, UserService userService, GroupService groupService) {
this.controllerService = controllerService;
public ValidationService(UserService userService, GroupService groupService) {
this.userService = userService;
this.groupService = groupService;
}
public void checkTitleAndDescription(String title, String description, Account account, String groupId) {
if (title == null || description == null) {
throw new NoValueException("Titel und Beschreibung müssen ausgefüllt werden");
}
controllerService.updateTitle(account, UUID.fromString(groupId), title);
controllerService.updateDescription(account, UUID.fromString(groupId), description);
}
public List<Group> checkSearch(String search, List<Group> groups, Account account) {
if (search != null) {
groups = groupService.findGroupWith(search, account);
@ -48,83 +32,73 @@ public class ValidationService {
return groups;
}
public void checkGroup(String title) {
public void throwIfGroupNotExisting(String title) {
if (title == null) {
throw new GroupNotFoundException("@details");
}
}
public boolean checkIfUserInGroup(Group group, User user) {
if (!group.getMembers().contains(user) && group.getVisibility() == Visibility.PRIVATE) {
public void throwIfNoAccessToPrivate(Group group, User user) {
if (!checkIfUserInGroup(group, user) && group.getVisibility() == Visibility.PRIVATE) {
throw new NoAccessException("");
} else {
return group.getMembers().contains(user);
}
}
//Warum ist das überhaupt nötig smh
public boolean checkIfUserInGroupWithoutNoAccessAcception(Group group, User user) {
public boolean checkIfUserInGroup(Group group, User user) {
return group.getMembers().contains(user);
}
public Group checkParent(UUID parentId) {
Group parent = new Group();
if (!controllerService.idIsEmpty(parentId)) {
parent = userService.getGroupById(parentId);
}
return parent;
}
public void checkIfUserInGroupJoin(Group group, User user) {
if (group.getMembers().contains(user)) {
public void throwIfUserAlreadyInGroup(Group group, User user) {
if (checkIfUserInGroup(group, user)) {
throw new UserAlreadyExistsException("@details");
}
}
public void checkIfGroupFull(Group group) {
public void throwIfNotInGroup(Group group, User user) {
if (!checkIfUserInGroup(group, user)) {
throw new UserNotFoundException(this.getClass().toString());
}
}
public void throwIfGroupFull(Group group) {
if (group.getUserMaximum() < group.getMembers().size() + 1) {
throw new GroupFullException("Du kannst der Gruppe daher leider nicht beitreten.");
}
}
public void checkIfGroupEmpty(String groupId, User user) {
if (userService.getGroupById(UUID.fromString(groupId)).getMembers().isEmpty()) {
controllerService.deleteGroupEvent(user.getId(), UUID.fromString(groupId));
}
public boolean checkIfGroupEmpty(UUID groupId) {
return userService.getGroupById(groupId).getMembers().isEmpty();
}
public void checkIfAdmin(Group group, User user) {
checkIfUserInGroup(group, user);
public void throwIfNoAdmin(Group group, User user) {
throwIfNoAccessToPrivate(group, user);
if (group.getRoles().get(user.getId()) != Role.ADMIN) {
throw new NoAccessException("");
}
}
public boolean checkIfDemotingSelf(String userId, String groupId, Account account) {
if (userId.equals(account.getName())) {
if (controllerService.passIfLastAdmin(account, UUID.fromString(groupId))) {
throw new NoAdminAfterActionException("Du Otto bist letzter Admin");
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return true;
public boolean checkIfAdmin(Group group, User user) {
if (checkIfUserInGroup(group, user)) {
return group.getRoles().get(user.getId()) == Role.ADMIN;
}
controllerService.updateRole(userId, UUID.fromString(groupId));
return false;
}
public List<User> checkFile(MultipartFile file, List<User> userList, String groupId, Group group, Account account) {
if (!file.isEmpty()) {
try {
userList = CsvService.read(file.getInputStream());
if (userList.size() + group.getMembers().size() > group.getUserMaximum()) {
controllerService.updateMaxUser(account, UUID.fromString(groupId), (long) userList.size() + group.getMembers().size());
public boolean checkIfLastAdmin(Account account, Group group) {
for (Map.Entry<String, Role> entry : group.getRoles().entrySet()) {
if (entry.getValue() == ADMIN) {
if (!(entry.getKey().equals(account.getName()))) {
return false;
}
} catch (IOException ex) {
throw new WrongFileException(file.getOriginalFilename());
}
}
return userList;
return true;
}
public void throwIfLastAdmin(Account account, Group group) {
if (checkIfLastAdmin(account, group)) {
throw new NoAdminAfterActionException("Du Otto bist letzter Admin!");
}
}
/**
@ -134,7 +108,7 @@ public class ValidationService {
* @param title Der Titel der Gruppe
* @param userMaximum Das user Limit der Gruppe
*/
public void checkFields(String description, String title, Long userMaximum, Boolean maxInfiniteUsers) {
public void checkFields(String title, String description, Long userMaximum, Boolean maxInfiniteUsers) {
if (description == null || description.trim().length() == 0) {
throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben");
}
@ -154,8 +128,17 @@ public class ValidationService {
}
}
public void checkIfNewMaximumIsValid(Long newUserMaximum, String groupId) {
Group group = userService.getGroupById(UUID.fromString(groupId));
public void checkFields(String title, String description) {
if (description == null || description.trim().length() == 0) {
throw new BadParameterException("Die Beschreibung wurde nicht korrekt angegeben");
}
if (title == null || title.trim().length() == 0) {
throw new BadParameterException("Der Titel wurde nicht korrekt angegeben");
}
}
public void throwIfNewMaximumIsValid(Long newUserMaximum, Group group) {
if (newUserMaximum == null) {
throw new BadParameterException("Es wurde keine neue maximale Teilnehmeranzahl angegeben!");
}

View File

@ -23,17 +23,23 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = Gruppen2Application.class)
@Transactional
@Rollback
class ControllerServiceTest {
Account account, account2, account3;
Account account;
Account account2;
Account account3;
ControllerService controllerService;
EventService eventService;
UserService userService;
ValidationService validationService;
@Autowired
EventRepository eventRepository;
GroupService groupService;
@ -47,7 +53,8 @@ class ControllerServiceTest {
eventService = new EventService(jsonService, eventRepository);
groupService = new GroupService(eventService, eventRepository);
userService = new UserService(groupService, eventService);
controllerService = new ControllerService(eventService, userService, inviteService);
validationService = new ValidationService(userService, groupService);
controllerService = new ControllerService(eventService, userService, validationService, inviteService);
Set<String> roles = new HashSet<>();
roles.add("l");
account = new Account("ich", "ich@hhu.de", "l", "ichdude", "jap", roles);
@ -220,7 +227,8 @@ class ControllerServiceTest {
controllerService.createGroup(account, "test", "hi", true, true, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
controllerService.deleteUser(account, user, groups.get(0));
assertTrue(userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail())).isEmpty());
}
@ -229,7 +237,8 @@ class ControllerServiceTest {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.updateRole(account.getName(), groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
controllerService.updateRole(user, groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account.getName()));
}
@ -239,7 +248,8 @@ class ControllerServiceTest {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.updateRole(account2.getName(), groups.get(0).getId());
User user = new User(account2.getName(), "", "", "");
controllerService.updateRole(user, groups.get(0).getId());
groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
}
@ -248,16 +258,18 @@ class ControllerServiceTest {
public void updateRoleNonUserTest() {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(account2.getName(), groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage());
User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.updateRole(user, groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage());
}
@Test
public void deleteNonUserTest() {
controllerService.createGroup(account, "test", "hi", true, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account2.getName(), groups.get(0).getId()));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ControllerService)\"", exception.getMessage());
User user = new User(account2.getName(), "", "", "");
Throwable exception = assertThrows(UserNotFoundException.class, () -> controllerService.deleteUser(account, user, groups.get(0)));
assertEquals("404 NOT_FOUND \"Der User wurde nicht gefunden. (class mops.gruppen2.service.ValidationService)\"", exception.getMessage());
}
void testTitleAndDescription(String title, String description) {
@ -270,8 +282,9 @@ class ControllerServiceTest {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
}
@ -281,10 +294,12 @@ class ControllerServiceTest {
controllerService.createGroup(account, "test", "hi", null, null, true, null, null);
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.updateRole(account2.getName(), groups.get(0).getId());
User user2 = new User(account2.getName(), "", "", "");
controllerService.updateRole(user2, groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
controllerService.changeRoleIfLastAdmin(account, groups.get(0));
User user = new User(account.getName(), "", "", "");
controllerService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));
}
@ -295,8 +310,9 @@ class ControllerServiceTest {
List<Group> groups = userService.getUserGroups(new User(account.getName(), account.getGivenname(), account.getFamilyname(), account.getEmail()));
controllerService.addUser(account2, groups.get(0).getId());
controllerService.addUser(account3, groups.get(0).getId());
controllerService.passIfLastAdmin(account, groups.get(0).getId());
controllerService.deleteUser(account.getName(), groups.get(0).getId());
User user = new User(account.getName(), "", "", "");
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
controllerService.deleteUser(account, user, groups.get(0));
groups = userService.getUserGroups(new User(account2.getName(), account2.getGivenname(), account2.getFamilyname(), account2.getEmail()));
assertEquals(Role.ADMIN, groups.get(0).getRoles().get(account2.getName()));
assertEquals(Role.MEMBER, groups.get(0).getRoles().get(account3.getName()));

View File

@ -77,38 +77,6 @@ class EventServiceTest {
assertThat(dto.getEvent_type()).isEqualTo("CreateGroupEvent");
}
@Disabled
@Test
void getNewEvents_createGroupEvents() {
eventService.saveAll(createPrivateGroupEvents(10));
assertThat(eventService.getNewEvents(0L)).hasSize(10);
assertThat(eventService.getNewEvents(5L)).hasSize(5);
assertThat(eventService.getNewEvents(10L)).isEmpty();
}
@Disabled
@Test
void getNewEvents_mixedEvents() {
eventService.saveAll(createPrivateGroupEvent(uuidFromInt(0)),
updateGroupDescriptionEvent(uuidFromInt(0)),
createPrivateGroupEvent(uuidFromInt(1)),
updateGroupDescriptionEvent(uuidFromInt(1)));
assertThat(eventService.getNewEvents(0L)).hasSize(4);
assertThat(eventService.getNewEvents(1L)).hasSize(4);
assertThat(eventService.getNewEvents(2L)).hasSize(2);
assertThat(eventService.getNewEvents(3L)).hasSize(2);
}
@Disabled
@Test
void getMaxEvent_id() {
eventService.saveAll(createPrivateGroupEvents(10));
assertThat(eventService.getMaxEvent_id()).isEqualTo(10);
}
@Test
void getEventsOfGroup() {
eventService.saveAll(addUserEvents(10, uuidFromInt(0)),