Search in sources :

Example 26 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class SdDAO method search.

/**
 * @param roles
 * @param sdSet
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
Set<SDSet> search(Set<String> roles, SDSet sdSet) throws FinderException {
    Set<SDSet> sdList = new HashSet<>();
    LdapConnection ld = null;
    String ssdRoot = getSdRoot(sdSet.getContextId());
    String objectClass = SSD_OBJECT_CLASS_NM;
    if (sdSet.getType() == SDSet.SDType.DYNAMIC) {
        objectClass = DSD_OBJECT_CLASS_NM;
    }
    try {
        if (CollectionUtils.isNotEmpty(roles)) {
            StringBuilder filterbuf = new StringBuilder();
            filterbuf.append(GlobalIds.FILTER_PREFIX);
            filterbuf.append(objectClass);
            filterbuf.append(")(|");
            for (String rle : roles) {
                filterbuf.append("(");
                filterbuf.append(ROLES);
                filterbuf.append("=");
                filterbuf.append(rle);
                filterbuf.append(")");
            }
            filterbuf.append("))");
            ld = getAdminConnection();
            SearchCursor searchResults = search(ld, ssdRoot, SearchScope.SUBTREE, filterbuf.toString(), SD_SET_ATRS, false, GlobalIds.BATCH_SIZE);
            long sequence = 0;
            while (searchResults.next()) {
                sdList.add(unloadLdapEntry(searchResults.getEntry(), sequence++));
            }
        }
    } catch (LdapException e) {
        String error = "search type [" + sdSet.getType() + "] caught LdapException=" + e.getMessage();
        int errCode;
        if (sdSet.getType() == SDSet.SDType.DYNAMIC) {
            errCode = GlobalErrIds.DSD_SEARCH_FAILED;
        } else {
            errCode = GlobalErrIds.SSD_SEARCH_FAILED;
        }
        throw new FinderException(errCode, error, e);
    } catch (CursorException e) {
        String error = "search type [" + sdSet.getType() + "] caught CursorException=" + e.getMessage();
        int errCode;
        if (sdSet.getType() == SDSet.SDType.DYNAMIC) {
            errCode = GlobalErrIds.DSD_SEARCH_FAILED;
        } else {
            errCode = GlobalErrIds.SSD_SEARCH_FAILED;
        }
        throw new FinderException(errCode, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return sdList;
}
Also used : SDSet(org.apache.directory.fortress.core.model.SDSet) 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 27 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor 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 28 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor 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 29 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor 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 30 with SearchCursor

use of org.apache.directory.api.ldap.model.cursor.SearchCursor in project directory-fortress-core by apache.

the class UserDAO method getAssignedUsers.

/**
 * @param role
 * @return
 * @throws FinderException
 */
List<User> getAssignedUsers(AdminRole role) 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_ADMINROLE_ASSIGN);
        filterbuf.append("=");
        filterbuf.append(roleVal);
        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 admin role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ARLE_USER_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "getAssignedUsers admin role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.ARLE_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)

Aggregations

SearchCursor (org.apache.directory.api.ldap.model.cursor.SearchCursor)55 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)52 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)50 LdapConnection (org.apache.directory.ldap.client.api.LdapConnection)49 FinderException (org.apache.directory.fortress.core.FinderException)48 ArrayList (java.util.ArrayList)44 Entry (org.apache.directory.api.ldap.model.entry.Entry)11 DefaultEntry (org.apache.directory.api.ldap.model.entry.DefaultEntry)7 Permission (org.apache.directory.fortress.core.model.Permission)7 Dn (org.apache.directory.api.ldap.model.name.Dn)5 User (org.apache.directory.fortress.core.model.User)5 IOException (java.io.IOException)4 SearchRequest (org.apache.directory.api.ldap.model.message.SearchRequest)4 SearchRequestImpl (org.apache.directory.api.ldap.model.message.SearchRequestImpl)4 HashSet (java.util.HashSet)3 Response (org.apache.directory.api.ldap.model.message.Response)3 SearchResultEntry (org.apache.directory.api.ldap.model.message.SearchResultEntry)3 AuthZ (org.apache.directory.fortress.core.model.AuthZ)3 SDSet (org.apache.directory.fortress.core.model.SDSet)3 HashMap (java.util.HashMap)2