Search in sources :

Example 21 with FinderException

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

the class OrgUnitDAO method findByKey.

/**
 * @param entity
 * @return
 * @throws FinderException
 */
OrgUnit findByKey(OrgUnit entity) throws FinderException {
    OrgUnit oe = null;
    LdapConnection ld = null;
    Dn dn = getDn(entity);
    try {
        ld = getAdminConnection();
        Entry findEntry = read(ld, dn, ORGUNIT_ATRS);
        if (findEntry == null) {
            String warning = "findByKey orgUnit name [" + entity.getName() + "] type [" + entity.getType() + "] COULD NOT FIND ENTRY for dn [" + dn + "]";
            int errCode;
            if (entity.getType() == OrgUnit.Type.PERM) {
                errCode = GlobalErrIds.ORG_NOT_FOUND_PERM;
            } else {
                errCode = GlobalErrIds.ORG_NOT_FOUND_USER;
            }
            throw new FinderException(errCode, warning);
        }
        oe = getEntityFromLdapEntry(findEntry, 0, entity.getContextId());
    } catch (LdapNoSuchObjectException e) {
        String warning = "findByKey orgUnit name [" + entity.getName() + "] type [" + entity.getType() + "] COULD NOT FIND ENTRY for dn [" + dn + "]";
        int errCode;
        if (entity.getType() == OrgUnit.Type.PERM) {
            errCode = GlobalErrIds.ORG_NOT_FOUND_PERM;
        } else {
            errCode = GlobalErrIds.ORG_NOT_FOUND_USER;
        }
        throw new FinderException(errCode, warning);
    } catch (LdapException e) {
        String error = "findByKey orgUnitName [" + entity.getName() + "] type [" + entity.getType() + "] dn [" + dn + "] caught LdapException=" + e;
        int errCode;
        if (entity.getType() == OrgUnit.Type.PERM) {
            errCode = GlobalErrIds.ORG_READ_FAILED_PERM;
        } else {
            errCode = GlobalErrIds.ORG_READ_FAILED_USER;
        }
        throw new FinderException(errCode, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return oe;
}
Also used : OrgUnit(org.apache.directory.fortress.core.model.OrgUnit) 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) Dn(org.apache.directory.api.ldap.model.name.Dn) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 22 with FinderException

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

the class SdDAO method getSD.

/**
 * @param sdSet
 * @return
 * @throws FinderException
 */
SDSet getSD(SDSet sdSet) throws FinderException {
    SDSet entity = null;
    LdapConnection ld = null;
    String dn = getDn(sdSet.getName(), sdSet.getContextId());
    try {
        ld = getAdminConnection();
        Entry findEntry = read(ld, dn, SD_SET_ATRS);
        if (findEntry == null) {
            String warning = "getSD no entry found dn [" + dn + "]";
            throw new FinderException(GlobalErrIds.SSD_NOT_FOUND, warning);
        }
        entity = unloadLdapEntry(findEntry, 0);
    } catch (LdapNoSuchObjectException e) {
        String warning = "getSD Obj COULD NOT FIND ENTRY for dn [" + dn + "]";
        throw new FinderException(GlobalErrIds.SSD_NOT_FOUND, warning);
    } catch (LdapException e) {
        String error = "getSSD dn [" + dn + "] LEXCD=" + e;
        int errCode;
        if (sdSet.getType() == SDSet.SDType.DYNAMIC) {
            errCode = GlobalErrIds.DSD_READ_FAILED;
        } else {
            errCode = GlobalErrIds.SSD_READ_FAILED;
        }
        throw new FinderException(errCode, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return entity;
}
Also used : SDSet(org.apache.directory.fortress.core.model.SDSet) 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)

Example 23 with FinderException

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

the class SdDAO method search.

/**
 * Given an SSD name and type, find matching object in the directory.
 * @param sdset requires name and type.
 * @return List of matching SDSets.
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<SDSet> search(SDSet sdset) throws FinderException {
    List<SDSet> sdList = new ArrayList<>();
    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 {
        String searchVal = encodeSafeText(sdset.getName(), GlobalIds.ROLE_LEN);
        String filter = GlobalIds.FILTER_PREFIX + objectClass + ")(" + SD_SET_NM + "=" + searchVal + "*))";
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, ssdRoot, SearchScope.SUBTREE, filter, 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 sdset name [" + sdset.getName() + "] 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 sdset name [" + sdset.getName() + "] 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) 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 24 with FinderException

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

the class SdDAO method search.

/**
 * @param role
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<SDSet> search(Role role, SDSet.SDType type) throws FinderException {
    List<SDSet> sdList = new ArrayList<>();
    LdapConnection ld = null;
    String ssdRoot = getSdRoot(role.getContextId());
    String objectClass = SSD_OBJECT_CLASS_NM;
    if (type == SDSet.SDType.DYNAMIC) {
        objectClass = DSD_OBJECT_CLASS_NM;
    }
    try {
        String roleVal = encodeSafeText(role.getName(), GlobalIds.ROLE_LEN);
        StringBuilder filterbuf = new StringBuilder();
        filterbuf.append(GlobalIds.FILTER_PREFIX);
        filterbuf.append(objectClass);
        filterbuf.append(")(");
        // Include any parents target role may have:
        Set<String> roles = RoleUtil.getInstance().getAscendants(role.getName(), role.getContextId());
        if (CollectionUtils.isNotEmpty(roles)) {
            filterbuf.append("|(");
            filterbuf.append(ROLES);
            filterbuf.append("=");
            filterbuf.append(roleVal);
            filterbuf.append(")");
            for (String uRole : roles) {
                filterbuf.append("(");
                filterbuf.append(ROLES);
                filterbuf.append("=");
                filterbuf.append(uRole);
                filterbuf.append(")");
            }
            filterbuf.append(")");
        } else {
            filterbuf.append(ROLES);
            filterbuf.append("=");
            filterbuf.append(roleVal);
            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 role [" + role.getName() + "] type [" + type + "] caught LdapException=" + e.getMessage();
        int errCode;
        if (type == 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 role [" + role.getName() + "] type [" + type + "] caught CursorException=" + e.getMessage();
        int errCode;
        if (type == 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) 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 25 with FinderException

use of org.apache.directory.fortress.core.FinderException 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)

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