Search in sources :

Example 16 with IPentahoRole

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

the class UserRoleDaoIT method testUpdateRole.

@Test
public void testUpdateRole() 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 });
    login("admin", mainTenant_1, new String[] { adminRoleName, authenticatedRoleName });
    IPentahoRole pentahoRole = userRoleDaoProxy.createRole(mainTenant_1, ROLE_5, ROLE_DESCRIPTION_5, null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_1, ROLE_5);
    assertEquals(pentahoRole.getDescription(), ROLE_DESCRIPTION_5);
    String changedDescription1 = ROLE_DESCRIPTION_5 + "change1";
    userRoleDaoProxy.setRoleDescription(mainTenant_1, ROLE_5, changedDescription1);
    String role_delim = ((DefaultTenantedPrincipleNameResolver) tenantedRoleNameUtils).getDelimeter();
    pentahoRole = userRoleDaoProxy.getRole(null, ROLE_5 + role_delim + mainTenant_1.getRootFolderAbsolutePath());
    assertNotNull(pentahoRole);
    assertEquals(changedDescription1, pentahoRole.getDescription());
    String changedDescription2 = ROLE_DESCRIPTION_5 + "change2";
    userRoleDaoProxy.setRoleDescription(null, ROLE_5 + role_delim + mainTenant_1.getRootFolderAbsolutePath(), changedDescription2);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_1, ROLE_5);
    assertEquals(changedDescription2, pentahoRole.getDescription());
    userRoleDaoProxy.setRoleDescription(null, ROLE_5 + role_delim + mainTenant_1.getRootFolderAbsolutePath(), null);
    pentahoRole = userRoleDaoProxy.getRole(mainTenant_1, ROLE_5);
    assertNull(pentahoRole.getDescription());
    try {
        userRoleDaoProxy.setRoleDescription(null, null, changedDescription2);
        fail("Exception not thrown");
    } catch (Exception ex) {
        // Expected exception
        assertNotNull(ex);
    }
    try {
        userRoleDaoProxy.setRoleDescription(mainTenant_1, UNKNOWN_ROLE, changedDescription2);
        fail("Exception not thrown");
    } catch (NotFoundException ex) {
        // Expected exception
        assertNotNull(ex);
    }
    logout();
    login("admin", mainTenant_2, new String[] { adminRoleName, authenticatedRoleName });
    try {
        changedDescription1 = ROLE_DESCRIPTION_5 + "change1";
        userRoleDaoProxy.setRoleDescription(mainTenant_1, ROLE_5, changedDescription1);
        fail("Exception not thrown");
    } catch (Throwable th) {
        assertNotNull(th);
    }
    logout();
}
Also used : NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) DefaultTenantedPrincipleNameResolver(org.pentaho.platform.security.userroledao.DefaultTenantedPrincipleNameResolver) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) AlreadyExistsException(org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException) NotFoundException(org.pentaho.platform.api.engine.security.userroledao.NotFoundException) AccessControlException(javax.jcr.security.AccessControlException) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) Test(org.junit.Test)

Example 17 with IPentahoRole

use of org.pentaho.platform.api.engine.security.userroledao.IPentahoRole 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 18 with IPentahoRole

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

the class UserRoleDaoService method removeRolesFromUser.

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();
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) HashSet(java.util.HashSet)

Example 19 with IPentahoRole

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

the class UserRoleDaoResource method assignAllRolesToUser.

/**
 * Associate all roles to the selected user
 *
 * @param tenantPath (tenant path where the user exist, null of empty string assumes default tenant)
 * @param userName   (username)
 * @return
 */
@PUT
@Path("/assignAllRolesToUser")
@Consumes({ MediaType.WILDCARD })
@Facet(name = "Unsupported")
public Response assignAllRolesToUser(@QueryParam("tenant") String tenantPath, @QueryParam("userName") String userName) {
    IUserRoleDao roleDao = getUserRoleDao();
    Set<String> assignedRoles = new HashSet<String>();
    for (IPentahoRole pentahoRole : roleDao.getRoles(getTenant(tenantPath))) {
        assignedRoles.add(pentahoRole.getName());
    }
    roleDao.setUserRoles(getTenant(tenantPath), userName, assignedRoles.toArray(new String[0]));
    if (userName.equals(getSession().getName())) {
        updateRolesForCurrentSession();
    }
    return Response.ok().build();
}
Also used : IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT) Facet(org.codehaus.enunciate.Facet)

Example 20 with IPentahoRole

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

the class UserRoleDaoServiceTest method testGetRoles.

@Test
public void testGetRoles() throws Exception {
    List<IPentahoRole> roleList = new ArrayList<>();
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getRoles()).thenReturn(roleList);
    PentahoSystem.registerObject(roleDao);
    IPentahoRole role = mock(IPentahoRole.class);
    when(role.getName()).thenReturn("testRole");
    roleList.add(role);
    RoleListWrapper wrapRoleList = new RoleListWrapper(roleList);
    assertEquals(wrapRoleList.getRoles(), userRoleService.getRoles().getRoles());
}
Also used : RoleListWrapper(org.pentaho.platform.web.http.api.resources.RoleListWrapper) ArrayList(java.util.ArrayList) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Aggregations

IPentahoRole (org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)48 Test (org.junit.Test)19 ArrayList (java.util.ArrayList)16 IPentahoUser (org.pentaho.platform.api.engine.security.userroledao.IPentahoUser)13 ITenant (org.pentaho.platform.api.mt.ITenant)12 IUserRoleDao (org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao)10 AlreadyExistsException (org.pentaho.platform.api.engine.security.userroledao.AlreadyExistsException)8 HashSet (java.util.HashSet)7 Matchers.anyString (org.mockito.Matchers.anyString)6 NotFoundException (org.pentaho.platform.api.engine.security.userroledao.NotFoundException)6 DefaultTenantedPrincipleNameResolver (org.pentaho.platform.security.userroledao.DefaultTenantedPrincipleNameResolver)6 IOException (java.io.IOException)5 RepositoryException (javax.jcr.RepositoryException)5 AccessControlException (javax.jcr.security.AccessControlException)4 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)4 PentahoRole (org.pentaho.platform.security.userroledao.PentahoRole)4 BeansException (org.springframework.beans.BeansException)4 StringTokenizer (java.util.StringTokenizer)2 Group (org.apache.jackrabbit.api.security.user.Group)2 UncategorizedUserRoleDaoException (org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException)2