Search in sources :

Example 1 with RolePreprocessingMapper

use of org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper in project pentaho-platform by pentaho.

the class DefaultLdapUserRoleListServiceTest method testGetAuthoritiesForUser1.

/**
 * Get the roles of user <code>suzy</code> by extracting the <code>cn</code> token from the <code>uniqueMember</code>
 * attribute of the object that matches base of <code>ou=users</code> and filter of <code>(uid={0})</code>.
 *
 * <p>
 * Note that the UserDetailsService used by Spring Security is re-used here.
 * </p>
 *
 * @throws Exception
 */
@Test
public void testGetAuthoritiesForUser1() throws Exception {
    // $NON-NLS-1$//$NON-NLS-2$
    LdapUserSearch userSearch = getUserSearch("ou=users", "(uid={0})");
    LdapUserDetailsService service = new LdapUserDetailsService(userSearch, new NoOpLdapAuthoritiesPopulator());
    RolePreprocessingMapper mapper = new RolePreprocessingMapper();
    // $NON-NLS-1$
    mapper.setRoleAttributes(new String[] { "uniqueMember" });
    // $NON-NLS-1$
    mapper.setTokenName("cn");
    service.setUserDetailsMapper(mapper);
    DefaultLdapUserRoleListService userRoleListService = getDefaultLdapUserRoleListService();
    userRoleListService.setUserDetailsService(service);
    // $NON-NLS-1$
    List res = userRoleListService.getRolesForUser(null, "suzy");
    // $NON-NLS-1$
    assertTrue(res.contains("ROLE_A"));
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("results of getAuthoritiesForUser1(): " + res);
    }
}
Also used : LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) DefaultLdapUserRoleListService(org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService) NoOpLdapAuthoritiesPopulator(org.pentaho.platform.plugin.services.security.userrole.ldap.NoOpLdapAuthoritiesPopulator) RolePreprocessingMapper(org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper) ArrayList(java.util.ArrayList) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) List(java.util.List) LdapUserSearch(org.springframework.security.ldap.search.LdapUserSearch) FilterBasedLdapUserSearch(org.springframework.security.ldap.search.FilterBasedLdapUserSearch) Test(org.junit.Test)

Example 2 with RolePreprocessingMapper

use of org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper in project pentaho-platform by pentaho.

the class DefaultLdapUserRoleListServiceTest method testGetAuthoritiesForUser1ForTenant.

@Test
public void testGetAuthoritiesForUser1ForTenant() throws Exception {
    ITenant defaultTenant = new Tenant("/pentaho/tenant0", true);
    login("suzy", defaultTenant);
    // $NON-NLS-1$//$NON-NLS-2$
    LdapUserSearch userSearch = getUserSearch("ou=users", "(uid={0})");
    LdapUserDetailsService service = new LdapUserDetailsService(userSearch, new NoOpLdapAuthoritiesPopulator());
    RolePreprocessingMapper mapper = new RolePreprocessingMapper();
    // $NON-NLS-1$
    mapper.setRoleAttributes(new String[] { "uniqueMember" });
    // $NON-NLS-1$
    mapper.setTokenName("cn");
    service.setUserDetailsMapper(mapper);
    DefaultLdapUserRoleListService userRoleListService = getDefaultLdapUserRoleListService();
    userRoleListService.setUserDetailsService(service);
    // $NON-NLS-1$
    List res = userRoleListService.getRolesForUser(defaultTenant, "suzy");
    // $NON-NLS-1$
    assertTrue(res.contains("ROLE_A"));
    if (logger.isDebugEnabled()) {
        // $NON-NLS-1$
        logger.debug("results of getAuthoritiesForUser1(): " + res);
    }
    try {
        userRoleListService.getRolesForUser(new Tenant("/pentaho", true), "suzy");
    } catch (UnsupportedOperationException uoe) {
        assertNotNull(uoe);
    }
}
Also used : LdapUserDetailsService(org.springframework.security.ldap.userdetails.LdapUserDetailsService) DefaultLdapUserRoleListService(org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService) ITenant(org.pentaho.platform.api.mt.ITenant) Tenant(org.pentaho.platform.core.mt.Tenant) ITenant(org.pentaho.platform.api.mt.ITenant) NoOpLdapAuthoritiesPopulator(org.pentaho.platform.plugin.services.security.userrole.ldap.NoOpLdapAuthoritiesPopulator) RolePreprocessingMapper(org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper) ArrayList(java.util.ArrayList) SearchResultToAttrValueList(org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList) List(java.util.List) LdapUserSearch(org.springframework.security.ldap.search.LdapUserSearch) FilterBasedLdapUserSearch(org.springframework.security.ldap.search.FilterBasedLdapUserSearch) Test(org.junit.Test)

Example 3 with RolePreprocessingMapper

use of org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper in project pentaho-platform by pentaho.

the class RolePreprocessingMapperTests method testMapUserFromContext.

@SuppressWarnings("unchecked")
@Test
public void testMapUserFromContext() throws Exception {
    RolePreprocessingMapper mapper = new RolePreprocessingMapper();
    // $NON-NLS-1$
    mapper.setTokenName("cn");
    // $NON-NLS-1$
    mapper.setRolePrefix("");
    // $NON-NLS-1$
    mapper.setRoleAttributes(new String[] { "uniqueMember" });
    // get the user record
    DirContextOperations ctx = // $NON-NLS-1$
    new SpringSecurityLdapTemplate(getContextSource()).retrieveEntry(// $NON-NLS-1$
    "uid=suzy,ou=users", null);
    // get any roles that aren't in the user record
    Set<String> extraRoles = new SpringSecurityLdapTemplate(getContextSource()).searchForSingleAttributeValues("ou=roles", "roleoccupant={0}", new String[] { "uid=suzy,ou=users,dc=pentaho,dc=org", "suzy" }, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
    "cn");
    List<GrantedAuthority> authorities = Arrays.asList(new GrantedAuthority[extraRoles.size()]);
    int i = 0;
    for (String extraRole : extraRoles) {
        authorities.add(new SimpleGrantedAuthority(extraRole));
    }
    // use the mapper to create a UserDetails instance
    // $NON-NLS-1$
    UserDetails userDetails = mapper.mapUserFromContext(ctx, "suzy", authorities);
    System.out.println(userDetails);
    // this asserts the ordering too; not strictly necessary
    Collection<? extends GrantedAuthority> expectedAuthorities = Arrays.asList(new GrantedAuthority[] { new SimpleGrantedAuthority("A"), new SimpleGrantedAuthority("cto"), new SimpleGrantedAuthority("is"), new SimpleGrantedAuthority("Authenticated") });
    Collection<? extends GrantedAuthority> unexpectedAuthorities = userDetails.getAuthorities();
    unexpectedAuthorities.removeAll(expectedAuthorities);
    assertTrue(unexpectedAuthorities.isEmpty());
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SpringSecurityLdapTemplate(org.springframework.security.ldap.SpringSecurityLdapTemplate) UserDetails(org.springframework.security.core.userdetails.UserDetails) DirContextOperations(org.springframework.ldap.core.DirContextOperations) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) RolePreprocessingMapper(org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 RolePreprocessingMapper (org.pentaho.platform.plugin.services.security.userrole.ldap.RolePreprocessingMapper)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 DefaultLdapUserRoleListService (org.pentaho.platform.plugin.services.security.userrole.ldap.DefaultLdapUserRoleListService)2 NoOpLdapAuthoritiesPopulator (org.pentaho.platform.plugin.services.security.userrole.ldap.NoOpLdapAuthoritiesPopulator)2 SearchResultToAttrValueList (org.pentaho.platform.plugin.services.security.userrole.ldap.transform.SearchResultToAttrValueList)2 FilterBasedLdapUserSearch (org.springframework.security.ldap.search.FilterBasedLdapUserSearch)2 LdapUserSearch (org.springframework.security.ldap.search.LdapUserSearch)2 LdapUserDetailsService (org.springframework.security.ldap.userdetails.LdapUserDetailsService)2 ITenant (org.pentaho.platform.api.mt.ITenant)1 Tenant (org.pentaho.platform.core.mt.Tenant)1 DirContextOperations (org.springframework.ldap.core.DirContextOperations)1 GrantedAuthority (org.springframework.security.core.GrantedAuthority)1 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1 SpringSecurityLdapTemplate (org.springframework.security.ldap.SpringSecurityLdapTemplate)1