Search in sources :

Example 11 with UserGroup

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);
}
Also used : Project(ca.corefacility.bioinformatics.irida.model.project.Project) User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) ArrayList(java.util.ArrayList) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) Test(org.junit.Test)

Example 12 with UserGroup

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));
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) Test(org.junit.Test)

Example 13 with UserGroup

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;
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(javax.transaction.Transactional)

Example 14 with UserGroup

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()));
}
Also used : ProjectSampleJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectSampleJoinRepository) Arrays(java.util.Arrays) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) ProjectEventRepository(ca.corefacility.bioinformatics.irida.repositories.ProjectEventRepository) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) SampleRepository(ca.corefacility.bioinformatics.irida.repositories.sample.SampleRepository) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ArrayList(java.util.ArrayList) ProjectUserJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectUserJoin) LaunchesProjectEvent(ca.corefacility.bioinformatics.irida.events.annotations.LaunchesProjectEvent) ProjectRepository(ca.corefacility.bioinformatics.irida.repositories.ProjectRepository) Logger(org.slf4j.Logger) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Collection(java.util.Collection) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) SampleRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleRemovedProjectEvent) UserGroupRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserGroupRoleSetProjectEvent) Project(ca.corefacility.bioinformatics.irida.model.project.Project) List(java.util.List) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) UserGroupProjectJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin) Optional(java.util.Optional) User(ca.corefacility.bioinformatics.irida.model.user.User) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) UserGroupRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.enums.UserGroupRemovedProjectEvent) Project(ca.corefacility.bioinformatics.irida.model.project.Project) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)

Example 15 with UserGroup

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));
}
Also used : User(ca.corefacility.bioinformatics.irida.model.user.User) Authentication(org.springframework.security.core.Authentication) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) Test(org.junit.Test)

Aggregations

UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)30 User (ca.corefacility.bioinformatics.irida.model.user.User)18 DTUserGroup (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup)12 UserGroupJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin)11 Test (org.junit.Test)10 Project (ca.corefacility.bioinformatics.irida.model.project.Project)9 UserGroupRole (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole)7 Authentication (org.springframework.security.core.Authentication)7 UserGroupWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException)6 EntityExistsException (ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException)5 ConstraintViolation (javax.validation.ConstraintViolation)5 ConstraintViolationException (javax.validation.ConstraintViolationException)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Autowired (org.springframework.beans.factory.annotation.Autowired)5 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)5 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)5 WithMockUser (org.springframework.security.test.context.support.WithMockUser)5 DataTablesResponse (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)4 DataTablesResponseModel (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel)4