Search in sources :

Example 6 with UserGroupJoin

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);
}
Also used : UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 7 with UserGroupJoin

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));
}
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 8 with UserGroupJoin

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));
}
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 9 with UserGroupJoin

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);
}
Also used : UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) DatabaseTearDown(com.github.springtestdbunit.annotation.DatabaseTearDown) AnnotationConfigContextLoader(org.springframework.test.context.support.AnnotationConfigContextLoader) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) TestExecutionListeners(org.springframework.test.context.TestExecutionListeners) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) DependencyInjectionTestExecutionListener(org.springframework.test.context.support.DependencyInjectionTestExecutionListener) WithSecurityContextTestExcecutionListener(org.springframework.security.test.context.support.WithSecurityContextTestExcecutionListener) Collection(java.util.Collection) UserGroupRole(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) Assert.assertTrue(org.junit.Assert.assertTrue) DatabaseSetup(com.github.springtestdbunit.annotation.DatabaseSetup) Test(org.junit.Test) IridaApiServicesConfig(ca.corefacility.bioinformatics.irida.config.services.IridaApiServicesConfig) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) UserGroupService(ca.corefacility.bioinformatics.irida.service.user.UserGroupService) DbUnitTestExecutionListener(com.github.springtestdbunit.DbUnitTestExecutionListener) WithMockUser(org.springframework.security.test.context.support.WithMockUser) UserService(ca.corefacility.bioinformatics.irida.service.user.UserService) ContextConfiguration(org.springframework.test.context.ContextConfiguration) User(ca.corefacility.bioinformatics.irida.model.user.User) IridaApiJdbcDataSourceConfig(ca.corefacility.bioinformatics.irida.config.data.IridaApiJdbcDataSourceConfig) WithMockUser(org.springframework.security.test.context.support.WithMockUser) User(ca.corefacility.bioinformatics.irida.model.user.User) UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroup(ca.corefacility.bioinformatics.irida.model.user.group.UserGroup) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 10 with UserGroupJoin

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);
}
Also used : UserGroupJoin(ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin) UserGroupWithoutOwnerException(ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

UserGroupJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin)13 User (ca.corefacility.bioinformatics.irida.model.user.User)10 UserGroup (ca.corefacility.bioinformatics.irida.model.user.group.UserGroup)9 Test (org.junit.Test)5 Authentication (org.springframework.security.core.Authentication)5 UserGroupWithoutOwnerException (ca.corefacility.bioinformatics.irida.exceptions.UserGroupWithoutOwnerException)4 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 Project (ca.corefacility.bioinformatics.irida.model.project.Project)3 UserGroupRole (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupJoin.UserGroupRole)3 UserGroupProjectJoin (ca.corefacility.bioinformatics.irida.model.user.group.UserGroupProjectJoin)3 DTUserGroup (ca.corefacility.bioinformatics.irida.ria.web.models.datatables.DTUserGroup)3 Autowired (org.springframework.beans.factory.annotation.Autowired)3 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)2 DataTablesResponse (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.DataTablesResponse)2 DataTablesResponseModel (ca.corefacility.bioinformatics.irida.ria.web.components.datatables.models.DataTablesResponseModel)2 UserGroupService (ca.corefacility.bioinformatics.irida.service.user.UserGroupService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 IridaApiJdbcDataSourceConfig (ca.corefacility.bioinformatics.irida.config.data.IridaApiJdbcDataSourceConfig)1