/** * Imports UserExport objects into the platform as users. * * @param users * @return A map of role names to list of users in that role */ protected Map<String, List<String>> importUsers(List<UserExport> users) { Map<String, List<String>> roleToUserMap = new HashMap<>(); IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class); ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true); if (users != null && roleDao != null) { for (UserExport user : users) { String password = user.getPassword(); log.debug("Importing user: "******"USER.Already.Exists", user.getUsername())); try { if (isOverwriteFile()) { // set the roles, maybe they changed roleDao.setUserRoles(tenant, user.getUsername(), userRoles); // set the password just in case it changed roleDao.setPassword(tenant, user.getUsername(), password); } } catch (Exception ex) { // couldn't set the roles or password either log.debug("Failed to set roles or password for existing user on import", ex); } } catch (Exception e) { log.error(Messages.getInstance().getString("ERROR.CreatingUser", user.getUsername())); } importUserSettings(user); } } return roleToUserMap; }
@Test public void testGetAuthoritiesForUser() { loginAsRepositoryAdmin(); ITenant systemTenant = tenantManager.createTenant( null, ServerRepositoryPaths.getPentahoRootFolderName(), tenantAdminAuthorityName, tenantAuthenticatedAuthorityName, "Anonymous"); userRoleDao.createUser( systemTenant, sysAdminUserName, "password", "", new String[] {tenantAdminAuthorityName}); login( sysAdminUserName, systemTenant, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); ITenant mainTenant_1 = tenantManager.createTenant( systemTenant, MAIN_TENANT_1, tenantAdminAuthorityName, tenantAuthenticatedAuthorityName, "Anonymous"); userRoleDao.createUser( mainTenant_1, "admin", "password", "", new String[] {tenantAdminAuthorityName}); ITenant mainTenant_2 = tenantManager.createTenant( systemTenant, MAIN_TENANT_2, tenantAdminAuthorityName, tenantAuthenticatedAuthorityName, "Anonymous"); userRoleDao.createUser( mainTenant_2, "admin", "password", "", new String[] {tenantAdminAuthorityName}); login( "admin", mainTenant_1, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); IPentahoUser pentahoUser = userRoleDao.createUser(mainTenant_1, USER_2, PASSWORD_2, USER_DESCRIPTION_2, null); pentahoUser = userRoleDao.createUser( null, tenantedUserNameUtils.getPrincipleId(mainTenant_1, USER_3), PASSWORD_3, USER_DESCRIPTION_3, null); pentahoUser = userRoleDao.createUser(null, USER_4, PASSWORD_4, USER_DESCRIPTION_4, null); logout(); login( "admin", mainTenant_2, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); pentahoUser = userRoleDao.createUser(mainTenant_2, USER_5, PASSWORD_5, USER_DESCRIPTION_5, null); pentahoUser = userRoleDao.createUser( null, tenantedUserNameUtils.getPrincipleId(mainTenant_2, USER_6), PASSWORD_6, USER_DESCRIPTION_6, null); logout(); login( "admin", mainTenant_1, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); IPentahoRole pentahoRole = userRoleDao.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null); pentahoRole = userRoleDao.createRole( null, tenantedRoleNameUtils.getPrincipleId(mainTenant_1, ROLE_2), ROLE_DESCRIPTION_2, null); pentahoRole = userRoleDao.createRole(null, ROLE_3, ROLE_DESCRIPTION_3, null); logout(); login( "admin", mainTenant_2, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); pentahoRole = userRoleDao.createRole(mainTenant_2, ROLE_4, ROLE_DESCRIPTION_4, null); userRoleDao.setUserRoles(null, USER_5, new String[] {ROLE_4}); userRoleDao.setUserRoles( null, tenantedUserNameUtils.getPrincipleId(mainTenant_2, USER_6), new String[] {ROLE_4}); logout(); login( "admin", mainTenant_1, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); userRoleDao.setUserRoles(mainTenant_1, USER_2, new String[] {ROLE_1, ROLE_2, ROLE_3}); List<String> systemRoles = Arrays.asList(new String[] {"Admin"}); try { userRoleDao.setUserRoles(mainTenant_1, USER_3, new String[] {ROLE_2, ROLE_3, ROLE_4}); fail("Exception should be thrown"); } catch (Throwable th) { assertNotNull(th); } try { userRoleDao.setUserRoles(mainTenant_1, USER_4, new String[] {ROLE_2, ROLE_4}); fail("Exception should be thrown"); } catch (Throwable th) { assertNotNull(th); } UserRoleDaoUserDetailsService userDetailsService = new UserRoleDaoUserDetailsService(); userDetailsService.setUserRoleDao(userRoleDao); userDetailsService.setDefaultRole(tenantAuthenticatedAuthorityName); List<String> extraRoles = Arrays.asList(new String[] {"Authenticated", "Anonymous"}); String adminRole = "Admin"; UserRoleDaoUserRoleListService service = new UserRoleDaoUserRoleListService( userRoleDao, userDetailsService, tenantedUserNameUtils, systemRoles, extraRoles, adminRole); service.setUserDetailsService(userDetailsService); logout(); login( "admin", mainTenant_1, new String[] {tenantAdminAuthorityName, tenantAuthenticatedAuthorityName}); List<String> rolesForUser_2 = service.getRolesForUser(mainTenant_1, USER_2); List<String> rolesForUser_2_1 = service.getRolesForUser(null, USER_2); List<String> rolesForUser_2_1_1 = service.getRolesForUser(null, tenantedUserNameUtils.getPrincipleId(mainTenant_1, USER_2)); List<String> rolesForUser_3 = service.getRolesForUser(mainTenant_1, USER_3); List<String> rolesForUser_4 = service.getRolesForUser(mainTenant_1, USER_4); assertTrue(rolesForUser_2.size() == 4); assertTrue(rolesForUser_2_1.size() == 4); assertTrue(rolesForUser_2_1_1.size() == 4); assertTrue(rolesForUser_3.size() == 3); assertTrue(rolesForUser_4.size() == 2); cleanupUserAndRoles("admin", mainTenant_1); cleanupUserAndRoles("admin", mainTenant_2); cleanupUserAndRoles(sysAdminUserName, systemTenant); }