use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroup in project irida by phac-nml.
the class ReadProjectPermissionTest method testGrantPermissionByGroup.
@Test
public void testGrantPermissionByGroup() {
final String username = "fbristow";
final User u = new User();
u.setUsername(username);
final Project p = new Project();
final UserGroup g = new UserGroup("The group");
final List<UserGroupProjectJoin> projectGroups = new ArrayList<>();
projectGroups.add(new UserGroupProjectJoin(p, g, ProjectRole.PROJECT_USER));
when(userRepository.loadUserByUsername(username)).thenReturn(u);
when(projectRepository.findOne(1L)).thenReturn(p);
when(pujRepository.getUsersForProject(p)).thenReturn(ImmutableList.of());
when(ugpjRepository.findGroupsByProject(p)).thenReturn(projectGroups);
when(ugRepository.findUsersInGroup(g)).thenReturn(ImmutableList.of(new UserGroupJoin(u, g, UserGroupJoin.UserGroupRole.GROUP_MEMBER)));
Authentication auth = new UsernamePasswordAuthenticationToken("fbristow", "password1");
assertTrue("permission should be granted by user group.", readProjectPermission.isAllowed(auth, 1L));
verify(userRepository).loadUserByUsername(username);
verify(projectRepository).findOne(1L);
verify(pujRepository).getUsersForProject(p);
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroup in project irida by phac-nml.
the class UpdateUserGroupPermissionTest method testGrantPermission.
@Test
public void testGrantPermission() {
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_OWNER)));
final Authentication auth = new UsernamePasswordAuthenticationToken(username, "password1");
assertTrue("permission was not granted.", updateUserPermission.isAllowed(auth, 1L));
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroup in project irida by phac-nml.
the class UserGroupServiceImpl method create.
/**
* {@inheritDoc}
*/
@Override
@PreAuthorize("hasRole('ROLE_USER')")
@Transactional
public UserGroup create(UserGroup object) throws EntityExistsException, ConstraintViolationException {
final UserGroup ug = super.create(object);
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
final User currentUser = userRepository.loadUserByUsername(auth.getName());
addUserToGroup(currentUser, ug, UserGroupRole.GROUP_OWNER);
return ug;
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroup in project irida by phac-nml.
the class ProjectEventHandler method handleUserGroupRemovedEvent.
/**
* Create a {@link UserRemovedProjectEvent}. The method arguments must
* contain a {@link Project} and {@link User}
*
* @param event
* The {@link MethodEvent} that this event is being launched from
*/
private ProjectEvent handleUserGroupRemovedEvent(MethodEvent event) {
final Optional<Object> user = Arrays.stream(event.getArgs()).filter(e -> e instanceof UserGroup).findAny();
final Optional<Object> project = Arrays.stream(event.getArgs()).filter(e -> e instanceof Project).findAny();
if (!user.isPresent() || !project.isPresent()) {
throw new IllegalArgumentException("Project or user group cannot be found on method annotated with @LaunchesProjectEvent(UserGroupRemovedProjectEvent.class)");
}
return eventRepository.save(new UserGroupRemovedProjectEvent((Project) project.get(), (UserGroup) user.get()));
}
use of ca.corefacility.bioinformatics.irida.model.user.group.UserGroup 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));
}
Aggregations