Search in sources :

Example 91 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project directory-fortress-core by apache.

the class ExampleDAO method findExamples.

/**
 * @param searchVal
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
public List<Example> findExamples(String searchVal) throws FinderException {
    List<Example> exampleList = new ArrayList<>();
    LdapConnection ld = null;
    String exampleRoot = Config.getInstance().getProperty(EIds.EXAMPLE_ROOT);
    if (LOG.isDebugEnabled()) {
        LOG.debug("findExamples: " + EIds.EXAMPLE_ROOT + " [" + exampleRoot + "]");
    }
    try {
        searchVal = encodeSafeText(searchVal, GlobalIds.ROLE_LEN);
        ld = getAdminConnection();
        String filter = GlobalIds.FILTER_PREFIX + Arrays.toString(EIds.EXAMPLE_OBJ_CLASS) + ")(" + EIds.EXAMPLE_NM + "=" + searchVal + "*))";
        SearchCursor searchResults = search(ld, exampleRoot, SearchScope.SUBTREE, filter, EXAMPLE_ATRS, false, GlobalIds.BATCH_SIZE);
        while (searchResults.next()) {
            exampleList.add(getEntityFromLdapEntry(searchResults.getEntry()));
        }
    } catch (LdapException e) {
        String error = "findExamples caught LDAPException=" + e;
        LOG.warn(error);
        throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error);
    } catch (CursorException e) {
        String error = "findExamples caught CursorException=" + e;
        throw new FinderException(EErrIds.EXAMPLE_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return exampleList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) ArrayList(java.util.ArrayList) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 92 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection 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 93 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project structr by structr.

the class LDAPService method doUpdate.

public void doUpdate() throws IOException, LdapException, CursorException, FrameworkException {
    final LdapConnection connection = new LdapNetworkConnection(host, port, useSsl);
    final App app = StructrApp.getInstance();
    if (connection != null) {
        // make connection persistent
        connection.setTimeOut(0);
        if (connection.connect()) {
            logger.info("Updating user/group information from LDAP server {}:{}..", new Object[] { host, port });
            if (StringUtils.isNotBlank(binddn) && StringUtils.isNotBlank(secret)) {
                connection.bind(binddn, secret);
            } else if (StringUtils.isNotBlank(binddn)) {
                connection.bind(binddn);
            }
            // step 1: fetch / update all users from LDAP server
            final EntryCursor cursor = connection.search(baseDn, filter, SearchScope.valueOf(scope));
            while (cursor.next()) {
                final Entry entry = cursor.get();
                synchronizeUserEntry(connection, entry);
            }
            // step 2: examine local users and refresh / remove
            try (final Tx tx = app.tx()) {
                for (final LDAPUser user : app.nodeQuery(LDAPUser.class).getAsList()) {
                    final String dn = user.getDistinguishedName();
                    if (dn != null) {
                        final Entry userEntry = connection.lookup(dn);
                        if (userEntry != null) {
                            // update user information
                            user.initializeFrom(userEntry);
                        } else {
                            logger.info("User {} doesn't exist in LDAP directory, deleting.", user);
                            app.delete(user);
                        }
                    } else {
                        logger.warn("User {} doesn't have an LDAP distinguished name, ignoring.", user);
                    }
                }
                tx.success();
            }
            cursor.close();
            connection.close();
        } else {
            logger.info("Connection to LDAP server {} failed", host);
        }
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) Entry(org.apache.directory.api.ldap.model.entry.Entry) Tx(org.structr.core.graph.Tx) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 94 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project structr by structr.

the class LDAPService method fetchObjectInfo.

// ----- public methods -----
public String fetchObjectInfo(final String dn) {
    final LdapConnection connection = new LdapNetworkConnection(host, port, useSsl);
    final StringBuilder buf = new StringBuilder();
    if (connection != null) {
        try {
            if (connection.connect()) {
                if (StringUtils.isNotBlank(binddn) && StringUtils.isNotBlank(secret)) {
                    connection.bind(binddn, secret);
                } else if (StringUtils.isNotBlank(binddn)) {
                    connection.bind(binddn);
                }
                final EntryCursor cursor = connection.search(dn, "(objectclass=*)", SearchScope.OBJECT);
                while (cursor.next()) {
                    buf.append(cursor.get());
                    buf.append("\n");
                }
                cursor.close();
                connection.close();
            }
            connection.close();
        } catch (CursorException | LdapException | IOException ex) {
            logger.warn("", ex);
        }
    }
    return buf.toString();
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 95 with LdapConnection

use of org.apache.directory.ldap.client.api.LdapConnection in project activemq-artemis by apache.

the class CachedLDAPAuthorizationModuleLegacyTest method getLdapConnection.

@Override
protected LdapConnection getLdapConnection() throws LdapException, IOException {
    LdapConnection connection = new LdapNetworkConnection("localhost", getLdapServer().getPort());
    connection.bind(new Dn("uid=admin,ou=system"), "secret");
    return connection;
}
Also used : Dn(org.apache.directory.shared.ldap.model.name.Dn) LdapNetworkConnection(org.apache.directory.ldap.client.api.LdapNetworkConnection) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)180 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)166 ArrayList (java.util.ArrayList)90 FinderException (org.apache.directory.fortress.core.FinderException)73 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)65 Entry (org.apache.directory.api.ldap.model.entry.Entry)52 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)49 Modification (org.apache.directory.api.ldap.model.entry.Modification)43 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)41 UpdateException (org.apache.directory.fortress.core.UpdateException)41 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)37 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)20 CreateException (org.apache.directory.fortress.core.CreateException)17 RemoveException (org.apache.directory.fortress.core.RemoveException)17 IOException (java.io.IOException)14 LdapNetworkConnection (org.apache.directory.ldap.client.api.LdapNetworkConnection)14 Permission (org.apache.directory.fortress.core.model.Permission)9 Dn (org.apache.directory.api.ldap.model.name.Dn)7 EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)6 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)6