Search in sources :

Example 1 with EntryCursorImpl

use of org.apache.directory.ldap.client.api.EntryCursorImpl in project openmeetings by apache.

the class LdapLoginManager method login.

/**
 * Ldap Login
 *
 * Connection Data is retrieved from ConfigurationFile
 *
 * @param _login - user login
 * @param passwd - user password
 * @param domainId - user domain id
 * @return - {@link User} with this credentials or <code>null</code>
 * @throws OmException - in case of any error
 */
public User login(String _login, String passwd, Long domainId) throws OmException {
    log.debug("LdapLoginmanager.doLdapLogin");
    if (!userDao.validLogin(_login)) {
        log.error("Invalid login provided");
        return null;
    }
    User u = null;
    try (LdapWorker w = new LdapWorker(domainId)) {
        String login = w.options.useLowerCase ? _login.toLowerCase() : _login;
        boolean authenticated = true;
        Dn userDn = null;
        Entry entry = null;
        switch(w.options.type) {
            case SEARCHANDBIND:
                {
                    bindAdmin(w.conn, w.options);
                    Dn baseDn = new Dn(w.options.searchBase);
                    String searchQ = String.format(w.options.searchQuery, login);
                    try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(searchQ).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
                        while (cursor.next()) {
                            try {
                                Entry e = cursor.get();
                                if (userDn != null) {
                                    log.error("more than 1 user found in LDAP");
                                    throw UNKNOWN;
                                }
                                userDn = e.getDn();
                                if (w.options.useAdminForAttrs) {
                                    entry = e;
                                }
                            } catch (CursorLdapReferralException cle) {
                                log.warn("Referral LDAP entry found, ignore it");
                            }
                        }
                    }
                    if (userDn == null) {
                        log.error("NONE users found in LDAP");
                        throw BAD_CREDENTIALS;
                    }
                    w.conn.bind(userDn, passwd);
                }
                break;
            case SIMPLEBIND:
                userDn = new Dn(String.format(w.options.userDn, login));
                w.conn.bind(userDn, passwd);
                break;
            case NONE:
            default:
                authenticated = false;
                break;
        }
        u = authenticated ? userDao.getByLogin(login, Type.ldap, domainId) : userDao.login(login, passwd);
        log.debug("getByLogin:: authenticated ? {}, login = '{}', domain = {}, user = {}", authenticated, login, domainId, u);
        if (u == null && Provisionning.AUTOCREATE != w.options.prov) {
            log.error("User not found in OM DB and Provisionning.AUTOCREATE was not set");
            throw BAD_CREDENTIALS;
        }
        if (authenticated && entry == null) {
            if (w.options.useAdminForAttrs) {
                bindAdmin(w.conn, w.options);
            }
            entry = w.conn.lookup(userDn);
        }
        switch(w.options.prov) {
            case AUTOUPDATE:
            case AUTOCREATE:
                u = w.getUser(entry, u);
                if (w.options.syncPasswd) {
                    u.updatePassword(cfgDao, passwd);
                }
                u = userDao.update(u, null);
                break;
            case NONE:
            default:
                break;
        }
    } catch (LdapAuthenticationException ae) {
        log.error("Not authenticated.", ae);
        throw BAD_CREDENTIALS;
    } catch (OmException e) {
        throw e;
    } catch (Exception e) {
        log.error("Unexpected exception.", e);
        throw new OmException(e);
    }
    return u;
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) EntryCursorImpl(org.apache.directory.ldap.client.api.EntryCursorImpl) User(org.apache.openmeetings.db.entity.user.User) GroupUser(org.apache.openmeetings.db.entity.user.GroupUser) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) OmException(org.apache.openmeetings.util.OmException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) OmException(org.apache.openmeetings.util.OmException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Entry(org.apache.directory.api.ldap.model.entry.Entry) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)

Example 2 with EntryCursorImpl

use of org.apache.directory.ldap.client.api.EntryCursorImpl in project openmeetings by apache.

the class LdapLoginManager method importUsers.

public void importUsers(Long domainId, boolean print) throws OmException {
    try (LdapWorker w = new LdapWorker(domainId)) {
        bindAdmin(w.conn, w.options);
        Dn baseDn = new Dn(w.options.searchBase);
        try (EntryCursor cursor = new EntryCursorImpl(w.conn.search(new SearchRequestImpl().setBase(baseDn).setFilter(w.options.importQuery).setScope(w.options.scope).addAttributes("*").setDerefAliases(w.options.derefMode)))) {
            while (cursor.next()) {
                try {
                    Entry e = cursor.get();
                    User u = userDao.getByLogin(getLogin(w.config, e), Type.ldap, domainId);
                    u = w.getUser(e, u);
                    if (print) {
                        log.info("Going to import user: {}", u);
                    } else {
                        userDao.update(u, null);
                        log.info("User {}, was imported", u);
                    }
                } catch (CursorLdapReferralException cle) {
                    log.warn("Referral LDAP entry found, ignore it");
                }
            }
        }
    } catch (LdapAuthenticationException ae) {
        log.error("Not authenticated.", ae);
        throw BAD_CREDENTIALS;
    } catch (OmException e) {
        throw e;
    } catch (Exception e) {
        log.error("Unexpected exception.", e);
        throw new OmException(e);
    }
}
Also used : EntryCursor(org.apache.directory.api.ldap.model.cursor.EntryCursor) EntryCursorImpl(org.apache.directory.ldap.client.api.EntryCursorImpl) Entry(org.apache.directory.api.ldap.model.entry.Entry) User(org.apache.openmeetings.db.entity.user.User) GroupUser(org.apache.openmeetings.db.entity.user.GroupUser) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) SearchRequestImpl(org.apache.directory.api.ldap.model.message.SearchRequestImpl) Dn(org.apache.directory.api.ldap.model.name.Dn) OmException(org.apache.openmeetings.util.OmException) LdapInvalidAttributeValueException(org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException) CursorLdapReferralException(org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) OmException(org.apache.openmeetings.util.OmException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 3 with EntryCursorImpl

use of org.apache.directory.ldap.client.api.EntryCursorImpl in project directory-ldap-api by apache.

the class LdapConnectionTemplate method search.

/**
 * {@inheritDoc}
 */
@Override
public <T> List<T> search(SearchRequest searchRequest, EntryMapper<T> entryMapper) {
    List<T> entries = new ArrayList<>();
    LdapConnection connection = null;
    try {
        connection = connectionPool.getConnection();
        for (Entry entry : new EntryCursorImpl(connection.search(searchRequest))) {
            entries.add(entryMapper.map(entry));
        }
    } catch (LdapException e) {
        throw new LdapRuntimeException(e);
    } finally {
        returnLdapConnection(connection);
    }
    return entries;
}
Also used : EntryCursorImpl(org.apache.directory.ldap.client.api.EntryCursorImpl) Entry(org.apache.directory.api.ldap.model.entry.Entry) ArrayList(java.util.ArrayList) LdapRuntimeException(org.apache.directory.ldap.client.template.exception.LdapRuntimeException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

Entry (org.apache.directory.api.ldap.model.entry.Entry)3 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)3 EntryCursorImpl (org.apache.directory.ldap.client.api.EntryCursorImpl)3 IOException (java.io.IOException)2 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)2 CursorLdapReferralException (org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException)2 EntryCursor (org.apache.directory.api.ldap.model.cursor.EntryCursor)2 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)2 LdapInvalidAttributeValueException (org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException)2 SearchRequestImpl (org.apache.directory.api.ldap.model.message.SearchRequestImpl)2 Dn (org.apache.directory.api.ldap.model.name.Dn)2 GroupUser (org.apache.openmeetings.db.entity.user.GroupUser)2 User (org.apache.openmeetings.db.entity.user.User)2 OmException (org.apache.openmeetings.util.OmException)2 ArrayList (java.util.ArrayList)1 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)1 LdapRuntimeException (org.apache.directory.ldap.client.template.exception.LdapRuntimeException)1