use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class UserGroupServiceImpl method changeUserGroupRole.
/**
* {@inheritDoc}
*/
@Override
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#userGroup, 'canUpdateUserGroup')")
public UserGroupJoin changeUserGroupRole(final User user, final UserGroup userGroup, final UserGroupRole role) throws UserGroupWithoutOwnerException {
final UserGroupJoin join = userGroupJoinRepository.findOne(findUserGroupJoin(user, userGroup));
if (!allowRoleChange(userGroup, join.getRole())) {
throw new UserGroupWithoutOwnerException("Cannot change this user's group role because it would leave the group without an owner.");
}
join.setRole(role);
return userGroupJoinRepository.save(join);
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class UpdateUserGroupPermissionTest method testRejectNotGroupOwnerPermission.
@Test
public void testRejectNotGroupOwnerPermission() {
final String username = "user";
final User u = new User();
final UserGroup ug = new UserGroup("group");
u.setUsername(username);
when(userRepository.loadUserByUsername(username)).thenReturn(u);
when(userRepository.findOne(1L)).thenReturn(u);
when(userGroupRepository.findOne(1L)).thenReturn(ug);
when(userGroupJoinRepository.findUsersInGroup(ug)).thenReturn(ImmutableList.of(new UserGroupJoin(u, ug, UserGroupRole.GROUP_MEMBER)));
final Authentication auth = new UsernamePasswordAuthenticationToken(username, "password1");
assertFalse("permission should not be granted.", updateUserPermission.isAllowed(auth, 1L));
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class UpdateUserGroupPermissionTest method testRejectNotInGroup.
@Test
public void testRejectNotInGroup() {
final String username = "user";
final User u = new User();
final User u2 = new User();
final UserGroup ug = new UserGroup("group");
u.setUsername(username);
when(userRepository.loadUserByUsername(username)).thenReturn(u);
when(userRepository.findOne(1L)).thenReturn(u);
when(userGroupRepository.findOne(1L)).thenReturn(ug);
when(userGroupJoinRepository.findUsersInGroup(ug)).thenReturn(ImmutableList.of(new UserGroupJoin(u2, ug, UserGroupRole.GROUP_OWNER)));
final Authentication auth = new UsernamePasswordAuthenticationToken(username, "password1");
assertFalse("permission should not be granted.", updateUserPermission.isAllowed(auth, 1L));
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class UserGroupServiceImplIT method testCreateAndEditGroup.
@Test
@WithMockUser(username = "differentUser", roles = "USER")
public void testCreateAndEditGroup() {
final UserGroup ug = new UserGroup("new group");
final User u = userService.read(2L);
final User u2 = userService.read(3L);
userGroupService.create(ug);
// now we should check that we're part of the group:
final Collection<UserGroupJoin> groupUsers = userGroupService.getUsersForGroup(ug);
assertTrue("Should be in the group after creating it.", groupUsers.stream().anyMatch(j -> j.getSubject().equals(u)));
// and then also check that we can edit the group
ug.setName("not new group");
userGroupService.update(ug);
// and add users to the group
userGroupService.addUserToGroup(u2, ug, UserGroupRole.GROUP_MEMBER);
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin in project irida by phac-nml.
the class UserGroupServiceImpl method removeUserFromGroup.
/**
* {@inheritDoc}
*/
@Override
@PreAuthorize("hasRole('ROLE_ADMIN') or hasPermission(#userGroup, 'canUpdateUserGroup')")
public void removeUserFromGroup(final User user, final UserGroup userGroup) throws UserGroupWithoutOwnerException {
final UserGroupJoin join = userGroupJoinRepository.findOne(findUserGroupJoin(user, userGroup));
if (!allowRoleChange(userGroup, join.getRole())) {
throw new UserGroupWithoutOwnerException("Cannot remove this user from the group because it would leave the group without an owner.");
}
userGroupJoinRepository.delete(join);
}
Aggregations