Search in sources :

Example 21 with RoleDto

use of org.motechproject.security.model.RoleDto in project motech by motech.

the class RolesBundleIT method setUp.

@Before
public void setUp() throws Exception {
    super.setUp();
    PermissionDto someOtherPermission = new PermissionDto(PERMISSION_NAME, BUNDLE_NAME);
    RoleDto someOtherRole = new RoleDto(SOME_ROLE, Arrays.asList(PERMISSION_NAME));
    // when
    permissionService.addPermission(someOtherPermission);
    roleService.createRole(someOtherRole);
    if (!userService.hasActiveMotechAdmin()) {
        userService.registerMotechAdmin("motech", "motech", "motech@motech.com", USER_LOCALE);
    }
    setUpSecurityContext("motech", "motech", getPermissions());
    if (!userService.hasUser(USER_AUTHORISED_TO_MANAGE_ROLES)) {
        userService.register(USER_AUTHORISED_TO_MANAGE_ROLES, USER_PASSWORD, "test-user-can-manage-roles@mail.com", USER_EXTERNAL_ID, Arrays.asList(MOTECH_ADMIN), USER_LOCALE);
    }
    if (!userService.hasUser(USER_NOT_AUTHORISED_TO_MANAGE_ROLES)) {
        userService.register(USER_NOT_AUTHORISED_TO_MANAGE_ROLES, USER_PASSWORD, "test-user-cannot-manage-roles@mail.com", USER_EXTERNAL_ID, Arrays.asList(SOME_ROLE), USER_LOCALE);
    }
    clearSecurityContext();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) PermissionDto(org.motechproject.security.model.PermissionDto) Before(org.junit.Before)

Example 22 with RoleDto

use of org.motechproject.security.model.RoleDto in project motech by motech.

the class MotechRoleServiceTest method shouldRefreshUserContextWhenRoleIsCreated.

@Test
public void shouldRefreshUserContextWhenRoleIsCreated() {
    RoleDto role = new RoleDto("role1", asList("permission1"));
    motechRoleService.createRole(role);
    verify(userContextsService).refreshAllUsersContextIfActive();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) Test(org.junit.Test)

Example 23 with RoleDto

use of org.motechproject.security.model.RoleDto in project motech by motech.

the class MotechOpenIdUserDetailsService method loadUserDetails.

/**
 * Adds user for given OpenId to {@link MotechUsersDao}
 * and return his {@link org.springframework.security.core.userdetails.UserDetails}
 *
 * @param token for OpenId
 * @return details of added user
 */
@Override
@Transactional
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
    MotechUser user = motechUsersDao.findUserByOpenId(token.getName());
    if (user == null) {
        List<String> roles = new ArrayList<>();
        if (motechUsersDao.getOpenIdUsers().isEmpty()) {
            for (RoleDto role : motechRoleService.getRoles()) {
                roles.add(role.getRoleName());
            }
        }
        user = new MotechUser(getAttribute(token.getAttributes(), "Email"), "", getAttribute(token.getAttributes(), "Email"), "", roles, token.getName(), Locale.getDefault());
        motechUsersDao.addOpenIdUser(user);
    }
    return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechUser(org.motechproject.security.domain.MotechUser) User(org.springframework.security.core.userdetails.User) MotechUser(org.motechproject.security.domain.MotechUser) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with RoleDto

use of org.motechproject.security.model.RoleDto in project motech by motech.

the class MotechPermissionServiceImpl method removePermissionFromRoles.

private void removePermissionFromRoles(String permissionName) {
    LOGGER.info("Removing permission: {} from roles", permissionName);
    List<RoleDto> roles = motechRoleService.getRoles();
    for (RoleDto role : roles) {
        if (role.hasPermission(permissionName)) {
            role.removePermission(permissionName);
            motechRoleService.updateRole(role);
        }
    }
    LOGGER.info("Removed permission: {} from roles", permissionName);
}
Also used : RoleDto(org.motechproject.security.model.RoleDto)

Example 25 with RoleDto

use of org.motechproject.security.model.RoleDto in project motech by motech.

the class SecurityRoleLoaderTest method shouldCreateNewRoles.

@Test
public void shouldCreateNewRoles() throws IOException {
    when(roleService.getRole("Test Role")).thenReturn(null);
    when(applicationContext.getResource("roles.json")).thenReturn(resource);
    when(resource.exists()).thenReturn(true);
    try (InputStream in = getClass().getClassLoader().getResourceAsStream("roles.json")) {
        when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(IOUtils.toByteArray(in)));
    }
    securityRoleLoader.loadRoles(applicationContext);
    verify(roleService).getRole("Test Role");
    ArgumentCaptor<RoleDto> captor = ArgumentCaptor.forClass(RoleDto.class);
    verify(roleService).createRole(captor.capture());
    assertEquals("Test Role", captor.getValue().getRoleName());
    assertEquals(asList("perm1", "perm2"), captor.getValue().getPermissionNames());
    ArgumentCaptor<PermissionDto> permissionCaptor = ArgumentCaptor.forClass(PermissionDto.class);
    verify(permissionService, times(2)).addPermission(permissionCaptor.capture());
    verifyPermission("perm1", null, permissionCaptor.getAllValues().get(0));
    verifyPermission("perm2", null, permissionCaptor.getAllValues().get(1));
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PermissionDto(org.motechproject.security.model.PermissionDto) Test(org.junit.Test)

Aggregations

RoleDto (org.motechproject.security.model.RoleDto)30 Test (org.junit.Test)21 PermissionDto (org.motechproject.security.model.PermissionDto)5 InputStream (java.io.InputStream)3 MotechRole (org.motechproject.security.domain.MotechRole)3 MotechUser (org.motechproject.security.domain.MotechUser)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Before (org.junit.Before)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 PostConstruct (javax.annotation.PostConstruct)1 HttpClient (org.apache.http.client.HttpClient)1 HttpGet (org.apache.http.client.methods.HttpGet)1 ModuleRegistrationData (org.motechproject.osgi.web.ModuleRegistrationData)1 SubmenuInfo (org.motechproject.osgi.web.SubmenuInfo)1 ModuleRegistrations (org.motechproject.osgi.web.util.ModuleRegistrations)1 MotechPermission (org.motechproject.security.domain.MotechPermission)1