Search in sources :

Example 6 with MotechPermission

use of org.motechproject.security.domain.MotechPermission in project motech by motech.

the class MotechPermissionServiceImpl method addPermission.

@Override
@Transactional
public void addPermission(PermissionDto permission) {
    LOGGER.info("Adding permission: {} from bundle: {}", permission.getPermissionName(), permission.getBundleName());
    add(new MotechPermission(permission.getPermissionName(), permission.getBundleName()));
    // the admin role was potentially updated
    userContextsService.refreshAllUsersContextIfActive();
    LOGGER.info("Added permission: {} from bundle: {}", permission.getPermissionName(), permission.getBundleName());
}
Also used : MotechPermission(org.motechproject.security.domain.MotechPermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with MotechPermission

use of org.motechproject.security.domain.MotechPermission 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)

Example 8 with MotechPermission

use of org.motechproject.security.domain.MotechPermission in project motech by motech.

the class MotechPermissionServiceImpl method deletePermission.

@Override
@Transactional
public void deletePermission(String permissionName) {
    LOGGER.info("Deleting permission: {}", permissionName);
    MotechPermission permission = permissionsDataService.findByPermissionName(permissionName);
    if (permission != null) {
        permissionsDataService.delete(permission);
        removePermissionFromRoles(permissionName);
        userContextsService.refreshAllUsersContextIfActive();
    }
    LOGGER.info("Deleted permission: {}", permissionName);
}
Also used : MotechPermission(org.motechproject.security.domain.MotechPermission) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with MotechPermission

use of org.motechproject.security.domain.MotechPermission in project motech by motech.

the class MotechPermissionServiceTest method shouldReturnPermissions.

@Test
public void shouldReturnPermissions() {
    MotechPermission perm1 = new MotechPermission("perm1", "bundle1");
    MotechPermission perm2 = new MotechPermission("perm2", "bundle2");
    when(motechPermissionsDataService.retrieveAll()).thenReturn(asList(perm1, perm2));
    List<PermissionDto> result = permissionService.getPermissions();
    assertNotNull(result);
    assertEquals(asList("perm1", "perm2"), extract(result, on(PermissionDto.class).getPermissionName()));
    assertEquals(asList("bundle1", "bundle2"), extract(result, on(PermissionDto.class).getBundleName()));
}
Also used : MotechPermission(org.motechproject.security.domain.MotechPermission) PermissionDto(org.motechproject.security.model.PermissionDto) Test(org.junit.Test)

Example 10 with MotechPermission

use of org.motechproject.security.domain.MotechPermission in project motech by motech.

the class MotechPermissionServiceBundleIT method shouldDeletePermissions.

@Test
public void shouldDeletePermissions() {
    permissionsDataService.create(new MotechPermission("testPermission", "testBundle"));
    MotechPermission permission = permissionsDataService.findByPermissionName("testPermission");
    assertNotNull(permission);
    setUpSecurityContextForDefaultUser(PermissionNames.MANAGE_ROLE_AND_PERMISSION_PERMISSION);
    // When
    permissionService.deletePermission(permission.getPermissionName());
    // Then
    List<MotechPermission> permissions = permissionsDataService.retrieveAll();
    assertTrue("There shouldn't be any permissions, but found: " + permissions.size(), permissions.isEmpty());
}
Also used : MotechPermission(org.motechproject.security.domain.MotechPermission) Test(org.junit.Test)

Aggregations

MotechPermission (org.motechproject.security.domain.MotechPermission)10 Test (org.junit.Test)7 PermissionDto (org.motechproject.security.model.PermissionDto)4 Transactional (org.springframework.transaction.annotation.Transactional)2 LinkedList (java.util.LinkedList)1 PostConstruct (javax.annotation.PostConstruct)1 RoleDto (org.motechproject.security.model.RoleDto)1