Search in sources :

Example 6 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleDaoEncodeIT method testCreateRole.

@Test
public void testCreateRole() 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 });
    IPentahoRole pentahoRole;
    userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_1, ROLE_1);
    assertEquals(pentahoRole.getTenant(), mainTenant_1);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_1);
    int DEFAULT_ROLE_COUNT = 3;
    List<IPentahoRole> roles = userRoleDaoProxy.getRoles(mainTenant_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_1) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), mainTenant_1);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_1);
    logout();
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
    pentahoRole = userRoleDaoProxy.createRole(mainTenant_2, ROLE_1, ROLE_DESCRIPTION_2, null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_2, ROLE_1);
    assertEquals(pentahoRole.getTenant(), mainTenant_2);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_2);
    roles = userRoleDaoProxy.getRoles(mainTenant_2);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_1) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), mainTenant_2);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_2);
    logout();
    login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(mainTenant_2, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    userRoleDaoProxy.createRole(null, ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath(), ROLE_DESCRIPTION_3, null);
    pentahoRole = userRoleDaoProxy.getRole(null, ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getTenant(), subTenant2_1);
    assertEquals(pentahoRole.getName(), ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_3);
    roles = userRoleDaoProxy.getRoles(subTenant2_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath()) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), subTenant2_1);
    assertEquals(pentahoRole.getName(), ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_3);
    logout();
    login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(subTenant2_1, ROLE_3, ROLE_DESCRIPTION_3, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
    pentahoRole = userRoleDaoProxy.createRole(null, ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath(), ROLE_DESCRIPTION_4, null);
    pentahoRole = userRoleDaoProxy.getRole(null, ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getTenant(), subTenant1_1);
    assertEquals(pentahoRole.getName(), ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_4);
    roles = userRoleDaoProxy.getRoles(subTenant1_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath()) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), subTenant1_1);
    assertEquals(pentahoRole.getName(), ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_4);
    logout();
    login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(subTenant1_1, ROLE_3, ROLE_DESCRIPTION_3, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (AlreadyExistsException e) {
    // Expected exception
    }
    logout();
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) Test(org.junit.Test)

Example 7 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleDaoIT method testCreateRole.

@Test
public void testCreateRole() 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 });
    IPentahoRole pentahoRole;
    userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_1, ROLE_1);
    assertEquals(pentahoRole.getTenant(), mainTenant_1);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_1);
    int DEFAULT_ROLE_COUNT = 3;
    List<IPentahoRole> roles = userRoleDaoProxy.getRoles(mainTenant_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_1) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), mainTenant_1);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_1);
    logout();
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
    pentahoRole = userRoleDaoProxy.createRole(mainTenant_2, ROLE_1, ROLE_DESCRIPTION_2, null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_2, ROLE_1);
    assertEquals(pentahoRole.getTenant(), mainTenant_2);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_2);
    roles = userRoleDaoProxy.getRoles(mainTenant_2);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_1) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), mainTenant_2);
    assertEquals(pentahoRole.getName(), ROLE_1);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_2);
    logout();
    login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(mainTenant_2, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    userRoleDaoProxy.createRole(null, ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath(), ROLE_DESCRIPTION_3, null);
    pentahoRole = userRoleDaoProxy.getRole(null, ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getTenant(), subTenant2_1);
    assertEquals(pentahoRole.getName(), ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_3);
    roles = userRoleDaoProxy.getRoles(subTenant2_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath()) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), subTenant2_1);
    assertEquals(pentahoRole.getName(), ROLE_3 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant2_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_3);
    logout();
    login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(subTenant2_1, ROLE_3, ROLE_DESCRIPTION_3, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", subTenant1_1, new String[] { adminRoleName, authenticatedRoleName });
    pentahoRole = userRoleDaoProxy.createRole(null, ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath(), ROLE_DESCRIPTION_4, null);
    pentahoRole = userRoleDaoProxy.getRole(null, ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getTenant(), subTenant1_1);
    assertEquals(pentahoRole.getName(), ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_4);
    roles = userRoleDaoProxy.getRoles(subTenant1_1);
    assertTrue(roles.size() == 1 + DEFAULT_ROLE_COUNT);
    for (IPentahoRole role : roles) {
        if (role.getName() == ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath()) {
            pentahoRole = role;
        }
    }
    assertEquals(pentahoRole.getTenant(), subTenant1_1);
    assertEquals(pentahoRole.getName(), ROLE_4 + DefaultTenantedPrincipleNameResolver.DEFAULT_DELIMETER + subTenant1_1.getRootFolderAbsolutePath());
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_4);
    logout();
    login("admin", subTenant2_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        pentahoRole = userRoleDaoProxy.createRole(subTenant1_1, ROLE_3, ROLE_DESCRIPTION_3, null);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    try {
        userRoleDaoProxy.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
        fail("Exception not thrown");
    } catch (AlreadyExistsException e) {
    // Expected exception
    }
    logout();
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) Test(org.junit.Test)

Example 8 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class UserRoleDaoIT 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
    }
}
Also used : AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) Test(org.junit.Test)

Example 9 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class SolutionImportHandler method importRoles.

protected void importRoles(List<RoleExport> roles, Map<String, List<String>> roleToUserMap) {
    IUserRoleDao roleDao = PentahoSystem.get(IUserRoleDao.class);
    ITenant tenant = new Tenant("/pentaho/" + TenantUtils.getDefaultTenant(), true);
    IRoleAuthorizationPolicyRoleBindingDao roleBindingDao = PentahoSystem.get(IRoleAuthorizationPolicyRoleBindingDao.class);
    Set<String> existingRoles = new HashSet<>();
    if (roles != null) {
        for (RoleExport role : roles) {
            log.debug("Importing role: " + role.getRolename());
            try {
                List<String> users = roleToUserMap.get(role.getRolename());
                String[] userarray = users == null ? new String[] {} : users.toArray(new String[] {});
                IPentahoRole role1 = roleDao.createRole(tenant, role.getRolename(), null, userarray);
            } catch (AlreadyExistsException e) {
                existingRoles.add(role.getRolename());
                // it's ok if the role already exists, it is probably a default role
                log.info(Messages.getInstance().getString("ROLE.Already.Exists", role.getRolename()));
            }
            try {
                if (existingRoles.contains(role.getRolename())) {
                    // Only update an existing role if the overwrite flag is set
                    if (isOverwriteFile()) {
                        roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                    }
                } else {
                    // Always write a roles permissions that were not previously existing
                    roleBindingDao.setRoleBindings(tenant, role.getRolename(), role.getPermissions());
                }
            } catch (Exception e) {
                log.info(Messages.getInstance().getString("ERROR.SettingRolePermissions", role.getRolename()), e);
            }
        }
    }
}
Also used : IRoleAuthorizationPolicyRoleBindingDao(org.pentaho.platform.security.policy.rolebased.IRoleAuthorizationPolicyRoleBindingDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) RoleExport(org.pentaho.platform.plugin.services.importexport.RoleExport) HashSet(java.util.HashSet)

Example 10 with AlreadyExistsException

use of org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException in project pentaho-platform by pentaho.

the class SolutionImportHandlerTest method testImportUsers_userAlreadyExists_overwriteFalse.

@Test
public void testImportUsers_userAlreadyExists_overwriteFalse() 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(false);
    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, Mockito.never()).setUserRoles(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.any(strings.getClass()));
    Mockito.verify(userRoleDao, Mockito.never()).setPassword(Mockito.any(ITenant.class), Mockito.anyString(), Mockito.anyString());
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UserExport(org.pentaho.platform.plugin.services.importexport.UserExport) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)14 Test (org.junit.Test)11 ITenant (org.pentaho.platform.api.mt.ITenant)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)4 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)4 HashMap (java.util.HashMap)3 RoleExport (org.pentaho.platform.plugin.services.importexport.RoleExport)3 UserExport (org.pentaho.platform.plugin.services.importexport.UserExport)3 IOException (java.io.IOException)2 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)2 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)2 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)2 IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)2 Tenant (org.pentaho.platform.core.mt.Tenant)2 IUserRoleWebService (org.pentaho.platform.security.userroledao.ws.IUserRoleWebService)2 UserRoleWebService (org.pentaho.platform.security.userroledao.ws.UserRoleWebService)2 HashSet (java.util.HashSet)1 WebApplicationException (javax.ws.rs.WebApplicationException)1