Search in sources :

Example 81 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project sonarqube by SonarSource.

the class ApacheDS method importLdif.

/**
 * Stream will be closed automatically.
 */
public void importLdif(InputStream is) throws Exception {
    try (LdifReader reader = new LdifReader(is)) {
        CoreSession coreSession = directoryService.getAdminSession();
        // see LdifFileLoader
        for (LdifEntry ldifEntry : reader) {
            String ldif = ldifEntry.toString();
            LOG.info(ldif);
            if (ChangeType.Add == ldifEntry.getChangeType() || /* assume "add" by default */
            ChangeType.None == ldifEntry.getChangeType()) {
                coreSession.add(new DefaultEntry(coreSession.getDirectoryService().getSchemaManager(), ldifEntry.getEntry()));
            } else if (ChangeType.Modify == ldifEntry.getChangeType()) {
                coreSession.modify(ldifEntry.getDn(), ldifEntry.getModifications());
            } else if (ChangeType.Delete == ldifEntry.getChangeType()) {
                coreSession.delete(ldifEntry.getDn());
            } else {
                throw new IllegalStateException();
            }
        }
    }
}
Also used : LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) CoreSession(org.apache.directory.server.core.api.CoreSession) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Example 82 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-fortress-core by apache.

the class SdDAO method create.

/**
 * @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.getMessage();
        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;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 83 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-fortress-core by apache.

the class PermDAO method createPermissionAttributeSet.

/**
 * @param entity
 * @return
 * @throws CreateException
 */
PermissionAttributeSet createPermissionAttributeSet(PermissionAttributeSet 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_ATTR_SET_OBJ_CLASS);
        entry.add(GlobalIds.FT_PERMISSION_ATTRIBUTE_SET, entity.getName());
        // this will generate a new random, unique id on this entity:
        entity.setInternalId();
        // create the internal id:
        entry.add(GlobalIds.FT_IID, entity.getInternalId());
        // description is optional:
        if (StringUtils.isNotEmpty(entity.getDescription())) {
            entry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        }
        if (StringUtils.isNotEmpty(entity.getType())) {
            entry.add(GlobalIds.FT_PERMISSION_ATTRIBUTE_SET_TYPE, entity.getType());
        }
        // organizational name requires CN attribute:
        entry.add(SchemaConstants.CN_AT, entity.getName());
        // now add the new entry to directory:
        ld = getAdminConnection();
        add(ld, entry, entity);
        entity.setDn(dn);
    } catch (LdapException e) {
        String error = "createPermissionAttributeSet name [" + entity.getName() + "] caught LdapException=" + e.getMessage();
        throw new CreateException(GlobalErrIds.PERM_ADD_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    // add each ftPA
    for (PermissionAttribute pa : entity.getAttributes()) {
        pa.setContextId(entity.getContextId());
        this.createPermissionAttribute(pa, entity.getName());
    }
    return entity;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) PermissionAttribute(org.apache.directory.fortress.core.model.PermissionAttribute) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 84 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-fortress-core by apache.

the class PolicyDAO method create.

/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 */
PwPolicy create(PwPolicy entity) throws CreateException {
    LdapConnection ld = null;
    String dn = getDn(entity);
    try {
        Entry entry = new DefaultEntry(dn);
        entry.add(SchemaConstants.OBJECT_CLASS_AT, PWPOLICY_OBJ_CLASS);
        entry.add(PW_PWD_ID, entity.getName());
        entry.add(PW_ATTRIBUTE, PW_POLICY_EXTENSION);
        if (entity.getMinAge() != null) {
            entry.add(PW_MIN_AGE, entity.getMinAge().toString());
        }
        if (entity.getMaxAge() != null) {
            entry.add(PW_MAX_AGE, entity.getMaxAge().toString());
        }
        if (entity.getInHistory() != null) {
            entry.add(PW_IN_HISTORY, entity.getInHistory().toString());
        }
        if (entity.getCheckQuality() != null) {
            entry.add(PW_CHECK_QUALITY, entity.getCheckQuality().toString());
        }
        if (entity.getMinLength() != null) {
            entry.add(PW_MIN_LENGTH, entity.getMinLength().toString());
        }
        if (entity.getExpireWarning() != null) {
            entry.add(PW_EXPIRE_WARNING, entity.getExpireWarning().toString());
        }
        if (entity.getGraceLoginLimit() != null) {
            entry.add(PW_GRACE_LOGIN_LIMIT, entity.getGraceLoginLimit().toString());
        }
        if (entity.getLockout() != null) {
            /**
             * OpenLDAP requires the pwdLockout boolean value to be upper case:
             */
            entry.add(PW_LOCKOUT, entity.getLockout().toString().toUpperCase());
        }
        if (entity.getLockoutDuration() != null) {
            entry.add(PW_LOCKOUT_DURATION, entity.getLockoutDuration().toString());
        }
        if (entity.getMaxFailure() != null) {
            entry.add(PW_MAX_FAILURE, entity.getMaxFailure().toString());
        }
        if (entity.getFailureCountInterval() != null) {
            entry.add(PW_FAILURE_COUNT_INTERVAL, entity.getFailureCountInterval().toString());
        }
        if (entity.getMustChange() != null) {
            /**
             * OpenLDAP requires the boolean values to be upper case:
             */
            entry.add(PW_MUST_CHANGE, entity.getMustChange().toString().toUpperCase());
        }
        if (entity.getAllowUserChange() != null) {
            /**
             * OpenLDAP requires the boolean values to be upper case:
             */
            entry.add(PW_ALLOW_USER_CHANGE, entity.getAllowUserChange().toString().toUpperCase());
        }
        if (entity.getSafeModify() != null) {
            entry.add(PW_SAFE_MODIFY, entity.getSafeModify().toString().toUpperCase());
        }
        ld = getAdminConnection();
        add(ld, entry, entity);
    } catch (LdapException e) {
        String error = "create name [" + entity.getName() + "] caught LdapException=" + e.getMessage();
        throw new CreateException(GlobalErrIds.PSWD_CREATE_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 85 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project directory-fortress-core by apache.

the class AdminRoleDAO method create.

/**
 * 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.getMessage();
        throw new CreateException(GlobalErrIds.ARLE_ADD_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) CreateException(org.apache.directory.fortress.core.CreateException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)128 Entry (org.apache.directory.api.ldap.model.entry.Entry)116 Test (org.junit.Test)55 DefaultAttribute (org.apache.directory.api.ldap.model.entry.DefaultAttribute)41 Attribute (org.apache.directory.api.ldap.model.entry.Attribute)39 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)23 LdifEntry (org.apache.directory.api.ldap.model.ldif.LdifEntry)20 Modification (org.apache.directory.api.ldap.model.entry.Modification)16 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)16 Dn (org.apache.directory.api.ldap.model.name.Dn)15 CreateException (org.apache.directory.fortress.core.CreateException)15 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)14 Value (org.apache.directory.api.ldap.model.entry.Value)12 LdifReader (org.apache.directory.api.ldap.model.ldif.LdifReader)12 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 SchemaManager (org.apache.directory.api.ldap.model.schema.SchemaManager)5 IOException (java.io.IOException)4 ObjectInputStream (java.io.ObjectInputStream)4 ObjectOutputStream (java.io.ObjectOutputStream)4