public boolean saveUserRole()

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


    public boolean saveUserRole( String roleName, String username, DirContext context )
        throws MappingException
    {

        String groupName = findGroupName( roleName );

        if ( groupName == null )
        {
            log.warn( "no group found for role '{}", roleName );
            groupName = roleName;
        }

        NamingEnumeration<SearchResult> namingEnumeration = null;
        try
        {
            SearchControls searchControls = new SearchControls( );

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

            String filter = "objectClass=" + getLdapGroupClass( );

            namingEnumeration = context.search( this.groupNameAttribute + "=" + groupName + "," + getGroupsDn( ), filter, searchControls );

            if ( namingEnumeration.hasMore( ) )
            {
                SearchResult searchResult = namingEnumeration.next( );
                Attribute attribute = searchResult.getAttributes( ).get( getLdapGroupMemberAttribute( ) );
                if ( attribute == null )
                {
                    BasicAttribute basicAttribute = new BasicAttribute( getLdapGroupMemberAttribute( ) );
                    basicAttribute.add( this.userIdAttribute + "=" + username + "," + getBaseDn( ) );
                    context.modifyAttributes( this.groupNameAttribute + "=" + groupName + "," + getGroupsDn( ), new ModificationItem[]{
                        new ModificationItem( DirContext.ADD_ATTRIBUTE, basicAttribute )} );
                }
                else
                {
                    attribute.add( this.userIdAttribute + "=" + username + "," + getBaseDn( ) );
                    context.modifyAttributes( this.groupNameAttribute + "=" + groupName + "," + getGroupsDn( ), new ModificationItem[]{
                        new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attribute )} );
                }
                return true;
            }

            return false;
        }
        catch ( LdapException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }
        catch ( NamingException e )
        {
            throw new MappingException( e.getMessage( ), e );
        }

        finally
        {
            if ( namingEnumeration != null )
            {
                try
                {
                    namingEnumeration.close( );
                }
                catch ( NamingException e )
                {
                    log.warn( "failed to close search results", e );
                }
            }
        }
    }