Search in sources :

Example 66 with LdapConnection

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

the class UserDAO method resetUserPassword.

/**
 * @param user
 * @throws UpdateException
 */
void resetUserPassword(User user) throws UpdateException {
    LdapConnection ld = null;
    String userDn = getDn(user.getUserId(), user.getContextId());
    try {
        List<Modification> mods = new ArrayList<Modification>();
        mods.add(new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, SchemaConstants.USER_PASSWORD_AT, user.getPassword()));
        mods.add(new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, OPENLDAP_PW_RESET, "TRUE"));
        ld = getAdminConnection();
        modify(ld, userDn, mods, user);
    } catch (LdapException e) {
        String warning = "resetUserPassword userId [" + user.getUserId() + "] caught LDAPException=" + e.getMessage();
        throw new UpdateException(GlobalErrIds.USER_PW_RESET_FAILED, warning, e);
    } finally {
        closeAdminConnection(ld);
    }
}
Also used : DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) Modification(org.apache.directory.api.ldap.model.entry.Modification) DefaultModification(org.apache.directory.api.ldap.model.entry.DefaultModification) ArrayList(java.util.ArrayList) UpdateException(org.apache.directory.fortress.core.UpdateException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 67 with LdapConnection

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

the class UserDAO method findUsers.

/**
 * @param user
 * @return
 * @throws FinderException
 */
List<User> findUsers(User user) throws FinderException {
    List<User> userList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(user.getContextId(), GlobalIds.USER_ROOT);
    try {
        // String filter;
        StringBuilder filterbuf = new StringBuilder();
        if (StringUtils.isNotEmpty(user.getUserId())) {
            // place a wild card after the input userId:
            String searchVal = encodeSafeText(user.getUserId(), GlobalIds.USERID_LEN);
            filterbuf.append(GlobalIds.FILTER_PREFIX);
            filterbuf.append(Config.getInstance().getProperty(USER_OBJECT_CLASS));
            filterbuf.append(")(");
            filterbuf.append(SchemaConstants.UID_AT);
            filterbuf.append("=");
            filterbuf.append(searchVal);
            filterbuf.append("*))");
        } else if (StringUtils.isNotEmpty(user.getInternalId())) {
            // internalUserId search
            String searchVal = encodeSafeText(user.getInternalId(), GlobalIds.USERID_LEN);
            // this is not a wildcard search. Must be exact match.
            filterbuf.append(GlobalIds.FILTER_PREFIX);
            filterbuf.append(Config.getInstance().getProperty(USER_OBJECT_CLASS));
            filterbuf.append(")(");
            filterbuf.append(GlobalIds.FT_IID);
            filterbuf.append("=");
            filterbuf.append(searchVal);
            filterbuf.append("))");
        } else {
            // Beware - returns ALL users!!:"
            filterbuf.append("(objectclass=");
            filterbuf.append(Config.getInstance().getProperty(USER_OBJECT_CLASS));
            filterbuf.append(")");
        }
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), defaultAtrs, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            userList.add(unloadLdapEntry(searchResults.getEntry(), sequence++, user.getContextId()));
        }
    } catch (LdapException e) {
        String warning = "findUsers userRoot [" + userRoot + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "findUsers userRoot [" + userRoot + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_SEARCH_FAILED, warning, e);
    } finally {
        closeAdminConnection(ld);
    }
    return userList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) User(org.apache.directory.fortress.core.model.User) 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 68 with LdapConnection

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

the class UserDAO method getUserRoles.

/**
 * @param userId
 * @return
 * @throws FinderException
 */
private List<UserRole> getUserRoles(String userId, String contextId) throws FinderException {
    List<UserRole> roles = null;
    LdapConnection ld = null;
    String userDn = getDn(userId, contextId);
    try {
        ld = getAdminConnection();
        Entry findEntry = read(ld, userDn, ROLE_ATR);
        roles = unloadUserRoles(findEntry, userId, contextId, null);
    } catch (LdapNoSuchObjectException e) {
        String warning = "getUserRoles COULD NOT FIND ENTRY for user [" + userId + "]";
        throw new FinderException(GlobalErrIds.USER_NOT_FOUND, warning);
    } catch (LdapException e) {
        String error = "getUserRoles [" + userDn + "]= caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_READ_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return roles;
}
Also used : LdapNoSuchObjectException(org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException) FinderException(org.apache.directory.fortress.core.FinderException) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) UserRole(org.apache.directory.fortress.core.model.UserRole) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 69 with LdapConnection

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

the class UserDAO method getAssignedUserIds.

/**
 * @param role
 * @return
 * @throws FinderException
 */
List<String> getAssignedUserIds(Role role) throws FinderException {
    List<String> userList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(role.getContextId(), GlobalIds.USER_ROOT);
    try {
        String roleVal = encodeSafeText(role.getName(), GlobalIds.USERID_LEN);
        StringBuilder filterbuf = new StringBuilder();
        filterbuf.append(GlobalIds.FILTER_PREFIX);
        filterbuf.append(USERS_AUX_OBJECT_CLASS_NAME);
        filterbuf.append(")(");
        filterbuf.append(GlobalIds.USER_ROLE_ASSIGN);
        filterbuf.append("=");
        filterbuf.append(roleVal);
        filterbuf.append("))");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), USERID_ATR, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            userList.add(unloadUser(searchResults.getEntry()));
        }
    } catch (LdapException e) {
        String warning = "getAssignedUserIds role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "getAssignedUserIds role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } finally {
        closeAdminConnection(ld);
    }
    return userList;
}
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 70 with LdapConnection

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

the class UserDAO method getAssignedUsers.

/**
 * @param role
 * @param roleConstraint
 * @return
 * @throws FinderException
 */
List<User> getAssignedUsers(Role role, RoleConstraint roleConstraint) throws FinderException {
    List<User> userList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(role.getContextId(), GlobalIds.USER_ROOT);
    try {
        String roleVal = encodeSafeText(role.getName(), GlobalIds.USERID_LEN);
        StringBuilder filterbuf = new StringBuilder();
        filterbuf.append(GlobalIds.FILTER_PREFIX);
        filterbuf.append(USERS_AUX_OBJECT_CLASS_NAME);
        filterbuf.append(")(");
        filterbuf.append(GlobalIds.USER_ROLE_ASSIGN);
        filterbuf.append("=");
        filterbuf.append(roleVal);
        filterbuf.append(")");
        if (roleConstraint != null) {
            filterbuf.append("(");
            filterbuf.append(GlobalIds.USER_ROLE_DATA);
            filterbuf.append("=");
            filterbuf.append(roleConstraint.getRawData(new UserRole(role.getName())));
            filterbuf.append(")");
        }
        filterbuf.append(")");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), defaultAtrs, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            userList.add(unloadLdapEntry(searchResults.getEntry(), sequence++, role.getContextId()));
        }
    } catch (LdapException e) {
        String warning = "getAssignedUsers role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "getAssignedUsers role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } finally {
        closeAdminConnection(ld);
    }
    return userList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) User(org.apache.directory.fortress.core.model.User) UserRole(org.apache.directory.fortress.core.model.UserRole) 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)

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