org.apache.directory.api.ldap.model.entry.Modification Java Examples

The following examples show how to use org.apache.directory.api.ldap.model.entry.Modification. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApacheLdapProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void writeBinaryAttribute( final String entryDN, final String attributeName, final byte[][] values, final boolean overwrite, final ChaiRequestControl[] controls )
        throws ChaiUnavailableException, ChaiOperationException
{
    try
    {
        final ModifyRequest modifyRequest = new ModifyRequestImpl();
        modifyRequest.setName( new Dn( entryDN ) );
        modifyRequest.addAllControls( figureControls( controls ) );
        {
            final Modification modification = new DefaultModification();
            modification.setOperation( overwrite ? ModificationOperation.REPLACE_ATTRIBUTE : ModificationOperation.ADD_ATTRIBUTE );
            modification.setAttribute( new DefaultAttribute( attributeName, values ) );
            modifyRequest.addModification( modification );
        }
        final ModifyResponse response = connection.modify( modifyRequest );
        processResponse( response );
    }
    catch ( LdapException e )
    {
        throw ChaiOperationException.forErrorMessage( e.getMessage() );
    }

}
 
Example #2
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * Convert a list of ModificationItemImpl to a list of 
 *
 * @param modificationImpls
 * @param atRegistry
 * @return
 * @throws LdapException
 */
public static List<Modification> convertToServerModification( List<ModificationItem> modificationItems,
    SchemaManager schemaManager ) throws LdapException
{
    if ( modificationItems != null )
    {
        List<Modification> modifications = new ArrayList<Modification>( modificationItems.size() );

        for ( ModificationItem modificationItem : modificationItems )
        {
            AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( modificationItem
                .getAttribute().getID() );
            modifications.add( toServerModification( modificationItem, attributeType ) );
        }

        return modifications;
    }
    else
    {
        return null;
    }
}
 
Example #3
Source File: DefaultCoreSession.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void modify( Dn dn, List<Modification> mods, LogChange log ) throws LdapException
{
    if ( mods == null )
    {
        return;
    }

    List<Modification> serverModifications = new ArrayList<Modification>( mods.size() );

    for ( Modification mod : mods )
    {
        serverModifications.add( new DefaultModification( directoryService.getSchemaManager(), mod ) );
    }

    ModifyOperationContext modifyContext = new ModifyOperationContext( this, dn, serverModifications );

    modifyContext.setLogChange( log );

    OperationManager operationManager = directoryService.getOperationManager();
    operationManager.modify( modifyContext );
}
 
Example #4
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
Group delete( Group group, String key, String value ) throws FinderException, RemoveException
{
    LdapConnection ld = null;
    String nodeDn = getDn( group.getName(), group.getContextId() );

    try
    {
        LOG.debug( "delete group property dn [{}], key [{}], value [{}]", nodeDn, key, value );
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.REMOVE_ATTRIBUTE, GROUP_PROPERTY_ATTR_IMPL, key + "=" + value ) );
        ld = getAdminConnection();
        modify( ld, nodeDn, mods, group );
    }
    catch ( LdapException e )
    {
        String error = "delete group property node dn [" + nodeDn + "] caught LDAPException=" + e;
        throw new RemoveException( GlobalErrIds.GROUP_DELETE_PROPERTY_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return get( group );
}
 
Example #5
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Group assign( Group entity, String userDn ) throws FinderException, UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    LOG.debug( "assign group property dn [{}], member dn [{}]", dn, userDn );
    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.ADD_ATTRIBUTE, SchemaConstants.MEMBER_AT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "assign group name [" + entity.getName() + "] user dn [" + userDn + "] caught " +
            "LDAPException=" + e;
        throw new UpdateException( GlobalErrIds.GROUP_USER_ASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return get( entity );
}
 
Example #6
Source File: DefaultPartitionNexus.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
private void createContextCsnModList() throws LdapException
{
    Modification contextCsnMod = new DefaultModification();
    contextCsnMod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
    DefaultAttribute contextCsnAt = new DefaultAttribute( schemaManager
        .lookupAttributeTypeRegistry( SchemaConstants.CONTEXT_CSN_AT ) );
    contextCsnMod.setAttribute( contextCsnAt );

    mods.add( contextCsnMod );

    Modification timeStampMod = new DefaultModification();
    timeStampMod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
    DefaultAttribute timeStampAt = new DefaultAttribute( schemaManager
        .lookupAttributeTypeRegistry( SchemaConstants.MODIFY_TIMESTAMP_AT ) );
    timeStampMod.setAttribute( timeStampAt );

    mods.add( timeStampMod );
}
 
Example #7
Source File: SchemaAwareLdifReaderTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void testReaderAttrIdCaseInsensitive( String ldif ) throws Exception
{
    LdifReader reader = new LdifReader( schemaManager );

    List<LdifEntry> entries = reader.parseLdif( ldif );
    assertNotNull( entries );
    reader.close();

    LdifEntry entry = entries.get( 0 );

    assertTrue( entry.isChangeModify() );

    assertEquals( "dc=example,dc=com", entry.getDn().getName() );

    List<Modification> mods = entry.getModifications();
    assertTrue( mods.size() == 1 );
    Attribute attr = mods.get( 0 ).getAttribute();
    assertTrue( attr.getId().equals( "administrativerole" ) );
    assertEquals( attr.getString(), "accessControlSpecificArea" );
}
 
Example #8
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap
 * modification set in preparation for ldap modify.
 *
 * @param props    contains {@link java.util.Properties} targeted for updating in ldap.
 * @param mods     ldap modification set containing name-value pairs in raw ldap format.
 * @param attrName contains the name of the ldap attribute to be updated.
 * @param replace  boolean variable, if set to true use {@link ModificationOperation#REPLACE_ATTRIBUTE} else {@link
 * ModificationOperation#ADD_ATTRIBUTE}.
 * @param separator contains the char value used to separate name and value in ldap raw format.
 */
protected void loadProperties( Properties props, List<Modification> mods, String attrName, boolean replace,
    char separator )
{
    if ( props != null && props.size() > 0 )
    {
        if ( replace )
        {
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attrName ) );
        }

        for ( Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); )
        {
            String key = ( String ) e.nextElement();
            String val = props.getProperty( key );
            // This LDAP attr is stored as a name-value pair separated by a ':'.
            mods.add( new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attrName,
                key + separator + val ) );
        }
    }
}
 
Example #9
Source File: AdminRoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * This method will remove the supplied DN as a role occupant to the target record.
 * This data will be stored in the {@link GlobalIds#ADMIN_ROLE_ROOT} container.
 *
 * @param entity record contains {@link AdminRole#name}.  Null attributes will be ignored.
 * @param userDn contains the DN for userId who is being deassigned.
 * @return input record back to client.
 * @throws UpdateException in the event LDAP errors occur.
 */
AdminRole deassign( AdminRole entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification(
            ModificationOperation.REMOVE_ATTRIBUTE, ROLE_OCCUPANT, userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deassign role name [" + entity.getName() + "] user dn [" + userDn
            + "] caught LdapException=" + e;
        throw new UpdateException( GlobalErrIds.ARLE_USER_DEASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return entity;
}
 
Example #10
Source File: AdminRoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param entity
 * @throws UpdateException
 */
void deleteParent( AdminRole entity ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, GlobalIds.PARENT_NODES ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deleteParent name [" + entity.getName() + "] caught LdapException=" + e;
        throw new UpdateException( GlobalErrIds.ARLE_REMOVE_PARENT_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example #11
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * @param user
 * @return
 * @throws UpdateException
 * @throws Exception
 */
String deletePwPolicy( User user ) throws UpdateException
{
    LdapConnection ld = null;
    String userDn = getDn( user.getUserId(), user.getContextId() );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();

        mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, OPENLDAP_POLICY_SUBENTRY ) );
        ld = getAdminConnection();
        modify( ld, userDn, mods, user );
    }
    catch ( LdapException e )
    {
        String warning = "deletePwPolicy userId [" + user.getUserId() + "] caught LDAPException=" + e + " msg=" + e;
        throw new UpdateException( GlobalErrIds.USER_PW_PLCY_DEL_FAILED, warning, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return userDn;
}
 
Example #12
Source File: RoleDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param entity
 * @throws UpdateException
 */
void deleteParent( Role entity ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE,
            GlobalIds.PARENT_NODES ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "deleteParent name [" + entity.getName() + "] caught LdapException=" + e;
        throw new UpdateException( GlobalErrIds.ROLE_REMOVE_PARENT_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example #13
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example #14
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test a addModification applied to an entry 
 */
@Test
public void testApplyAddModificationToEntry() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.add( "dc", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );

    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 2, entry.size() );
    assertEquals( attr, entry.get( "cn" ) );
}
 
Example #15
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the deletion of an attribute into an entry which does not contain the attribute
 */
@Test
public void testApplyRemoveModificationFromEntryAttributeNotPresent() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute dc = new DefaultAttribute( "dc", "apache" );
    entry.put( dc );

    Attribute cn = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, cn );

    AttributeUtils.applyModification( entry, modification );

    assertNull( entry.get( "cn" ) );
    assertNotNull( entry.get( "dc" ) );
    assertEquals( 1, entry.size() );
    assertEquals( dc, entry.get( "dc" ) );
}
 
Example #16
Source File: DefaultPartitionNexus.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
private void createContextCsnModList() throws LdapException
{
    Modification contextCsnMod = new DefaultModification();
    contextCsnMod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
    DefaultAttribute contextCsnAt = new DefaultAttribute( schemaManager
        .lookupAttributeTypeRegistry( SchemaConstants.CONTEXT_CSN_AT ) );
    contextCsnMod.setAttribute( contextCsnAt );

    mods.add( contextCsnMod );

    Modification timeStampMod = new DefaultModification();
    timeStampMod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
    DefaultAttribute timeStampAt = new DefaultAttribute( schemaManager
        .lookupAttributeTypeRegistry( SchemaConstants.MODIFY_TIMESTAMP_AT ) );
    timeStampMod.setAttribute( timeStampAt );

    mods.add( timeStampMod );
}
 
Example #17
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of {@link java.util.Properties}, convert to raw data name-value format and load into ldap
 * modification set in preparation for ldap modify.
 *
 * @param props    contains {@link java.util.Properties} targeted for removal from ldap.
 * @param mods     ldap modification set containing name-value pairs in raw ldap format to be removed.
 * @param attrName contains the name of the ldap attribute to be removed.
 */
protected void removeProperties( Properties props, List<Modification> mods, String attrName )
{
    if ( props != null && props.size() > 0 )
    {
        for ( Enumeration<?> e = props.propertyNames(); e.hasMoreElements(); )
        {
            String key = ( String ) e.nextElement();
            String val = props.getProperty( key );

            // This LDAP attr is stored as a name-value pair separated by a ':'.
            mods.add( new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attrName,
                key + GlobalIds.PROP_SEP + val ) );
        }
    }
}
 
Example #18
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the removing by modification of an existing attribute in an .
 * 
 * @throws LdapException
 */
@Test
public void testApplyModifyModificationRemoveAttribute() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test" );
    entry.put( "ou", "apache", "acme corp" );

    Attribute newOu = new DefaultAttribute( "ou" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, newOu );

    AttributeUtils.applyModification( entry, modification );

    assertEquals( 1, entry.size() );

    assertNotNull( entry.get( "cn" ) );
    assertNull( entry.get( "ou" ) );
}
 
Example #19
Source File: LdifReaderTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
private void testReaderAttrIdCaseInsensitive( String ldif ) throws Exception
{
    LdifReader reader = new LdifReader();

    List<LdifEntry> entries = reader.parseLdif( ldif );
    assertNotNull( entries );
    reader.close();

    LdifEntry entry = entries.get( 0 );

    assertTrue( entry.isChangeModify() );

    assertEquals( "dc=example,dc=com", entry.getDn().getName() );

    List<Modification> mods = entry.getModifications();
    assertTrue( mods.size() == 1 );
    Attribute attr = mods.get( 0 ).getAttribute();
    assertTrue( attr.getId().equals( "administrativerole" ) );
    assertEquals( attr.getString(), "accessControlSpecificArea" );
}
 
Example #20
Source File: PropertyDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Delete properties to the provided entity using the provided property provider    
 *
 * @param entity A FortressEntity that supports properties (Role, AdminRole, Group, Permission, PermObj)
 * @param properties
 * @param propProvider DAO for entity type that implements property provider interface
 * @throws UpdateException
 * @throws FinderException
 */
void deleteProperties( FortEntity entity, Properties properties, PropertyProvider propProvider ) throws UpdateException, FinderException
{
    LdapConnection ld = null;
    String entityDn = propProvider.getDn( entity );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        removeProperties( properties, mods, GlobalIds.PROPS );            

        ld = getAdminConnection();
        modify( ld, entityDn, mods, entity );            
    }
    catch ( LdapException e )
    {
        String error = "delete entity properties[" + entity.getClass().getSimpleName() + "] caught LDAPException=" + e;
        throw new UpdateException( GlobalErrIds.USER_UPDATE_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example #21
Source File: UserDAO.java    From directory-fortress-core with Apache License 2.0 6 votes vote down vote up
/**
 * Given a collection of RBAC roles, {@link UserRole}, convert to raw data format and load into ldap modification
 * set in preparation for ldap modify.
 *
 * @param list contains List of type {@link UserRole} targeted for updating into ldap.
 * @param mods contains ldap modification set containing RBAC role assignments in raw ldap format to be updated.
 * @throws LdapInvalidAttributeValueException
 */
private void loadUserRoles( List<UserRole> list, List<Modification> mods )
    throws LdapInvalidAttributeValueException
{
    Attribute userRoleData = new DefaultAttribute( GlobalIds.USER_ROLE_DATA );
    Attribute userRoleAssign = new DefaultAttribute( USER_ROLE_ASSIGN );

    if ( list != null )
    {
        for ( UserRole userRole : list )
        {
            userRoleData.add( userRole.getRawData() );
            userRoleAssign.add( userRole.getName() );
        }

        if ( userRoleData.size() != 0 )
        {
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleData ) );
            mods.add( new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, userRoleAssign ) );
        }
    }
}
 
Example #22
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to extract a modification item from an array of modifications.
 * 
 * @param mods the array of ModificationItems to extract the Attribute from.
 * @param type the attributeType spec of the Attribute to extract
 * @return the modification item on the attributeType specified
 */
public static final Modification getModificationItem( List<Modification> mods, AttributeType type )
{
    for ( Modification modification : mods )
    {
        Attribute attribute = modification.getAttribute();

        if ( attribute.getAttributeType() == type )
        {
            return modification;
        }
    }

    return null;
}
 
Example #23
Source File: LdifEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a Modify changeType LdifEntry with no attributes
 */
@Test
public void testLdifEntryChangeTypeModifyNoAttribute() throws Exception
{
    String ldif =
        "changetype: modify\n" +
            "add: cn\n" +
            "-";

    LdifEntry ldifEntry = new LdifEntry( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldif );

    assertNotNull( ldifEntry );
    assertEquals( ChangeType.Modify, ldifEntry.getChangeType() );
    assertNull( ldifEntry.getEntry() );
    assertEquals( "cn=app1,ou=applications,ou=conf,dc=apache,dc=org", ldifEntry.getDn().getName() );
    assertFalse( ldifEntry.hasControls() );
    assertTrue( ldifEntry.isLdifChange() );

    // Check the modification
    assertNotNull( ldifEntry.getModifications() );

    for ( Modification modification : ldifEntry.getModifications() )
    {
        assertEquals( ModificationOperation.ADD_ATTRIBUTE, modification.getOperation() );
        Attribute attribute = modification.getAttribute();

        assertNotNull( attribute );
        assertEquals( "cn", attribute.getId() );
        assertNotNull( attribute.get() );
        assertTrue( attribute.get().isNull() );
    }
}
 
Example #24
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test a addModification applied to an entry with the same attribute
 * and the same value 
 */
@Test
public void testApplyAddModificationToEntryWithSameValue() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test", "apache" );
    assertEquals( 1, entry.size() );

    Attribute attr = new DefaultAttribute( "cn", "test" );
    Modification modification = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute cnAttr = entry.get( "cn" );

    assertTrue( cnAttr.size() != 0 );

    Set<String> expectedValues = new HashSet<String>();
    expectedValues.add( "apache" );
    expectedValues.add( "test" );

    for ( Value value : cnAttr )
    {
        String valueStr = value.getString();

        assertTrue( expectedValues.contains( valueStr ) );

        expectedValues.remove( valueStr );
    }

    assertEquals( 0, expectedValues.size() );
}
 
Example #25
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method to extract an attribute from a list of modifications.
 * 
 * @param mods the list of ModificationItems to extract the Attribute from.
 * @param type the attributeType spec of the Attribute to extract
 * @return the extract Attribute or null if no such attribute exists
 */
public static Attribute getAttribute( List<Modification> mods, AttributeType type )
{
    Modification mod = getModificationItem( mods, type );

    if ( mod != null )
    {
        return mod.getAttribute();
    }

    return null;
}
 
Example #26
Source File: RoleDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param entity
 * @param userDn
 * @return
 * @throws org.apache.directory.fortress.core.UpdateException
 *
 */
Role assign( Role entity, String userDn ) throws UpdateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );

    try
    {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add( new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, SchemaConstants.ROLE_OCCUPANT_AT,
            userDn ) );
        ld = getAdminConnection();
        modify( ld, dn, mods, entity );
    }
    catch ( LdapException e )
    {
        String error = "assign role name [" + entity.getName() + "] user dn [" + userDn + "] caught LdapException="
            + e;
        throw new UpdateException( GlobalErrIds.ROLE_USER_ASSIGN_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example #27
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the deletion of an attribute into an entry which contains the attribute
 * with more than one value
 * 
 * The entry should contain the attribute after the operation, but with one less value
 */
@Test
public void testApplyRemoveModificationFromEntrySameAttributeValues() throws LdapException
{
    Entry entry = new DefaultEntry();
    entry.put( "cn", "test", "apache" );

    Attribute attr = new DefaultAttribute( "cn", "test" );

    Modification modification = new DefaultModification( ModificationOperation.REMOVE_ATTRIBUTE, attr );

    AttributeUtils.applyModification( entry, modification );

    assertNotNull( entry.get( "cn" ) );
    assertEquals( 1, entry.size() );

    Attribute modifiedAttr = entry.get( "cn" );

    assertTrue( modifiedAttr.size() != 0 );

    boolean isFirst = true;

    for ( Value value : modifiedAttr )
    {
        assertTrue( isFirst );

        isFirst = false;
        assertEquals( "apache", value.getString() );
    }
}
 
Example #28
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * Delete exiting ldap entry and all descendants from the directory.  Add audit context.  This method will call
 * modify prior to delete which will
 * force corresponding audit record to be written to slapd access log.
 *
 * @param connection handle to ldap connection.
 * @param dn         contains distinguished node of entry targeted for removal..
 * @param entity     contains audit context.
 * @throws LdapException   in the event system error occurs.
 * @throws CursorException
 */
protected void deleteRecursive( LdapConnection connection, String dn, FortEntity entity ) throws LdapException,
    CursorException
{
    List<Modification> mods = new ArrayList<Modification>();
    audit( mods, entity );

    if ( mods.size() > 0 )
    {
        modify( connection, dn, mods );
    }

    deleteRecursive( connection, dn );
}
 
Example #29
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a Modification to an instance of a ServerModification object.
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( Modification modification, AttributeType attributeType )
    throws LdapException
{
    Modification serverModification = new DefaultModification(
        modification.getOperation(),
        new DefaultAttribute( attributeType, modification.getAttribute() ) );

    return serverModification;

}
 
Example #30
Source File: AttributeUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test the replacement by modification of an attribute in an empty entry.
 * 
 * As we are replacing a non existing attribute, it should not change the entry.
 *
 * @throws LdapException
 */
@Test
public void testApplyModifyEmptyModificationFromEmptyEntry() throws LdapException
{
    Entry entry = new DefaultEntry();

    Attribute attr = new DefaultAttribute( "cn" );

    Modification modification = new DefaultModification( ModificationOperation.REPLACE_ATTRIBUTE, attr );
    AttributeUtils.applyModification( entry, modification );
    assertNull( entry.get( "cn" ) );
    assertEquals( 0, entry.size() );
}