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