Java Code Examples for org.apache.directory.api.ldap.model.entry.Entry#add()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Entry#add() . 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: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for add( String, String... )
 */
@Test
public void testAddStringStringArray() throws LdapException
{
    Entry entry = new DefaultEntry();

    entry.add( "cn", ( String ) null );
    assertEquals( 1, entry.size() );
    Attribute attributeCN = entry.get( "cn" );
    assertEquals( 1, attributeCN.size() );
    assertNotNull( attributeCN.get() );
    assertNull( attributeCN.get().getString() );

    entry.add( "sn", "test", "test", "TEST" );
    assertEquals( 2, entry.size() );
    Attribute attributeSN = entry.get( "sn" );
    assertEquals( 2, attributeSN.size() );
    assertNotNull( attributeSN.get() );
    assertTrue( attributeSN.contains( "test" ) );
    assertTrue( attributeSN.contains( "TEST" ) );
}
 
Example 2
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for userCertificate;binary AT
 */
@Test
public void testUserCertificateBinary() throws LdapException
{
    Entry entry = new DefaultEntry( schemaManager );
    entry.add( "objectClass", "top", "person", "inetorgPerson" );
    entry.add( "cn", "test1", "test2" );
    entry.add( "sn", "Test1", "Test2" );
    entry.add( "userPassword", BYTES1, BYTES2 );

    entry.add( "userCertificate;binary", Strings.getBytesUtf8( "secret" ) );
    assertTrue( entry.containsAttribute( "userCertificate;binary" ) );
    assertTrue( entry.containsAttribute( "userCertificate" ) );

    entry.removeAttributes( "userCertificate;binary" );
    assertFalse( entry.containsAttribute( "userCertificate;binary" ) );
    assertFalse( entry.containsAttribute( "userCertificate" ) );

    entry.add( "userCertificate", Strings.getBytesUtf8( "secret" ) );
    assertTrue( entry.containsAttribute( "userCertificate;binary" ) );
    assertTrue( entry.containsAttribute( "userCertificate" ) );
}
 
Example 3
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for contains( EntryAttribute... )
 */
@Test
public void testContainsEntryAttributeArray() throws LdapException
{
    Entry entry = new DefaultEntry( exampleDn );

    Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
    Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
    Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
    Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );

    assertFalse( entry.contains( attrOC, attrCN ) );

    entry.add( attrOC, attrCN );

    assertTrue( entry.contains( attrOC, attrCN ) );
    assertFalse( entry.contains( attrOC, attrCN, attrSN ) );

    entry.add( attrSN, attrPWD );

    assertTrue( entry.contains( attrSN, attrPWD ) );
}
 
Example 4
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for contains( String, byte[]... )
 */
@Test
public void testContainsStringByteArray() throws LdapException
{
    Entry entry = new DefaultEntry( exampleDn );

    assertFalse( entry.containsAttribute( "objectClass" ) );

    Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, ( byte[] ) null, BYTES2 );

    entry.add( attrPWD );

    assertTrue( entry.contains( "  userPASSWORD  ", BYTES1, BYTES2 ) );
    assertTrue( entry.contains( "  userPASSWORD  ", ( byte[] ) null ) );

    // We can search for byte[] using Strings. the strings will be converted to byte[]
    assertTrue( entry.contains( "  userPASSWORD  ", "ab", "b" ) );

    assertFalse( entry.contains( "  userPASSWORD  ", "ab", "b", "d" ) );
}
 
Example 5
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method for remove( String, String... )
 */
@Test
public void testRemoveStringStringArray() throws LdapException
{
    Entry entry = createEntry();

    assertTrue( entry.remove( "cn", "test1" ) );
    assertTrue( entry.remove( "cn", "test2" ) );
    assertFalse( entry.containsAttribute( "cn" ) );

    entry.add( "cn", "test1", ( String ) null, "test2" );
    assertTrue( entry.remove( "cn", ( String ) null ) );
    assertEquals( 2, entry.get( "cn" ).size() );
    assertTrue( entry.remove( "cn", "test1", "test3" ) );
    assertEquals( 1, entry.get( "cn" ).size() );
    assertEquals( "test2", entry.get( "cn" ).get().getString() );

    assertFalse( entry.remove( "cn", "test3" ) );
    assertFalse( entry.remove( "void", "whatever" ) );
}
 
Example 6
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method for clone()
 */
@Test
public void testClone() throws LdapException
{
    Entry entry1 = new DefaultEntry();

    Entry entry2 = entry1.clone();

    assertEquals( entry1, entry2 );
    entry2.setDn( exampleDn );

    assertEquals( Dn.EMPTY_DN, entry1.getDn() );

    entry1.setDn( exampleDn );
    entry2 = entry1.clone();
    assertEquals( entry1, entry2 );

    entry1.add( "objectClass", "top", "person" );
    entry1.add( "cn", "test1", "test2" );

    entry2 = entry1.clone();
    assertEquals( entry1, entry2 );

    entry1.add( "cn", "test3" );
    assertEquals( 2, entry2.get( "cn" ).size() );
    assertFalse( entry2.contains( "cn", "test3" ) );

    entry1.add( "sn", ( String ) null );
    assertFalse( entry2.containsAttribute( "sn" ) );
}
 
Example 7
Source File: GroupDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param group
 * @throws org.apache.directory.fortress.core.CreateException
 *
 */
Group create( Group group ) throws CreateException
{
    LdapConnection ld = null;
    String nodeDn = getDn( group.getName(), group.getContextId() );

    try
    {
        LOG.debug( "create group dn [{}]", nodeDn );
        Entry myEntry = new DefaultEntry( nodeDn );
        myEntry.add( SchemaConstants.OBJECT_CLASS_AT, GROUP_OBJ_CLASS );
        myEntry.add( SchemaConstants.CN_AT, group.getName() );
        // protocol is required:
        myEntry.add( GROUP_PROTOCOL_ATTR_IMPL, group.getProtocol() );
        // type is required:
        myEntry.add( GlobalIds.TYPE, group.getType().toString() );

        loadAttrs( group.getMembers(), myEntry, SchemaConstants.MEMBER_AT );
        loadProperties( group.getProperties(), myEntry, GROUP_PROPERTY_ATTR_IMPL, '=' );

        if ( StringUtils.isNotEmpty( group.getDescription() ) )
        {
            myEntry.add( SchemaConstants.DESCRIPTION_AT, group.getDescription() );
        }

        ld = getAdminConnection();
        add( ld, myEntry );
    }
    catch ( LdapException e )
    {
        String error = "create group node dn [" + nodeDn + "] caught LDAPException=" + e;
        throw new CreateException( GlobalErrIds.GROUP_ADD_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return group;
}
 
Example 8
Source File: InMemorySchemaPartition.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Partition initialization - loads schema entries from the files on classpath.
 *
 * @see org.apache.directory.server.core.partition.impl.avl.AvlPartition#doInit()
 */
@Override
protected void doInit() throws Exception {
    if (initialized)
        return;

    LOG.debug("Initializing schema partition " + getId());
    suffixDn.apply(schemaManager);
    super.doInit();

    // load schema
    final Map<String, Boolean> resMap = ResourceMap.getResources(Pattern.compile("schema[/\\Q\\\\E]ou=schema.*"));
    for (String resourcePath : new TreeSet<String>(resMap.keySet())) {
        if (resourcePath.endsWith(".ldif")) {
            URL resource = DefaultSchemaLdifExtractor.getUniqueResource(resourcePath, "Schema LDIF file");
            LdifReader reader = new LdifReader(resource.openStream());
            LdifEntry ldifEntry = reader.next();
            reader.close();

            Entry entry = new DefaultEntry(schemaManager, ldifEntry.getEntry());
            // add mandatory attributes
            if (entry.get(SchemaConstants.ENTRY_CSN_AT) == null) {
                entry.add(SchemaConstants.ENTRY_CSN_AT, defaultCSNFactory.newInstance().toString());
            }
            if (entry.get(SchemaConstants.ENTRY_UUID_AT) == null) {
                entry.add(SchemaConstants.ENTRY_UUID_AT, UUID.randomUUID().toString());
            }
            AddOperationContext addContext = new AddOperationContext(null, entry);
            super.add(addContext);
        }
    }
}
 
Example 9
Source File: LdifUtilsTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Check that the correct reverse LDIF is produced for a modifyDn
 * operation that moves and renames the entry while preserving the
 * old rdn.
 *
 * @throws NamingException on error
 */
@Test
public void testReverseModifyDNSuperior() throws LdapException
{
    Dn dn = new Dn( "cn=john doe, dc=example, dc=com" );
    Dn newSuperior = new Dn( "ou=system" );

    Entry entry = new DefaultEntry( dn );
    entry.add( "objectClass", "person", "uidObject" );
    entry.add( "cn", "john doe", "jack doe" );
    entry.add( "sn", "doe" );
    entry.add( "uid", "jdoe" );

    List<LdifEntry> reverseds = LdifRevertor.reverseMoveAndRename( entry, newSuperior, new Rdn( "cn=jack doe" ),
        false );

    assertNotNull( reverseds );
    assertEquals( 1, reverseds.size() );

    LdifEntry reversed = reverseds.get( 0 );
    assertEquals( "cn=jack doe,ou=system", reversed.getDn().getName() );
    assertEquals( ChangeType.ModRdn, reversed.getChangeType() );
    assertFalse( reversed.isDeleteOldRdn() );
    assertEquals( "cn=john doe", reversed.getNewRdn() );
    assertEquals( "dc=example, dc=com", Strings.trim( reversed.getNewSuperior() ) );
    assertNull( reversed.getEntry() );
}
 
Example 10
Source File: AttributesFactory.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
private void injectCommon( SchemaObject object, Entry entry, SchemaManager schemaManager )
    throws LdapException
{
    injectNames( object.getNames(), entry, schemaManager );
    entry.put( MetaSchemaConstants.M_OBSOLETE_AT, getBoolean( object.isObsolete() ) );
    entry.put( MetaSchemaConstants.M_OID_AT, object.getOid() );

    if ( object.getDescription() != null )
    {
        entry.put( MetaSchemaConstants.M_DESCRIPTION_AT, object.getDescription() );
    }

    // The extensions
    Map<String, List<String>> extensions = object.getExtensions();

    if ( extensions != null )
    {
        for ( Map.Entry<String, List<String>> mapEntry : extensions.entrySet() )
        {
            String key = mapEntry.getKey();
            List<String> values = mapEntry.getValue();

            for ( String value : values )
            {
                entry.add( key, value );
            }
        }
    }
}
 
Example 11
Source File: SuffixDAO.java    From directory-fortress-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param se
 * @throws org.apache.directory.fortress.core.CreateException
 */
void create( Suffix se )
    throws CreateException
{
    LdapConnection ld = null;
    String nodeDn = getDn( se );
    try
    {
        LOG.info( "create suffix dn [{}]", nodeDn );
        Entry myEntry = new DefaultEntry( nodeDn );
        myEntry.add( SchemaConstants.OBJECT_CLASS_AT, SUFFIX_OBJ_CLASS );
        myEntry.add( SchemaConstants.DC_AT, se.getName() );
        myEntry.add( SchemaConstants.O_AT, se.getDescription() );

        ld = getAdminConnection();
        add( ld, myEntry );
    }
    catch ( LdapException e )
    {
        String error = "create container node dn [" + nodeDn + "] caught LDAPException="
            + e;
        throw new CreateException( GlobalErrIds.SUFX_CREATE_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
}
 
Example 12
Source File: AdminRoleDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * Create a new AdminRole entity using supplied data.  Required attribute is {@link org.apache.directory.fortress.core.model.AdminRole#name}.
 * This data will be stored in the {@link GlobalIds#ADMIN_ROLE_ROOT} container.
 *
 * @param entity record contains AdminRole data.  Null attributes will be ignored.
 * @return input record back to client.
 * @throws org.apache.directory.fortress.core.CreateException in the event LDAP errors occur.
 */
AdminRole create( AdminRole entity ) throws CreateException
{
    LdapConnection ld = null;
    String dn = getDn( entity );

    try
    {
        Entry entry = new DefaultEntry( dn );

        entry.add( SchemaConstants.OBJECT_CLASS_AT, ADMIN_ROLE_OBJ_CLASS );
        entity.setId();
        entry.add( GlobalIds.FT_IID, entity.getId() );
        entry.add( ROLE_NM, entity.getName() );

        // description field is optional on this object class:
        if ( StringUtils.isNotEmpty( entity.getDescription() ) )
        {
            entry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
        }

        // CN attribute is required for this object class:
        entry.add( SchemaConstants.CN_AT, entity.getName() );
        entry.add( GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint( entity ) );
        loadAttrs( entity.getOsPSet(), entry, ROLE_OSP );
        loadAttrs( entity.getOsUSet(), entry, ROLE_OSU );
        String szRaw = entity.getRoleRangeRaw();

        if ( StringUtils.isNotEmpty( szRaw ) )
        {
            entry.add( ROLE_RANGE, szRaw );
        }

        // These multi-valued attributes are optional.  The utility function will return quietly if no items are loaded into collection:
        loadAttrs( entity.getParents(), entry, GlobalIds.PARENT_NODES );

        ld = getAdminConnection();
        add( ld, entry, entity );
    }
    catch ( LdapException e )
    {
        String error = "create role [" + entity.getName() + "] caught LdapException=" + e;
        throw new CreateException( GlobalErrIds.ARLE_ADD_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example 13
Source File: SdDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 */
SDSet create( SDSet entity ) throws CreateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );
    String[] objectClass = SSD_OBJ_CLASS;

    if ( entity.getType() == SDSet.SDType.DYNAMIC )
    {
        objectClass = DSD_OBJ_CLASS;
    }

    try
    {
        Entry entry = new DefaultEntry( dn );
        entry.add( createAttributes( SchemaConstants.OBJECT_CLASS_AT, objectClass ) );
        entity.setId();
        entry.add( GlobalIds.FT_IID, entity.getId() );
        entry.add( SD_SET_NM, entity.getName() );

        // description field is optional on this object class:
        if ( StringUtils.isNotEmpty( entity.getDescription() ) )
        {
            entry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
        }

        // CN attribute is required for this object class:
        entry.add( SchemaConstants.CN_AT, entity.getName() );
        loadAttrs( entity.getMembers(), entry, ROLES );
        entry.add( SD_SET_CARDINALITY, "" + entity.getCardinality() );

        ld = getAdminConnection();
        add( ld, entry, entity );
    }
    catch ( LdapException e )
    {
        String error = "create SD set name [" + entity.getName() + "] type [" + entity.getType()
            + "] caught LdapException=" + e;
        int errCode;
        if ( entity.getType() == SDSet.SDType.DYNAMIC )
        {
            errCode = GlobalErrIds.DSD_ADD_FAILED;
        }
        else
        {
            errCode = GlobalErrIds.SSD_ADD_FAILED;
        }

        throw new CreateException( errCode, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
    return entity;
}
 
Example 14
Source File: MiniKdc.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void initDirectoryService() throws Exception {
  ds = new DefaultDirectoryService();
  ds.setInstanceLayout(new InstanceLayout(workDir));

  CacheService cacheService = new CacheService();
  ds.setCacheService(cacheService);

  // first load the schema
  InstanceLayout instanceLayout = ds.getInstanceLayout();
  File schemaPartitionDirectory = new File(
          instanceLayout.getPartitionsDirectory(), "schema");
  SchemaLdifExtractor extractor = new DefaultSchemaLdifExtractor(
          instanceLayout.getPartitionsDirectory());
  extractor.extractOrCopy();

  SchemaLoader loader = new LdifSchemaLoader(schemaPartitionDirectory);
  SchemaManager schemaManager = new DefaultSchemaManager(loader);
  schemaManager.loadAllEnabled();
  ds.setSchemaManager(schemaManager);
  // Init the LdifPartition with schema
  LdifPartition schemaLdifPartition = new LdifPartition(schemaManager);
  schemaLdifPartition.setPartitionPath(schemaPartitionDirectory.toURI());

  // The schema partition
  SchemaPartition schemaPartition = new SchemaPartition(schemaManager);
  schemaPartition.setWrappedPartition(schemaLdifPartition);
  ds.setSchemaPartition(schemaPartition);

  JdbmPartition systemPartition = new JdbmPartition(ds.getSchemaManager());
  systemPartition.setId("system");
  systemPartition.setPartitionPath(new File(
          ds.getInstanceLayout().getPartitionsDirectory(),
          systemPartition.getId()).toURI());
  systemPartition.setSuffixDn(new Dn(ServerDNConstants.SYSTEM_DN));
  systemPartition.setSchemaManager(ds.getSchemaManager());
  ds.setSystemPartition(systemPartition);

  ds.getChangeLog().setEnabled(false);
  ds.setDenormalizeOpAttrsEnabled(true);
  ds.addLast(new KeyDerivationInterceptor());

  // create one partition
  String orgName= conf.getProperty(ORG_NAME).toLowerCase(Locale.ENGLISH);
  String orgDomain = conf.getProperty(ORG_DOMAIN).toLowerCase(Locale.ENGLISH);

  JdbmPartition partition = new JdbmPartition(ds.getSchemaManager());
  partition.setId(orgName);
  partition.setPartitionPath(new File(
          ds.getInstanceLayout().getPartitionsDirectory(), orgName).toURI());
  partition.setSuffixDn(new Dn("dc=" + orgName + ",dc=" + orgDomain));
  ds.addPartition(partition);
  // indexes
  Set<Index<?, ?, String>> indexedAttributes = new HashSet<Index<?, ?, String>>();
  indexedAttributes.add(new JdbmIndex<String, Entry>("objectClass", false));
  indexedAttributes.add(new JdbmIndex<String, Entry>("dc", false));
  indexedAttributes.add(new JdbmIndex<String, Entry>("ou", false));
  partition.setIndexedAttributes(indexedAttributes);

  // And start the ds
  ds.setInstanceId(conf.getProperty(INSTANCE));
  ds.startup();
  // context entry, after ds.startup()
  Dn dn = new Dn("dc=" + orgName + ",dc=" + orgDomain);
  Entry entry = ds.newEntry(dn);
  entry.add("objectClass", "top", "domain");
  entry.add("dc", orgName);
  ds.getAdminSession().add(entry);
}
 
Example 15
Source File: RoleDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * @param entity
 * @return
 * @throws CreateException
 */
Role create( Role entity ) throws CreateException
{
    LdapConnection ld = null;
    String dn = getDn( entity.getName(), entity.getContextId() );

    try
    {
        Entry entry = new DefaultEntry( dn );
        entry.add( SchemaConstants.OBJECT_CLASS_AT, ROLE_OBJ_CLASS );
        entity.setId();
        entry.add( GlobalIds.FT_IID, entity.getId() );
        entry.add( ROLE_NM, entity.getName() );
        // description field is optional on this object class:
        if ( StringUtils.isNotEmpty( entity.getDescription() ) )
        {
            entry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
        }

        // CN attribute is required for this object class:
        entry.add( SchemaConstants.CN_AT, entity.getName() );
        entry.add( GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint( entity ) );

        // These multi-valued attributes are optional.  The utility function will return quietly if items are not loaded into collection:
        loadAttrs( entity.getParents(), entry, GlobalIds.PARENT_NODES );

        if ( IS_RFC2307 )
        {
            // Supporting RFC2307 posixGroups attributes on fortress roles.
            loadGidNumber( entity );
            entry.add( GlobalIds.GID_NUMBER, entity.getGidNumber() );
        }

        ld = getAdminConnection();
        add( ld, entry, entity );
    }
    catch ( LdapException e )
    {
        String error = "create role [" + entity.getName() + "] caught LdapException=" + e;
        throw new CreateException( GlobalErrIds.ROLE_ADD_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example 16
Source File: SchemaAwareEntryTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method for equals()
 */
@Test
public void testEqualsObject() throws LdapException
{
    Entry entry1 = new DefaultEntry();
    Entry entry2 = new DefaultEntry();

    assertEquals( entry1, entry2 );

    entry1.setDn( exampleDn );
    assertNotSame( entry1, entry2 );

    entry2.setDn( exampleDn );
    assertEquals( entry1, entry2 );

    Attribute attrOC = new DefaultAttribute( "objectClass", "top", "person" );
    Attribute attrCN = new DefaultAttribute( "cn", "test1", "test2" );
    Attribute attrSN = new DefaultAttribute( "sn", "Test1", "Test2" );
    Attribute attrPWD = new DefaultAttribute( "userPassword", BYTES1, BYTES2 );

    entry1.put( attrOC, attrCN, attrSN, attrPWD );
    entry2.put( attrOC, attrCN, attrSN );
    assertNotSame( entry1, entry2 );

    entry2.put( attrPWD );
    assertEquals( entry1, entry2 );

    Attribute attrL1 = new DefaultAttribute( "l", "Paris", "New-York" );
    Attribute attrL2 = new DefaultAttribute( "l", "Paris", "Tokyo" );

    entry1.put( attrL1 );
    entry2.put( attrL1 );
    assertEquals( entry1, entry2 );

    entry1.add( "l", "London" );
    assertNotSame( entry1, entry2 );

    entry2.add( attrL2 );
    assertNotSame( entry1, entry2 );

    entry1.clear();
    entry2.clear();
    assertEquals( entry1, entry2 );
}
 
Example 17
Source File: PermDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param entity
 * @param attributeSetName
 * @return
 * @throws CreateException
 */
PermissionAttribute createPermissionAttribute( PermissionAttribute entity, String attributeSetName ) throws CreateException
{
    LdapConnection ld = null;
    String dn = getDn( entity, attributeSetName, entity.getContextId() );

    try
    {
        Entry entry = new DefaultEntry( dn );

        entry.add( SchemaConstants.OBJECT_CLASS_AT, PERM_ATTR_OBJ_CLASS );

        // this will generate a new random, unique id on this entity:
        entity.setInternalId();            
        
        // create the internal id:
        entry.add( GlobalIds.FT_IID, entity.getInternalId() );
        
        entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE, entity.getAttributeName() );
        entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_SET, attributeSetName );
        
        // description is optional
        if ( StringUtils.isNotEmpty( entity.getDescription() ) )
        {
            entry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
        }

        if ( StringUtils.isNotEmpty( entity.getDataType() ) )
        {
            entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_DATA_TYPE, entity.getDataType() );
        }

        if ( StringUtils.isNotEmpty( entity.getDefaultOperator() ) )
        {
            entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_DEFAULT_OPERATOR, entity.getDefaultOperator() );
        }
        
        if ( StringUtils.isNotEmpty( entity.getDefaultStrategy() ) )
        {
            entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_DEFAULT_STRATEGY, entity.getDefaultStrategy() );
        }
        
        if ( StringUtils.isNotEmpty( entity.getDefaultValue() ) )
        {
            entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_DEFAULT_VALUE, entity.getDefaultValue() );
        }
        
        //add one to many valid values
        for(String validValue : entity.getValidValues()){
        	entry.add( GlobalIds.FT_PERMISSION_ATTRIBUTE_VALID_VALUES, validValue );
        }
        
        // organizational name requires CN attribute:
        entry.add( SchemaConstants.CN_AT, entity.getAttributeName() );    
        
        
        // now add the new entry to directory:
        ld = getAdminConnection();
        add( ld, entry, entity );
        entity.setDn( dn );
    }
    catch ( LdapException e )
    {
        String error = "createPermissionAttribute name [" + entity.getAttributeName() + "] caught LdapException=" + e;
        throw new CreateException( GlobalErrIds.PERM_ATTR_ADD_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }
	
	return entity;    	
}
 
Example 18
Source File: PermDAO.java    From directory-fortress-core with Apache License 2.0 4 votes vote down vote up
/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 *
 */
PermObj createObject( PermObj entity ) throws CreateException
{
    LdapConnection ld = null;
    String dn = getDn( entity, entity.getContextId() );

    try
    {
        Entry entry = new DefaultEntry( dn );
        entry.add( SchemaConstants.OBJECT_CLASS_AT, PERM_OBJ_OBJ_CLASS );
        entry.add( GlobalIds.POBJ_NAME, entity.getObjName() );

        // this will generatre a new random, unique id on this entity:
        entity.setInternalId();

        // create the rDN:
        entry.add( GlobalIds.FT_IID, entity.getInternalId() );

        // ou is required:
        entry.add( SchemaConstants.OU_AT, entity.getOu() );

        // description is optional:
        if ( StringUtils.isNotEmpty( entity.getDescription() ) )
        {
            entry.add( SchemaConstants.DESCRIPTION_AT, entity.getDescription() );
        }

        // type is optional:
        if ( StringUtils.isNotEmpty( entity.getType() ) )
        {
            entry.add( GlobalIds.TYPE, entity.getType() );
        }

        // props are optional as well:
        //if the props is null don't try to load these attributes
        if ( PropUtil.isNotEmpty( entity.getProperties() ) )
        {
            loadProperties( entity.getProperties(), entry, GlobalIds.PROPS );
        }

        // now add the new entry to directory:
        ld = getAdminConnection();
        add( ld, entry, entity );
        entity.setDn( dn );
    }
    catch ( LdapException e )
    {
        String error = "createObject perm obj [" + entity.getObjName() + "] caught LdapException="
            + e;
        throw new CreateException( GlobalErrIds.PERM_ADD_FAILED, error, e );
    }
    finally
    {
        closeAdminConnection( ld );
    }

    return entity;
}
 
Example 19
Source File: LdapDirectoryServerConnectionTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
public void testUserCreation() {
        LdapConnection connection = new LdapNetworkConnection( "localhost", 10389 );
        try {
            connection.bind( "uid=admin,ou=system", "secret" );

            connection.add(new DefaultEntry(
                    "ou=acsadmins,ou=users,ou=system",
            "objectClass: organizationalUnit",
// might also need to be           objectClass: top
            "ou: acsadmins"
            ));
            connection.add(new DefaultEntry(
                    "uid=dahn,ou=acsadmins,ou=users,ou=system",
                    "objectClass: inetOrgPerson",
                    "objectClass: top",
                    "cn: dahn",
                    "sn: Hoogland",
                    "givenName: Daan",
                    "mail: [email protected]",
                    "uid: dahn"
            ));

            connection.add(
                    new DefaultEntry(
                            "cn=JuniorAdmins,ou=groups,ou=system", // The Dn
                            "objectClass: groupOfUniqueNames",
                            "ObjectClass: top",
                            "cn: JuniorAdmins",
                            "uniqueMember: uid=dahn,ou=acsadmins,ou=system,ou=users") );

            assertTrue( connection.exists( "cn=JuniorAdmins,ou=groups,ou=system" ) );
            assertTrue( connection.exists( "uid=dahn,ou=acsadmins,ou=users,ou=system" ) );

            Entry ourUser = connection.lookup("uid=dahn,ou=acsadmins,ou=users,ou=system");
            ourUser.add("memberOf", "cn=JuniorAdmins,ou=groups,ou=system");
            AddRequest addRequest = new AddRequestImpl();
            addRequest.setEntry( ourUser );
            AddResponse response = connection.add( addRequest );
            assertNotNull( response );
            // We would need to either
//            assertEquals( ResultCodeEnum.SUCCESS, response.getLdapResult().getResultCode() );
            // or have the automatic virtual attribute

            List<LdapUser> usahs = ldapManager.getUsers(1L);
            assertEquals("now an admin and a normal user should be present",2, usahs.size());

        } catch (LdapException | NoLdapUserMatchingQueryException e) {
            fail(e.getLocalizedMessage());
        }
    }
 
Example 20
Source File: LdapDataProvider.java    From directory-fortress-core with Apache License 2.0 3 votes vote down vote up
/**
 * Given an ldap attribute name and a set of attribute values, construct an ldap attribute set to be added to
 * directory.
 *
 * @param values   set of type string containing attribute values to load into attribute set.
 * @param entry    contains ldap entry to pull attrs from.
 * @param attrName name of ldap attribute being added.
 * @throws LdapException If we weren't able to add the values into the entry
 */
protected void loadAttrs( Set<String> values, Entry entry, String attrName ) throws LdapException
{
    if ( ( values != null ) && ( values.size() > 0 ) )
    {
        entry.add( attrName, values.toArray( new String[]
            {} ) );
    }
}