Search in sources :

Example 46 with FinderException

use of org.apache.directory.fortress.core.FinderException in project directory-fortress-core by apache.

the class UserDAO method findUsers.

/**
 * @param user
 * @param limit
 * @return
 * @throws FinderException
 */
List<String> findUsers(User user, int limit) throws FinderException {
    List<String> userList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(user.getContextId(), GlobalIds.USER_ROOT);
    try {
        String searchVal = encodeSafeText(user.getUserId(), GlobalIds.USERID_LEN);
        StringBuilder filterbuf = new StringBuilder();
        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("*))");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), USERID, false, limit);
        while (searchResults.next()) {
            Entry entry = searchResults.getEntry();
            userList.add(getAttribute(entry, SchemaConstants.UID_AT));
        }
    } catch (LdapException e) {
        String warning = "findUsers caught LdapException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "findUsers 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) DefaultEntry(org.apache.directory.api.ldap.model.entry.DefaultEntry) Entry(org.apache.directory.api.ldap.model.entry.Entry) 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 47 with FinderException

use of org.apache.directory.fortress.core.FinderException in project directory-fortress-core by apache.

the class UserDAO method getAssignedUsers.

/**
 * @param roles
 * @return
 * @throws FinderException
 */
Set<String> getAssignedUsers(Set<String> roles, String contextId) throws FinderException {
    Set<String> userSet = new HashSet<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(contextId, GlobalIds.USER_ROOT);
    try {
        StringBuilder filterbuf = new StringBuilder();
        filterbuf.append(GlobalIds.FILTER_PREFIX);
        filterbuf.append(USERS_AUX_OBJECT_CLASS_NAME);
        filterbuf.append(")(|");
        if (CollectionUtils.isNotEmpty(roles)) {
            for (String roleVal : roles) {
                String filteredVal = encodeSafeText(roleVal, GlobalIds.USERID_LEN);
                filterbuf.append("(");
                filterbuf.append(GlobalIds.USER_ROLE_ASSIGN);
                filterbuf.append("=");
                filterbuf.append(filteredVal);
                filterbuf.append(")");
            }
        } else {
            return null;
        }
        filterbuf.append("))");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), USERID_ATRS, false, GlobalIds.BATCH_SIZE);
        while (searchResults.next()) {
            userSet.add(getAttribute(searchResults.getEntry(), SchemaConstants.UID_AT));
        }
    } catch (LdapException e) {
        String warning = "getAssignedUsers caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "getAssignedUsers caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } finally {
        closeAdminConnection(ld);
    }
    return userSet;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) SearchCursor(org.apache.directory.api.ldap.model.cursor.SearchCursor) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) HashSet(java.util.HashSet) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 48 with FinderException

use of org.apache.directory.fortress.core.FinderException in project directory-fortress-core by apache.

the class UserDAO method getUserAdminRoles.

/**
 * @param user
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<UserAdminRole> getUserAdminRoles(User user) throws FinderException {
    List<UserAdminRole> roles = null;
    LdapConnection ld = null;
    String userDn = getDn(user.getUserId(), user.getContextId());
    try {
        ld = getAdminConnection();
        Entry findEntry = read(ld, userDn, AROLE_ATR);
        roles = unloadUserAdminRoles(findEntry, user.getUserId(), user.getContextId());
    } catch (LdapNoSuchObjectException e) {
        String warning = "getUserAdminRoles COULD NOT FIND ENTRY for user [" + user.getUserId() + "]";
        throw new FinderException(GlobalErrIds.USER_NOT_FOUND, warning);
    } catch (LdapException e) {
        String error = "getUserAdminRoles [" + 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) UserAdminRole(org.apache.directory.fortress.core.model.UserAdminRole) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 49 with FinderException

use of org.apache.directory.fortress.core.FinderException in project directory-fortress-core by apache.

the class UserDAO method findUsersList.

/**
 * @param searchVal
 * @return
 * @throws FinderException
 */
List<String> findUsersList(String searchVal, String contextId) throws FinderException {
    List<String> userList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(contextId, GlobalIds.USER_ROOT);
    try {
        searchVal = encodeSafeText(searchVal, GlobalIds.USERID_LEN);
        StringBuilder filterbuf = new StringBuilder();
        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("*))");
        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++, contextId)).getUserId());
        }
    } catch (LdapException e) {
        String warning = "findUsersList caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "findUsersList caught CursorException=" + 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) 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 50 with FinderException

use of org.apache.directory.fortress.core.FinderException in project directory-fortress-core by apache.

the class UserDAO method getRoles.

/**
 * @param user
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<String> getRoles(User user) throws FinderException {
    List<String> roles = null;
    LdapConnection ld = null;
    String userDn = getDn(user.getUserId(), user.getContextId());
    try {
        ld = getAdminConnection();
        Entry findEntry = read(ld, userDn, ROLES);
        if (findEntry == null) {
            String warning = "getRoles userId [" + user.getUserId() + "] not found, Fortress rc=" + GlobalErrIds.USER_NOT_FOUND;
            throw new FinderException(GlobalErrIds.USER_NOT_FOUND, warning);
        }
        roles = getAttributes(findEntry, GlobalIds.USER_ROLE_ASSIGN);
    } catch (LdapNoSuchObjectException e) {
        String warning = "getRoles COULD NOT FIND ENTRY for user [" + user.getUserId() + "]";
        throw new FinderException(GlobalErrIds.USER_NOT_FOUND, warning);
    } catch (LdapException e) {
        String error = "getRoles [" + userDn + "]= caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_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) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Aggregations

FinderException (org.apache.directory.fortress.core.FinderException)80 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)72 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)72 ArrayList (java.util.ArrayList)49 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)48 SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)48 Entry (org.apache.directory.api.ldap.model.entry.Entry)22 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)21 LdapNoSuchObjectException (org.apache.directory.api.ldap.model.exception.LdapNoSuchObjectException)17 Permission (org.apache.directory.fortress.core.model.Permission)10 User (org.apache.directory.fortress.core.model.User)8 SecurityException (org.apache.directory.fortress.core.SecurityException)7 DefaultModification (org.apache.directory.api.ldap.model.entry.DefaultModification)6 Modification (org.apache.directory.api.ldap.model.entry.Modification)6 UpdateException (org.apache.directory.fortress.core.UpdateException)6 Role (org.apache.directory.fortress.core.model.Role)6 UserRole (org.apache.directory.fortress.core.model.UserRole)6 OrgUnit (org.apache.directory.fortress.core.model.OrgUnit)5 AdminRole (org.apache.directory.fortress.core.model.AdminRole)4 RoleConstraint (org.apache.directory.fortress.core.model.RoleConstraint)4