Skip to content
Snippets Groups Projects
Commit dd49a0c2 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch 'feature/addEndpointtoChangeAuthorityInGroup' into 'main'

The endpoint to change authority level was added

See merge request idatt2106-v23-03/backend!127
parents 3039a890 414b349c
No related branches found
No related tags found
No related merge requests found
...@@ -62,4 +62,20 @@ public class UserGroupAssoController { ...@@ -62,4 +62,20 @@ public class UserGroupAssoController {
return userGroupAssoService.addPersonToGroup(username,linkCode,"USER").map(ResponseEntity::ok).orElseGet(()-> ResponseEntity.notFound().build()); return userGroupAssoService.addPersonToGroup(username,linkCode,"USER").map(ResponseEntity::ok).orElseGet(()-> ResponseEntity.notFound().build());
} }
/**
* Changes the authority level of a user in a group.
*
* @param groupId the ID of the group
* @param username the username of the user whose authority level is to be changed
* @param authority the new authority level of the user
* @return a ResponseEntity object containing the updated UserGroupAsso object and an HTTP status code of 200,
* or a ResponseEntity object with an HTTP status code of 404 if the group or user does not exist
*/
@PutMapping("/changeAuthority/{groupId}/{username}/{authority}")
public ResponseEntity<?> changeAuthority(@PathVariable("groupId") long groupId,
@PathVariable("username") String username,
@PathVariable("authority") String authority){
return userGroupAssoService.changeAuthorityOfUser(username,groupId,authority).map(ResponseEntity::ok).orElseGet(() ->ResponseEntity.notFound().build());
}
} }
...@@ -26,7 +26,7 @@ public class UserGroupAssoService { ...@@ -26,7 +26,7 @@ public class UserGroupAssoService {
UserGroupAsso userGroupTable1 = new UserGroupAsso(); UserGroupAsso userGroupTable1 = new UserGroupAsso();
userGroupTable1.setGroup(group); userGroupTable1.setGroup(group);
userGroupTable1.setUser(user); userGroupTable1.setUser(user);
userGroupTable1.setPrimaryGroup(true); userGroupTable1.setPrimaryGroup(false);
userGroupTable1.setGroupAuthority(authority); userGroupTable1.setGroupAuthority(authority);
userGroupTable1.setId(UserGroupId.builder() userGroupTable1.setId(UserGroupId.builder()
.groupId(group.getGroupId()) .groupId(group.getGroupId())
...@@ -84,8 +84,16 @@ public class UserGroupAssoService { ...@@ -84,8 +84,16 @@ public class UserGroupAssoService {
return Optional.of(userGroupAsso); return Optional.of(userGroupAsso);
} }
/**
* Adds a new user to a group.
*
* @param username the username of the user to add
* @param linkCode the link code of the group to which the user is to be added
* @param authority the authority level of the user in the group
* @return an Optional object containing the UserGroupAsso object of the user in the group,
* or an empty Optional if the group or user does not exist, or if the user is already in the group
*/
public Optional<Object> addPersonToGroup(String username, String linkCode, String authority){ public Optional<Object> addPersonToGroup(String username, String linkCode, String authority){
Optional<Group> group = groupRepository.findByLinkCode(linkCode); Optional<Group> group = groupRepository.findByLinkCode(linkCode);
Optional<User> user = userRepository.findByUsername(username); Optional<User> user = userRepository.findByUsername(username);
if(group.isEmpty()) return Optional.empty(); if(group.isEmpty()) return Optional.empty();
...@@ -96,4 +104,22 @@ public class UserGroupAssoService { ...@@ -96,4 +104,22 @@ public class UserGroupAssoService {
return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get()); return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get());
} }
/**
* Changes the authority of a user in a group and updates the primary group if necessary.
* @param username the username of the user whose authority will be changed.
* @param groupId the ID of the group in which the user's authority will be changed.
* @param authority the new authority level for the user in the group.
* @return an Optional containing the updated UserGroupAsso object, or an empty Optional if the user or group does not exist, or if the user is not in the group.
*/
public Optional<Object> changeAuthorityOfUser(String username, long groupId, String authority){
Optional<Group> group = groupRepository.findByGroupId(groupId);
Optional<User> user = userRepository.findByUsername(username);
if(group.isEmpty()) return Optional.empty();
if(user.isEmpty()) return Optional.empty();
if(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).isEmpty()) return Optional.of("User is not in this group!");
save(user.get(),group.get(),authority);
changePrimaryGroup(userGroupAssoRepository.findFirstByUserAndPrimaryGroup(user.get(),true).get().getGroup().getGroupId(), group.get().getGroupId(),username);
return Optional.of(userGroupAssoRepository.findAllByGroupAndUser(group.get(),user.get()).get());
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment