Search in sources :

Example 61 with DefaultEntry

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

the class SuffixDAO method create.

/**
 * @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.getMessage();
        throw new CreateException(GlobalErrIds.SUFX_CREATE_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
}
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 62 with DefaultEntry

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

the class UserDAO method create.

/**
 * Add new user entity to LDAP
 *
 * @param entity
 * @return
 * @throws CreateException
 */
User create(User entity) throws CreateException {
    LdapConnection ld = null;
    try {
        entity.setInternalId();
        String dn = getDn(entity.getUserId(), entity.getContextId());
        Entry myEntry = new DefaultEntry(dn);
        myEntry.add(SchemaConstants.OBJECT_CLASS_AT, getUserObjectClass());
        // myEntry.add( SchemaConstants.OBJECT_CLASS_AT, USER_OBJ_CLASS );
        myEntry.add(GlobalIds.FT_IID, entity.getInternalId());
        myEntry.add(SchemaConstants.UID_AT, entity.getUserId());
        // CN is required on inetOrgPerson object class, if caller did not set, use the userId:
        if (StringUtils.isEmpty(entity.getCn())) {
            entity.setCn(entity.getUserId());
        }
        myEntry.add(SchemaConstants.CN_AT, entity.getCn());
        // SN is required on inetOrgPerson object class, if caller did not set, use the userId:
        if (StringUtils.isEmpty(entity.getSn())) {
            entity.setSn(entity.getUserId());
        }
        myEntry.add(SchemaConstants.SN_AT, entity.getSn());
        if (StringUtils.isNotEmpty(entity.getPassword())) {
            myEntry.add(SchemaConstants.USER_PASSWORD_AT, entity.getPassword());
        } else if (!Config.getInstance().getBoolean(GlobalIds.USER_CREATION_PASSWORD_FIELD, false)) {
            myEntry.add(SchemaConstants.USER_PASSWORD_AT, new String());
        }
        myEntry.add(SchemaConstants.DISPLAY_NAME_AT, entity.getCn());
        if (StringUtils.isNotEmpty(entity.getTitle())) {
            myEntry.add(SchemaConstants.TITLE_AT, entity.getTitle());
        }
        if (StringUtils.isNotEmpty(entity.getEmployeeType())) {
            myEntry.add(EMPLOYEE_TYPE, entity.getEmployeeType());
        }
        // These are multi-valued attributes, use the util function to load.
        // These items are optional.  The utility function will return quietly if item list is empty:
        loadAttrs(entity.getPhones(), myEntry, SchemaConstants.TELEPHONE_NUMBER_AT);
        loadAttrs(entity.getMobiles(), myEntry, MOBILE);
        loadAttrs(entity.getEmails(), myEntry, SchemaConstants.MAIL_AT);
        // The following attributes are optional:
        if (entity.isSystem() != null) {
            myEntry.add(SYSTEM_USER, entity.isSystem().toString().toUpperCase());
        }
        // If password policy is set and either openldap or apacheds in use:
        if ((Config.getInstance().isOpenldap() || Config.getInstance().isApacheds()) && StringUtils.isNotEmpty(entity.getPwPolicy())) {
            myEntry.add(OPENLDAP_POLICY_SUBENTRY, PolicyDAO.getPolicyDn(entity));
        }
        if (StringUtils.isNotEmpty(entity.getOu())) {
            myEntry.add(SchemaConstants.OU_AT, entity.getOu());
        }
        if (StringUtils.isNotEmpty(entity.getDescription())) {
            myEntry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        }
        // props are optional as well:
        // Add "initial" property here.
        entity.addProperty("initAttrArrays", "");
        loadProperties(entity.getProperties(), myEntry, GlobalIds.PROPS);
        // map the userid to the name field in constraint:
        entity.setName(entity.getUserId());
        myEntry.add(GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint(entity));
        loadAddress(entity.getAddress(), myEntry);
        if (ArrayUtils.isNotEmpty(entity.getJpegPhoto())) {
            myEntry.add(JPEGPHOTO, entity.getJpegPhoto());
        }
        ld = getAdminConnection();
        add(ld, myEntry, entity);
        entity.setDn(dn);
    } catch (LdapEntryAlreadyExistsException e) {
        String error = "create userId [" + entity.getUserId() + "] failed, already exists in directory";
        throw new CreateException(GlobalErrIds.USER_ADD_FAILED_ALREADY_EXISTS, error, e);
    } catch (LdapException e) {
        String error = "create userId [" + entity.getUserId() + "] caught LDAPException=" + e.getMessage();
        throw new CreateException(GlobalErrIds.USER_ADD_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : LdapEntryAlreadyExistsException(org.apache.directory.api.ldap.model.exception.LdapEntryAlreadyExistsException) 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) CreateException(org.apache.directory.fortress.core.CreateException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 63 with DefaultEntry

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

the class ExampleDAO method create.

/**
 * @param entity
 * @return
 * @throws org.apache.directory.fortress.core.CreateException
 */
public Example create(Example entity) throws CreateException {
    LdapConnection ld = null;
    String dn = SchemaConstants.CN_AT + "=" + entity.getName() + "," + Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
    if (LOG.isDebugEnabled()) {
        LOG.debug("create dn [" + dn + "]");
    }
    try {
        /*
        public class Example
                implements Constraint, java.io.Serializable
        {
            private String id;          // this maps to oamId
            private String name;          // this is oamRoleName
            private String description; // this is description
            private String dn;          // this attribute is automatically saved to each ldap record.
            private String beginTime;     // this attribute is oamBeginTime
            private String endTime;        // this attribute is oamEndTime
            private String beginDate;    // this attribute is oamBeginDate
            private String endDate;        // this attribute is oamEndDate
            private String beginLockDate;// this attribute is oamBeginLockDate
            private String endLockDate;    // this attribute is oamEndLockDate
            private String dayMask;        // this attribute is oamDayMask
            private int timeout;        // this attribute is oamTimeOut
            */
        ld = getAdminConnection();
        Entry entry = new DefaultEntry(dn);
        entry.add(createAttributes(SchemaConstants.OBJECT_CLASS_AT, EIds.EXAMPLE_OBJ_CLASS));
        entity.setId();
        entry.add(GlobalIds.FT_IID, entity.getId());
        entry.add(EIds.EXAMPLE_NM, entity.getName());
        if (entity.getDescription() != null && entity.getDescription().length() > 0)
            entry.add(SchemaConstants.DESCRIPTION_AT, entity.getDescription());
        // organizational name requires CN attribute:
        entry.add(SchemaConstants.CN_AT, entity.getName());
        // AttrHelper.loadTemporalAttrs(entity, attrs);
        entity.setName("EXAMPLE");
        entry.add(GlobalIds.CONSTRAINT, ConstraintUtil.setConstraint(entity));
        add(ld, entry);
    } catch (LdapException e) {
        String error = "create [" + entity.getName() + "] caught LDAPException=" + e;
        LOG.error(error);
        throw new CreateException(EErrIds.EXAMPLE_ADD_FAILED, error);
    } 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 64 with DefaultEntry

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

the class LDAPServerService method initSchema.

// ----- private methods -----
private void initSchema(final SchemaManager schemaManager, final CoreSession adminSession, final StructrPartition partition) throws Exception {
    final URL url = SchemaEntityFactory.class.getProtectionDomain().getCodeSource().getLocation();
    final JarFile jarFile = new JarFile(new File(url.toURI()));
    final List<String> names = new LinkedList<>();
    for (final Enumeration<JarEntry> e = jarFile.entries(); e.hasMoreElements(); ) {
        final ZipEntry zipEntry = e.nextElement();
        final String name = zipEntry.getName();
        if (name.startsWith("schema/") && name.endsWith(".ldif")) {
            names.add(name);
        }
    }
    Collections.sort(names, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            // first sort key: number of slashes (level)
            Integer s1 = StringUtils.countMatches(o1, "/");
            Integer s2 = StringUtils.countMatches(o2, "/");
            if (s1.equals(s2)) {
                // secondary sort key: string length
                Integer l1 = o1.length();
                Integer l2 = o2.length();
                if (l1.equals(l2)) {
                    // tertiary sort key: the string values
                    return o1.compareTo(o2);
                }
                return l1.compareTo(l2);
            }
            return s1.compareTo(s2);
        }
    });
    for (final String name : names) {
        try (final LdifReader reader = new LdifReader(jarFile.getInputStream(jarFile.getEntry(name)))) {
            for (final LdifEntry entry : reader) {
                final Entry schemaEntry = new DefaultEntry(schemaManager, entry.getEntry());
                final Dn dn = schemaEntry.getDn();
                if (!partition.hasEntry(new HasEntryOperationContext(adminSession, dn))) {
                    logger.info("Importing {}...", name);
                    partition.add(new AddOperationContext(adminSession, schemaEntry));
                }
            }
        }
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Dn(org.apache.directory.api.ldap.model.name.Dn) AddOperationContext(org.apache.directory.server.core.api.interceptor.context.AddOperationContext) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) LinkedList(java.util.LinkedList) HasEntryOperationContext(org.apache.directory.server.core.api.interceptor.context.HasEntryOperationContext) LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) JarEntry(java.util.jar.JarEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) ZipEntry(java.util.zip.ZipEntry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry) JarFile(java.util.jar.JarFile) File(java.io.File) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

Example 65 with DefaultEntry

use of org.apache.directory.api.ldap.model.entry.DefaultEntry in project activemq-artemis by apache.

the class SaslKrb5LDAPSecurityTest method createPrincipal.

public synchronized void createPrincipal(String principal, String password) throws Exception {
    String baseDn = getKdcServer().getSearchBaseDn();
    String content = "dn: uid=" + principal + "," + baseDn + "\n" + "objectClass: top\n" + "objectClass: person\n" + "objectClass: inetOrgPerson\n" + "objectClass: krb5principal\n" + "objectClass: krb5kdcentry\n" + "cn: " + principal + "\n" + "sn: " + principal + "\n" + "uid: " + principal + "\n" + "userPassword: " + password + "\n" + // using businessCategory as a proxy for memberoOf attribute pending: https://issues.apache.org/jira/browse/DIRSERVER-1844
    "businessCategory: " + "cn=admins,ou=system" + "\n" + "businessCategory: " + "cn=bees,ou=system" + "\n" + "krb5PrincipalName: " + principal + "@" + getRealm() + "\n" + "krb5KeyVersionNumber: 0";
    for (LdifEntry ldifEntry : new LdifReader(new StringReader(content))) {
        service.getAdminSession().add(new DefaultEntry(service.getSchemaManager(), ldifEntry.getEntry()));
    }
}
Also used : LdifReader(org.apache.directory.api.ldap.model.ldif.LdifReader) StringReader(java.io.StringReader) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) LdifEntry(org.apache.directory.api.ldap.model.ldif.LdifEntry)

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