public List getGroups()

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


    public List<String> getGroups( String username, DirContext context )
        throws MappingException
    {

        Set<String> userGroups = new HashSet<String>( );

        NamingEnumeration<SearchResult> namingEnumeration = null;
        try
        {

            SearchControls searchControls = new SearchControls( );

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


            String groupEntry = 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. Need to setup a memberOf Attribute
                    LdapUser ldapUser = (LdapUser) user ;
                    Attribute dnAttribute = ldapUser.getOriginalAttributes( ).get( getLdapDnAttribute( ) );
                    if ( dnAttribute != null )
                    {
                        groupEntry = dnAttribute.get( ).toString();
                    }

                }
            }
            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 ( groupEntry == null )
            {
                //failed to look up the user's groupEntry directly

                if ( this.useDnAsMemberValue )
                {
                    groupEntry = getUserDnFromId( username );
                }
                else
                {
                    groupEntry = username;
                }
            }

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

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

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

            while ( namingEnumeration.hasMore( ) )
            {
                SearchResult groupSearchResult = namingEnumeration.next( );

                String groupName = getGroupNameFromResult( groupSearchResult );

                if (StringUtils.isNotEmpty( groupName )) {
                    userGroups.add( groupName );
                }


            }

            return new ArrayList( userGroups );
        }
        catch ( LdapException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }
        catch ( NamingException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }
        finally
        {
            close( namingEnumeration );
        }
    }