Search in sources :

Example 11 with RoleDto

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

the class MotechRoleServiceBundleIT method shouldNotDeleteRoleWithUsers.

@Test(expected = RoleHasUserException.class)
public void shouldNotDeleteRoleWithUsers() {
    motechRoleService.createRole(new RoleDto("Role-With-User", asList("permissionA, permissionB"), true));
    RoleDto role = motechRoleService.getRole("Role-With-User");
    assertNotNull(role);
    motechUserService.register("duke", "password", "email", "1234", asList("Role-With-User"), Locale.ENGLISH);
    MotechUser motechUser = usersDataService.findByUserName("duke");
    assertNotNull(motechUser);
    assertTrue(motechUser.hasRole("Role-With-User"));
    motechRoleService.deleteRole(role);
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechUser(org.motechproject.security.domain.MotechUser) Test(org.junit.Test)

Example 12 with RoleDto

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

the class MotechRoleServiceBundleIT method shouldNotDeleteNondeletableRole.

@Test
public void shouldNotDeleteNondeletableRole() {
    motechRoleService.createRole(new RoleDto("Nondeletable-Role", asList("permissionA, permissionB"), false));
    RoleDto role = motechRoleService.getRole("Nondeletable-Role");
    assertNotNull(role);
    motechRoleService.deleteRole(role);
    role = motechRoleService.getRole("Nondeletable-Role");
    assertNotNull(role);
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) Test(org.junit.Test)

Example 13 with RoleDto

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

the class MotechPermissionServiceImpl method add.

private void add(final MotechPermission permission) {
    if (findPermissionByName(permission.getPermissionName()) != null) {
        return;
    }
    permissionsDataService.create(permission);
    RoleDto adminRole = motechRoleService.getRole(MOTECH_ADMIN);
    if (adminRole != null) {
        List<String> permissions = adminRole.getPermissionNames();
        permissions.add(permission.getPermissionName());
        adminRole.setPermissionNames(permissions);
        motechRoleService.updateRole(adminRole);
    }
}
Also used : RoleDto(org.motechproject.security.model.RoleDto)

Example 14 with RoleDto

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

the class SecurityRoleLoader method loadRoles.

/**
 * Loads from roles.json file and adds or update them using
 * {@link org.motechproject.security.service.MotechRoleService}
 *
 * @param applicationContext in which file with roles can be found
 */
public void loadRoles(ApplicationContext applicationContext) {
    LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName());
    Resource rolesResource = applicationContext.getResource("roles.json");
    if (rolesResource.exists()) {
        LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName());
        try (InputStream in = rolesResource.getInputStream()) {
            List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() {
            }.getType());
            for (RoleDto role : roles) {
                RoleDto existingRole = roleService.getRole(role.getRoleName());
                if (existingRole == null) {
                    roleService.createRole(role);
                } else if (roleNeedUpdate(existingRole, role)) {
                    existingRole.setPermissionNames(role.getPermissionNames());
                    roleService.updateRole(existingRole);
                }
                savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext));
            }
        } catch (IOException e) {
            LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e);
        }
    }
    LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName());
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) List(java.util.List) IOException(java.io.IOException)

Example 15 with RoleDto

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

the class Initialize method initialize.

/**
 * Initializes module by creating MOTECH Admin role and permissions
 */
@PostConstruct
public void initialize() {
    // Create MOTECH Admin role
    if (motechRoleService.getRole(MOTECH_ADMIN) == null) {
        List<String> permissionsNames = new LinkedList<>();
        List<MotechPermission> permissions = permissionsDataService.retrieveAll();
        for (MotechPermission permission : permissions) {
            permissionsNames.add(permission.getPermissionName());
        }
        RoleDto adminRole = new RoleDto(MOTECH_ADMIN, permissionsNames, false);
        motechRoleService.createRole(adminRole);
    }
    // initialize startup permission for Admin role
    prepareStartupPermissions();
}
Also used : RoleDto(org.motechproject.security.model.RoleDto) MotechPermission(org.motechproject.security.domain.MotechPermission) LinkedList(java.util.LinkedList) PostConstruct(javax.annotation.PostConstruct)

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