public List getGroupObjects()

in redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/role/DefaultLdapRoleMapper.java [623:708]


    public List<LdapGroup> getGroupObjects( String username, DirContext context ) throws MappingException
    {
        Set<LdapGroup> userGroups = new HashSet<>( );

        NamingEnumeration<SearchResult> namingEnumeration = null;
        try
        {

            SearchControls searchControls = new SearchControls( );

            searchControls.setDerefLinkFlag( true );
            searchControls.setSearchScope( SearchControls.SUBTREE_SCOPE );


            String userIdentifier = null;
            String userDn = null;
            try
            {
                //try to look the user up
                User user = userManager.findUser( username );
                if ( user != null && user instanceof LdapUser )
                {
                    // TODO: This is some kind of memberOf retrieval, but will not work with DN.
                    // We need a configuration entry for the memberOf attribute and a flag, if this should be used
                    LdapUser ldapUser = (LdapUser) user ;
                    Attribute dnAttribute = ldapUser.getOriginalAttributes( ).get( getLdapDnAttribute( ) );
                    if ( dnAttribute != null )
                    {
                        userIdentifier = dnAttribute.get( ).toString();
                    }
                    userDn = ldapUser.getDn( );

                }
            }
            catch ( UserNotFoundException e )
            {
                log.warn( "Failed to look up user {}. Computing distinguished name manually", username, e );
            }
            catch ( UserManagerException e )
            {
                log.warn( "Failed to look up user {}. Computing distinguished name manually", username, e );
            }
            if ( userIdentifier == null )
            {
                //failed to look up the user's groupEntry directly

                if ( this.useDnAsMemberValue )
                {
                    userIdentifier = userDn;
                }
                else
                {
                    userIdentifier = username;
                }
            }

            String filter =
                new StringBuilder( ).append( "(&" ).append( "(objectClass=" + getLdapGroupClass( ) + ")" ).append(
                    "(" ).append( getLdapGroupMemberAttribute( ) ).append( "=" ).append( Rdn.escapeValue( userIdentifier ) ).append( ")" ).append(
                    ")" ).toString( );

            log.debug( "filter: {}", filter );

            namingEnumeration = context.search( getGroupsDn( ), filter, searchControls );

            while ( namingEnumeration.hasMore( ) )
            {
                SearchResult groupSearchResult = namingEnumeration.next( );
                LdapGroup groupName = getGroupFromResult( groupSearchResult );
                userGroups.add( groupName );
            }
        }
        catch ( LdapException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }
        catch ( NamingException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }
        finally
        {
            close( namingEnumeration );
        }
        return new ArrayList( userGroups );
    }