UserRoleDaoService { public void removeRolesFromUser( String userName, String roleNames ) throws NotFoundException, UncategorizedUserRoleDaoException, SecurityException { if ( canAdminister() ) { StringTokenizer tokenizer = new StringTokenizer( roleNames, "\t" ); Set<String> assignedRoles = new HashSet<>(); ITenant tenant = TenantUtils.getCurrentTenant(); for ( IPentahoRole pentahoRole : getRoleDao().getUserRoles( tenant, userName ) ) { assignedRoles.add( pentahoRole.getName() ); } while ( tokenizer.hasMoreTokens() ) { assignedRoles.remove( tokenizer.nextToken() ); } getRoleDao().setUserRoles( tenant, userName, assignedRoles.toArray( new String[assignedRoles.size()] ) ); } else { throw new SecurityException(); } }  UserListWrapper getUsers(); RoleListWrapper getRolesForUser( String user ); void assignRolesToUser( String userName, String roleNames ); void removeRolesFromUser( String userName, String roleNames ); void createRole( String roleName ); RoleListWrapper getRoles(); UserListWrapper getRoleMembers( String roleName ); void createUser( User user ); void changeUserPassword( final String userName, final String newPass, String oldPass ); void deleteUsers( String userNames ); void deleteRoles( String roleNames ); SystemRolesMap getRoleBindingStruct( String locale ); void setLogicalRoles( LogicalRoleAssignments roleAssignments ); void updatePassword( User user, String administratorPassword ); void updatePassword( User user ); static final String PUC_USER_PASSWORD_LENGTH; static final String PUC_USER_PASSWORD_REQUIRE_SPECIAL_CHARACTER; }
@Test public void testRemoveRoleFromUser() { String userName = "testUser"; String roleNames = "Power User\tBusiness User\t"; setupMockSessionUser( SESSION_USER_NAME, true ); IPentahoSession session = mock( IPentahoSession.class ); when( session.getAttribute( IPentahoSession.TENANT_ID_KEY ) ).thenReturn( "testTenantPath" ); PentahoSessionHolder.setSession( session ); IPentahoRole ceoRole = mock( IPentahoRole.class ); when( ceoRole.getName() ).thenReturn( "ceo" ); IPentahoRole ctoRole = mock( IPentahoRole.class ); when( ctoRole.getName() ).thenReturn( "cto" ); IPentahoRole powerUserRole = mock( IPentahoRole.class ); when( powerUserRole.getName() ).thenReturn( "Power User" ); IPentahoRole businessUserRole = mock( IPentahoRole.class ); when( businessUserRole.getName() ).thenReturn( "Business User" ); List<IPentahoRole> roleList = new ArrayList<>(); roleList.add( ceoRole ); roleList.add( ctoRole ); roleList.add( powerUserRole ); roleList.add( businessUserRole ); IUserRoleDao roleDao = mock( IUserRoleDao.class ); when( roleDao.getUserRoles( any( ITenant.class ), anyString() ) ).thenReturn( roleList ); PentahoSystem.registerObject( roleDao ); userRoleService.removeRolesFromUser( userName, roleNames ); verify( roleDao ).setUserRoles( any( ITenant.class ), anyString(), argThat( new UnorderedArrayMatcher( new String[] { "ceo", "cto" } ) ) ); }
@Test( expected = SecurityException.class ) public void testRemoveRoleFromUserSecurityException() { String userName = "testUser"; String roleNames = "Power User\tBusiness User\t"; setupMockSessionUser( SESSION_USER_NAME, false ); userRoleService.removeRolesFromUser( userName, roleNames ); }
@Test( expected = NotFoundException.class ) public void testRemoveRoleFromUserNotFoundException() { String userName = "testUser"; String roleNames = "Power User\tBusiness User\t"; setupMockSessionUser( SESSION_USER_NAME, true ); IPentahoSession session = mock( IPentahoSession.class ); when( session.getAttribute( IPentahoSession.TENANT_ID_KEY ) ).thenReturn( "testTenantPath" ); PentahoSessionHolder.setSession( session ); IUserRoleDao roleDao = mock( IUserRoleDao.class ); when( roleDao.getUserRoles( any( ITenant.class ), anyString() ) ).thenThrow( new NotFoundException( "expectedTestException" ) ); PentahoSystem.registerObject( roleDao ); userRoleService.removeRolesFromUser( userName, roleNames ); }
@Test( expected = UncategorizedUserRoleDaoException.class ) public void testRemoveRolesFromUserUncategorizedUserRoleDaoException() throws UserRoleListService.UnauthorizedException { String userName = "testUser"; String roleNames = "Power User\tBusiness User\t"; setupMockSessionUser( SESSION_USER_NAME, true ); IPentahoSession session = mock( IPentahoSession.class ); when( session.getAttribute( IPentahoSession.TENANT_ID_KEY ) ).thenReturn( "testTenantPath" ); PentahoSessionHolder.setSession( session ); IUserRoleDao roleDao = mock( IUserRoleDao.class ); when( roleDao.getUserRoles( any( ITenant.class ), anyString() ) ).thenThrow( new UncategorizedUserRoleDaoException( "expectedTestException" ) ); PentahoSystem.registerObject( roleDao ); userRoleService.removeRolesFromUser( userName, roleNames ); }