Search in sources :

Example 51 with FinderException

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

Example 52 with FinderException

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

the class UserDAO method getAuthorizedUsers.

/**
 * @param role
 * @param limit
 * @return
 * @throws FinderException
 */
List<String> getAuthorizedUsers(Role role, int limit) 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, false, limit);
        while (searchResults.next()) {
            Entry entry = searchResults.getEntry();
            userList.add(getAttribute(entry, SchemaConstants.UID_AT));
        }
    } catch (LdapException e) {
        String warning = "getAuthorizedUsers role name [" + role.getName() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.URLE_SEARCH_FAILED, warning, e);
    } catch (CursorException e) {
        String warning = "getAuthorizedUsers 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) 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 53 with FinderException

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

the class UserDAO method getUserRoles.

List<UserRole> getUserRoles(Role role, RCType rcType, String paSetName) throws FinderException {
    List<UserRole> userRoleList = new ArrayList<>();
    LdapConnection ld = null;
    String userRoot = getRootDn(role.getContextId(), GlobalIds.USER_ROOT);
    try {
        String roleVal = encodeSafeText(role.getName(), GlobalIds.ROLE_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(")");
        filterbuf.append("(");
        filterbuf.append(GlobalIds.USER_ROLE_DATA);
        filterbuf.append("=");
        filterbuf.append(this.getFilterForRoleConstraint(role.getName(), rcType, paSetName));
        filterbuf.append(")");
        filterbuf.append(")");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, userRoot, SearchScope.ONELEVEL, filterbuf.toString(), defaultAtrs, false, GlobalIds.BATCH_SIZE);
        while (searchResults.next()) {
            userRoleList.addAll(this.unloadUserRoles(searchResults.getEntry(), getAttribute(searchResults.getEntry(), SchemaConstants.UID_AT), role.getContextId(), role.getName()));
        }
    } 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 userRoleList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) 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)

Example 54 with FinderException

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

the class UserDAO method checkPassword.

/**
 * @param user
 * @return
 * @throws org.apache.directory.fortress.core.FinderException,  org.apache.directory.fortress.core.PasswordException
 */
Session checkPassword(User user) throws FinderException, PasswordException {
    Session session = null;
    LdapConnection ld = null;
    String userDn = getDn(user.getUserId(), user.getContextId());
    try {
        session = new ObjectFactory().createSession();
        session.setAuthenticated(false);
        session.setUserId(user.getUserId());
        ld = getUserConnection();
        BindResponse bindResponse = bind(ld, userDn, user.getPassword());
        String info;
        if (bindResponse.getLdapResult().getResultCode() != ResultCodeEnum.SUCCESS) {
            info = "PASSWORD INVALID for userId [" + user.getUserId() + "], resultCode [" + bindResponse.getLdapResult().getResultCode() + "]";
            session.setMsg(info);
            session.setErrorId(GlobalErrIds.USER_PW_INVLD);
        }
        PasswordPolicy respCtrl = getPwdRespCtrl(bindResponse);
        if (respCtrl != null) {
            // check IETF password policies here
            checkPwPolicies(session, respCtrl);
        }
        if (session.getErrorId() == 0) {
            session.setAuthenticated(true);
        } else {
            // pw invalid or pw policy violation:
            throw new PasswordException(session.getErrorId(), session.getMsg());
        }
    } catch (LdapAuthenticationException e) {
        String info = "checkPassword INVALID PASSWORD for userId [" + user.getUserId() + "] exception [" + e + "]";
        throw new PasswordException(GlobalErrIds.USER_PW_INVLD, info);
    } catch (LdapException e) {
        String error = "checkPassword userId [" + user.getUserId() + "] caught LDAPException=" + e.getMessage();
        throw new FinderException(GlobalErrIds.USER_READ_FAILED, error, e);
    } finally {
        closeUserConnection(ld);
    }
    return session;
}
Also used : PasswordException(org.apache.directory.fortress.core.PasswordException) FinderException(org.apache.directory.fortress.core.FinderException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) ObjectFactory(org.apache.directory.fortress.core.model.ObjectFactory) PasswordPolicy(org.apache.directory.api.ldap.extras.controls.ppolicy.PasswordPolicy) BindResponse(org.apache.directory.api.ldap.model.message.BindResponse) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) Session(org.apache.directory.fortress.core.model.Session) LdapConnection(org.apache.directory.ldap.client.api.LdapConnection)

Example 55 with FinderException

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

the class PermDAO method findPermissions.

/**
 * @param user
 * @return
 * @throws org.apache.directory.fortress.core.FinderException
 */
List<Permission> findPermissions(User user) throws FinderException {
    List<Permission> permList = new ArrayList<>();
    LdapConnection ld = null;
    String permRoot = getRootDn(user.getContextId(), GlobalIds.PERM_ROOT);
    try {
        StringBuilder filterbuf = new StringBuilder();
        filterbuf.append(GlobalIds.FILTER_PREFIX);
        filterbuf.append(PERM_OP_OBJECT_CLASS_NAME);
        filterbuf.append(")(|");
        Set<String> roles = RoleUtil.getInstance().getInheritedRoles(user.getRoles(), user.getContextId());
        if (CollectionUtils.isNotEmpty(roles)) {
            for (String uRole : roles) {
                filterbuf.append("(");
                filterbuf.append(ROLES);
                filterbuf.append("=");
                filterbuf.append(encodeSafeText(uRole, GlobalIds.ROLE_LEN));
                filterbuf.append(")");
            }
        }
        filterbuf.append("(");
        filterbuf.append(USERS);
        filterbuf.append("=");
        filterbuf.append(user.getUserId());
        filterbuf.append(")))");
        ld = getAdminConnection();
        SearchCursor searchResults = search(ld, permRoot, SearchScope.SUBTREE, filterbuf.toString(), PERMISSION_OP_ATRS, false, GlobalIds.BATCH_SIZE);
        long sequence = 0;
        while (searchResults.next()) {
            permList.add(unloadPopLdapEntry(searchResults.getEntry(), sequence++, false));
        }
    } catch (LdapException e) {
        String error = "findPermissions user [" + user.getUserId() + "] caught LdapException in PermDAO.findPermissions=" + e.getMessage();
        throw new FinderException(GlobalErrIds.PERM_USER_SEARCH_FAILED, error, e);
    } catch (CursorException e) {
        String error = "findPermissions user [" + user.getUserId() + "] caught CursorException in PermDAO.findPermissions=" + e.getMessage();
        throw new FinderException(GlobalErrIds.PERM_USER_SEARCH_FAILED, error, e);
    } finally {
        closeAdminConnection(ld);
    }
    return permList;
}
Also used : FinderException(org.apache.directory.fortress.core.FinderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) Permission(org.apache.directory.fortress.core.model.Permission) 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