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);
}
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);
}
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);
}
}
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());
}
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();
}
Aggregations