use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.
the class SolutionImportHandler method importUsers.
/**
* 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.getUsername());
// map the user to the roles he/she is in
for (String role : user.getRoles()) {
List<String> userList;
if (!roleToUserMap.containsKey(role)) {
userList = new ArrayList<>();
roleToUserMap.put(role, userList);
} else {
userList = roleToUserMap.get(role);
}
userList.add(user.getUsername());
}
String[] userRoles = user.getRoles().toArray(new String[] {});
try {
roleDao.createUser(tenant, user.getUsername(), password, null, userRoles);
} catch (AlreadyExistsException e) {
// it's ok if the user already exists, it is probably a default user
log.info(Messages.getInstance().getString("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;
}
use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.
the class MockUserRoleDao method createRole.
public IPentahoRole createRole(ITenant tenant, String roleName, String description, String[] memberUserNames) throws AlreadyExistsException, UncategorizedUserRoleDaoException {
if (tenant == null) {
tenant = getTenant(roleName, false);
roleName = getPrincipalName(roleName, false);
}
if (tenant == null || tenant.getId() == null) {
tenant = getCurrentTenant();
}
addTenant(tenant);
MockPentahoRole role = null;
HashSet<IPentahoRole> set = tenantRoles.get(tenant);
if (set != null) {
for (IPentahoRole iRole : set) {
if (iRole.getName() == roleName) {
role = (MockPentahoRole) iRole;
}
}
}
if (role == null) {
role = new MockPentahoRole(tenant, roleName, description);
}
if (!tenantRoles.get(tenant).contains(role)) {
tenantRoles.get(tenant).add(role);
roleMembers.put(role, new HashSet<IPentahoUser>());
} else {
throw new AlreadyExistsException(roleName.toString());
}
setRoleMembers(tenant, roleName, memberUserNames);
return role;
}
use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.
the class SolutionImportHandlerTest method testImportRoles_roleAlreadyExists_overwriteFalse.
@Test
public void testImportRoles_roleAlreadyExists_overwriteFalse() throws Exception {
String roleName = "ADMIN";
List<String> permissions = new ArrayList<String>();
RoleExport role = new RoleExport();
role.setRolename(roleName);
role.setPermission(permissions);
List<RoleExport> roles = new ArrayList<>();
roles.add(role);
Map<String, List<String>> roleToUserMap = new HashMap<>();
final List<String> adminUsers = new ArrayList<>();
adminUsers.add("admin");
adminUsers.add("root");
roleToUserMap.put(roleName, adminUsers);
String[] userStrings = adminUsers.toArray(new String[] {});
Mockito.when(userRoleDao.createRole(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(userStrings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
importHandler.setOverwriteFile(false);
importHandler.importRoles(roles, roleToUserMap);
Mockito.verify(userRoleDao).createRole(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString(), Mockito.any(userStrings.getClass()));
// even if the roles exists, make sure we set the permissions on it Mockito.anyway... they might have changed
Mockito.verify(roleAuthorizationPolicyRoleBindingDao, Mockito.never()).setRoleBindings(Mockito.any(ITenant.class), Mockito.eq(roleName), Mockito.eq(permissions));
}
use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.
the class SolutionImportHandlerTest method testImportUsers_userAlreadyExists.
@Test
public void testImportUsers_userAlreadyExists() throws Exception {
List<UserExport> users = new ArrayList<>();
UserExport user = new UserExport();
user.setUsername("scrum master");
user.setRole("coder");
user.setPassword("password");
users.add(user);
String[] strings = {};
Mockito.when(userRoleDao.createUser(Mockito.any(ITenant.class), Mockito.eq("scrum master"), Mockito.anyString(), Mockito.anyString(), Mockito.any(strings.getClass()))).thenThrow(new AlreadyExistsException("already there"));
importHandler.setOverwriteFile(true);
Map<String, List<String>> rolesToUsers = importHandler.importUsers(users);
Assert.assertEquals(1, rolesToUsers.size());
Assert.assertEquals("scrum master", rolesToUsers.get("coder").get(0));
Mockito.verify(userRoleDao).createUser(Mockito.any(ITenant.class), Mockito.eq("scrum master"), Mockito.anyString(), Mockito.anyString(), Mockito.any(strings.getClass()));
// should set the password or roles explicitly if the createUser failed
Mockito.verify(userRoleDao).setUserRoles(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.any(strings.getClass()));
Mockito.verify(userRoleDao).setPassword(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString());
}
use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.
the class UserRoleDaoEncodeIT method testCreateUser.
@Test
public void testCreateUser() throws Exception {
loginAsRepositoryAdmin();
systemTenant = tenantManager.createTenant(null, ServerRepositoryPaths.getPentahoRootFolderName(), adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(systemTenant, sysAdminUserName, "password", "", new String[] { adminRoleName });
login(sysAdminUserName, systemTenant, new String[] { adminRoleName, authenticatedRoleName });
mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(mainTenant_1, "admin", "password", "", new String[] { adminRoleName });
mainTenant_2 = tenantManager.createTenant(systemTenant, MAIN_TENANT_2, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(mainTenant_2, "admin", "password", "", new String[] { adminRoleName });
subTenant1_1 = tenantManager.createTenant(mainTenant_1, SUB_TENANT1_1, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(subTenant1_1, "admin", "password", "", new String[] { adminRoleName });
subTenant1_2 = tenantManager.createTenant(mainTenant_1, SUB_TENANT1_2, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(subTenant1_2, "admin", "password", "", new String[] { adminRoleName });
subTenant2_1 = tenantManager.createTenant(mainTenant_2, SUB_TENANT2_1, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(subTenant2_1, "admin", "password", "", new String[] { adminRoleName });
subTenant2_2 = tenantManager.createTenant(mainTenant_2, SUB_TENANT2_2, adminRoleName, authenticatedRoleName, "Anonymous");
userRoleDaoProxy.createUser(subTenant2_2, "admin", "password", "", new String[] { adminRoleName });
login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
List<IPentahoUser> users = userRoleDaoProxy.getUsers(mainTenant_1);
IPentahoUser pentahoUser = userRoleDaoProxy.createUser(mainTenant_1, USER_2, PASSWORD_2, USER_DESCRIPTION_2, null);
pentahoUser = userRoleDaoProxy.getUser(mainTenant_1, USER_2);
assertEquals(pentahoUser.getTenant(), mainTenant_1);
assertEquals(pentahoUser.getUsername(), USER_2);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_2);
assertEquals(pentahoUser.isEnabled(), true);
logout();
/*login( "admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName } );
try {
pentahoUser = userRoleDaoProxy.createUser( mainTenant_1, USER_2, PASSWORD_2,
USER_DESCRIPTION_2, null );
fail( "Exception not thrown" );
} catch ( Throwable th ) {
assertNotNull( th );
}
logout();*/
login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
users = userRoleDaoProxy.getUsers(mainTenant_1);
int DEFAULT_USER_COUNT = 1;
assertTrue(users.size() == 1 + DEFAULT_USER_COUNT);
boolean foundUser = false;
for (IPentahoUser user : users) {
if (user.getUsername().equals(USER_2)) {
foundUser = true;
pentahoUser = user;
break;
}
}
assertTrue(foundUser);
assertEquals(pentahoUser.getTenant(), mainTenant_1);
assertEquals(pentahoUser.getUsername(), USER_2);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_2);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
pentahoUser = userRoleDaoProxy.createUser(mainTenant_2, USER_2, PASSWORD_2, USER_DESCRIPTION_2, null);
logout();
login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
try {
pentahoUser = userRoleDaoProxy.createUser(mainTenant_2, USER_2, PASSWORD_2, USER_DESCRIPTION_2, null);
fail("Exception not thrown");
} catch (Throwable th) {
assertNotNull(th);
}
logout();
login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
pentahoUser = userRoleDaoProxy.getUser(mainTenant_2, USER_2);
assertEquals(pentahoUser.getTenant(), mainTenant_2);
assertEquals(pentahoUser.getUsername(), USER_2);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_2);
assertEquals(pentahoUser.isEnabled(), true);
users = userRoleDaoProxy.getUsers(mainTenant_2);
assertTrue(users.size() == 1 + DEFAULT_USER_COUNT);
foundUser = false;
for (IPentahoUser user : users) {
if (user.getUsername().equals(USER_2)) {
foundUser = true;
pentahoUser = user;
break;
}
}
assertTrue(foundUser);
assertEquals(pentahoUser.getTenant(), mainTenant_2);
assertEquals(pentahoUser.getUsername(), USER_2);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_2);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
pentahoUser = userRoleDaoProxy.createUser(null, USER_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath(), PASSWORD_3, USER_DESCRIPTION_3, null);
pentahoUser = userRoleDaoProxy.getUser(null, USER_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
assertEquals(pentahoUser.getTenant(), subTenant2_1);
assertEquals(pentahoUser.getUsername(), USER_3);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_3);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
try {
pentahoUser = userRoleDaoProxy.createUser(subTenant2_1, USER_1, PASSWORD_1, USER_DESCRIPTION_1, null);
fail("Exception not thrown");
} catch (Throwable th) {
assertNotNull(th);
}
logout();
login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
users = userRoleDaoProxy.getUsers(subTenant2_1);
assertTrue(users.size() == 1 + DEFAULT_USER_COUNT);
foundUser = false;
for (IPentahoUser user : users) {
if (user.getUsername().equals(USER_3)) {
foundUser = true;
pentahoUser = user;
break;
}
}
assertTrue(foundUser);
assertEquals(pentahoUser.getTenant(), subTenant2_1);
assertEquals(pentahoUser.getUsername(), USER_3);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_3);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
pentahoUser = userRoleDaoProxy.createUser(null, USER_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath(), PASSWORD_4, USER_DESCRIPTION_4, null);
pentahoUser = userRoleDaoProxy.getUser(null, USER_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
assertEquals(pentahoUser.getTenant(), subTenant1_1);
assertEquals(pentahoUser.getUsername(), USER_4);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_4);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
try {
pentahoUser = userRoleDaoProxy.createUser(subTenant1_1, USER_1, PASSWORD_1, USER_DESCRIPTION_1, null);
fail("Exception not thrown");
} catch (Throwable th) {
assertNotNull(th);
}
logout();
login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
users = userRoleDaoProxy.getUsers(subTenant1_1);
assertTrue(users.size() == 1 + DEFAULT_USER_COUNT);
foundUser = false;
for (IPentahoUser user : users) {
if (user.getUsername().equals(USER_4)) {
foundUser = true;
pentahoUser = user;
break;
}
}
assertTrue(foundUser);
assertEquals(pentahoUser.getTenant(), subTenant1_1);
assertEquals(pentahoUser.getUsername(), USER_4);
assertEquals(pentahoUser.getDescription(), USER_DESCRIPTION_4);
assertEquals(pentahoUser.isEnabled(), true);
logout();
login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
try {
pentahoUser = userRoleDaoProxy.createUser(mainTenant_1, USER_1, PASSWORD_1, USER_DESCRIPTION_1, null);
fail("Exception not thrown");
} catch (AlreadyExistsException e) {
// Expected exception
}
try {
pentahoUser = userRoleDaoProxy.createUser(null, USER_1 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + mainTenant_1.getRootFolderAbsolutePath(), PASSWORD_1, USER_DESCRIPTION_1, null);
fail("Exception not thrown");
} catch (AlreadyExistsException e) {
// Expected exception
}
}
Aggregations