Search in sources :

Example 26 with IPentahoRole

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

the class UserRoleDaoServiceTest method testDeleteRole.

@Test
public void testDeleteRole() {
    String roles = "role1\trole2\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    IPentahoRole role = mock(IPentahoRole.class);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getRole(any(ITenant.class), anyString())).thenReturn(role);
    PentahoSystem.registerObject(roleDao);
    userRoleService.deleteRoles(roles);
    verify(roleDao, times(2)).deleteRole(any(IPentahoRole.class));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) Matchers.anyString(org.mockito.Matchers.anyString) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 27 with IPentahoRole

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

the class UserRoleDaoServiceTest method testGetRolesForUser.

@Test
public void testGetRolesForUser() throws Exception {
    List<IPentahoRole> roleList = new ArrayList<>();
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenReturn(roleList);
    PentahoSystem.registerObject(roleDao);
    RoleListWrapper wrapRoleList = new RoleListWrapper(roleList);
    IPentahoSession session = mock(IPentahoSession.class);
    String tenantPath = "testPath";
    when(session.getAttribute(IPentahoSession.TENANT_ID_KEY)).thenReturn(tenantPath);
    PentahoSessionHolder.setSession(session);
    setupMockSessionUser(SESSION_USER_NAME, true);
    assertEquals(wrapRoleList.getRoles(), userRoleService.getRolesForUser("admin").getRoles());
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) RoleListWrapper(org.pentaho.platform.web.http.api.resources.RoleListWrapper) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) Matchers.anyString(org.mockito.Matchers.anyString) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 28 with IPentahoRole

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

the class UserRoleDaoServiceTest method testAssignRoleToUser.

@Test
public void testAssignRoleToUser() throws UserRoleListService.UnauthorizedException {
    String userName = "testUser";
    String roleNames = "Power User\tBusiness User\t";
    setupMockSessionUser(SESSION_USER_NAME, true);
    // Create session that will generate tenant
    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");
    List<IPentahoRole> roleList = new ArrayList<>();
    roleList.add(ceoRole);
    roleList.add(ctoRole);
    IUserRoleDao roleDao = mock(IUserRoleDao.class);
    when(roleDao.getUserRoles(any(ITenant.class), anyString())).thenReturn(roleList);
    PentahoSystem.registerObject(roleDao);
    userRoleService.assignRolesToUser(userName, roleNames);
    verify(roleDao).setUserRoles(any(ITenant.class), anyString(), argThat(new UnorderedArrayMatcher(new String[] { "ceo", "cto", "Power User", "Business User" })));
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) Test(org.junit.Test)

Example 29 with IPentahoRole

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

the class UserRoleDaoUserDetailsServiceIT method testLoadUserByUsername.

@Test
public void testLoadUserByUsername() {
    loginAsSysTenantAdmin();
    ITenant mainTenant_1 = tenantManager.createTenant(systemTenant, MAIN_TENANT_1, tenantAdminRoleName, tenantAuthenticatedRoleName, ANONYMOUS_ROLE_NAME);
    userRoleDao.createUser(mainTenant_1, USERNAME_ADMIN, PASSWORD, "", new String[] { tenantAdminRoleName });
    login(USERNAME_ADMIN, mainTenant_1, new String[] { tenantAdminRoleName, tenantAuthenticatedRoleName });
    IPentahoUser pentahoUser = userRoleDao.createUser(mainTenant_1, USER_2, PASSWORD_2, USER_DESCRIPTION_2, null);
    IPentahoRole pentahoRole = userRoleDao.createRole(mainTenant_1, ROLE_1, ROLE_DESCRIPTION_1, null);
    pentahoRole = userRoleDao.createRole(mainTenant_1, ROLE_2, ROLE_DESCRIPTION_2, null);
    pentahoRole = userRoleDao.createRole(mainTenant_1, ROLE_3, ROLE_DESCRIPTION_3, null);
    userRoleDao.setUserRoles(mainTenant_1, USER_2, new String[] { ROLE_1, ROLE_2, ROLE_3 });
    UserRoleDaoUserDetailsService userDetailsService = new UserRoleDaoUserDetailsService();
    userDetailsService.setUserRoleDao(userRoleDao);
    userDetailsService.setDefaultRole(tenantAuthenticatedRoleName);
    UserDetails userFromService = userDetailsService.loadUserByUsername(USER_2);
    assertTrue(userFromService.getUsername().equals(USER_2));
    assertTrue(userFromService.getPassword() != null);
    assertTrue(userFromService.isEnabled() == true);
    assertTrue(userFromService.getAuthorities().size() == 4);
    GrantedAuthority[] auths = new GrantedAuthority[4];
    userFromService.getAuthorities().toArray(auths);
    assertTrue(auths[0].getAuthority().equals(ROLE_0) || auths[0].getAuthority().equals(ROLE_3) || auths[0].getAuthority().equals(ROLE_2) || auths[0].getAuthority().equals(ROLE_1));
    assertTrue(auths[1].getAuthority().equals(ROLE_0) || auths[1].getAuthority().equals(ROLE_3) || auths[1].getAuthority().equals(ROLE_2) || auths[1].getAuthority().equals(ROLE_1));
    assertTrue(auths[2].getAuthority().equals(ROLE_0) || auths[2].getAuthority().equals(ROLE_3) || auths[2].getAuthority().equals(ROLE_2) || auths[2].getAuthority().equals(ROLE_1));
    assertTrue(auths[3].getAuthority().equals(ROLE_0) || auths[3].getAuthority().equals(ROLE_3) || auths[3].getAuthority().equals(ROLE_2) || auths[3].getAuthority().equals(ROLE_1));
    cleanupUserAndRoles(mainTenant_1);
}
Also used : ITenant(org.pentaho.platform.api.mt.ITenant) UserRoleDaoUserDetailsService(org.pentaho.platform.security.userroledao.service.UserRoleDaoUserDetailsService) UserDetails(org.springframework.security.core.userdetails.UserDetails) GrantedAuthority(org.springframework.security.core.GrantedAuthority) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) Test(org.junit.Test)

Example 30 with IPentahoRole

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

the class TestPrincipalProvider method getGroupMembership.

/**
 * {@inheritDoc}
 *
 * <p>
 * Called from {@code AbstractLoginModule.getPrincipals()}
 * </p>
 */
@Override
public PrincipalIterator getGroupMembership(Principal principal) {
    if (principal instanceof EveryonePrincipal) {
        return PrincipalIteratorAdapter.EMPTY;
    }
    if (principal instanceof AclMetadataPrincipal) {
        return PrincipalIteratorAdapter.EMPTY;
    }
    Set<Principal> principals = new HashSet<Principal>(roleAssignments.containsKey(principal.getName()) ? roleAssignments.get(principal.getName()) : new HashSet<Principal>());
    principals.add(EveryonePrincipal.getInstance());
    if (principal instanceof AdminPrincipal) {
        principals.add(adminRolePrincipal);
    } else if (principal instanceof UserPrincipal) {
        if (userRoleDao != null) {
            List<IPentahoRole> roles;
            try {
                roles = userRoleDao.getUserRoles(null, principal.getName());
                for (IPentahoRole role : roles) {
                    principals.add(new SpringSecurityRolePrincipal(tenantedRoleNameUtils.getPrincipleId(role.getTenant(), role.getName())));
                }
            } catch (Exception e) {
                roles = userRoleDao.getUserRoles(null, principal.getName());
                for (IPentahoRole role : roles) {
                    principals.add(new SpringSecurityRolePrincipal(tenantedRoleNameUtils.getPrincipleId(role.getTenant(), role.getName())));
                }
            }
        } else {
            if (principal.getName() != null && (principal.getName().startsWith("admin") || principal.getName().startsWith("suzy") || principal.getName().startsWith("tiffany"))) {
                ITenant tenant = tenantedUserNameUtils.getTenant(principal.getName());
                principals.add(new SpringSecurityRolePrincipal(tenantedRoleNameUtils.getPrincipleId(tenant, "Authenticated")));
            }
            if (principal.getName() != null && principal.getName().startsWith("admin")) {
                ITenant tenant = tenantedUserNameUtils.getTenant(principal.getName());
                principals.add(new SpringSecurityRolePrincipal(tenantedRoleNameUtils.getPrincipleId(tenant, "TenantAdmin")));
            }
            if (principal.getName() != null && principal.getName().startsWith("super")) {
                ITenant tenant = tenantedUserNameUtils.getTenant(principal.getName());
                principals.add(new SpringSecurityRolePrincipal(tenantedRoleNameUtils.getPrincipleId(tenant, "SysAdmin")));
            }
        }
    }
    return new PrincipalIteratorAdapter(principals);
}
Also used : PrincipalIteratorAdapter(org.apache.jackrabbit.core.security.principal.PrincipalIteratorAdapter) UserPrincipal(org.apache.jackrabbit.core.security.UserPrincipal) NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) RepositoryException(javax.jcr.RepositoryException) LoginException(javax.jcr.LoginException) AclMetadataPrincipal(org.pentaho.platform.repository2.unified.jcr.JcrAclMetadataStrategy.AclMetadataPrincipal) AdminPrincipal(org.apache.jackrabbit.core.security.principal.AdminPrincipal) ITenant(org.pentaho.platform.api.mt.ITenant) ArrayList(java.util.ArrayList) List(java.util.List) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole) EveryonePrincipal(org.apache.jackrabbit.core.security.principal.EveryonePrincipal) EveryonePrincipal(org.apache.jackrabbit.core.security.principal.EveryonePrincipal) AnonymousPrincipal(org.apache.jackrabbit.core.security.AnonymousPrincipal) UserPrincipal(org.apache.jackrabbit.core.security.UserPrincipal) AclMetadataPrincipal(org.pentaho.platform.repository2.unified.jcr.JcrAclMetadataStrategy.AclMetadataPrincipal) AdminPrincipal(org.apache.jackrabbit.core.security.principal.AdminPrincipal) Principal(java.security.Principal) HashSet(java.util.HashSet)

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