Skip to content
LdapUserDAO.java 45.5 KiB
Newer Older
Dustin Jenkins's avatar
Dustin Jenkins committed

            return (searchResults != null);
Jeff Burke's avatar
Jeff Burke committed
        }
        catch (LDAPException e)
        {
            LdapDAO.checkLdapResult(e.getResultCode());
Jeff Burke's avatar
Jeff Burke committed
        }
Jeff Burke's avatar
Jeff Burke committed
        return false;
Jeff Burke's avatar
Jeff Burke committed
    }
Jeff Burke's avatar
Jeff Burke committed
    /**
     * Returns a member user identified by the X500Principal only. The
     * returned object has the fields required by the LdapGroupDAO.
Dustin Jenkins's avatar
Dustin Jenkins committed
     * Note that this method binds as a proxy user and not as the
Jeff Burke's avatar
Jeff Burke committed
     * @param userDN
     * @return
     * @throws UserNotFoundException
     * @throws LDAPException
     */
    User<X500Principal> getX500User(DN userDN)
            throws UserNotFoundException, LDAPException, TransientException
Dustin Jenkins's avatar
Dustin Jenkins committed
        Filter filter =
                Filter.createEqualityFilter(LDAP_ENTRYDN,
Dustin Jenkins's avatar
Dustin Jenkins committed

        SearchRequest searchRequest =
                new SearchRequest(config.getUsersDN(), SearchScope.ONE,
Dustin Jenkins's avatar
Dustin Jenkins committed

        SearchResultEntry searchResult =
Brian Major's avatar
Brian Major committed
                getReadOnlyConnection().searchForEntry(searchRequest);

        if (searchResult == null)
        {
            logger.debug(msg);
            throw new UserNotFoundException(msg);
        }
Jeff Burke's avatar
Jeff Burke committed
        User<X500Principal> user = new User<X500Principal>(
                new X500Principal(searchResult.getAttributeValue(
Dustin Jenkins's avatar
Dustin Jenkins committed
                        userLdapAttrib.get(X500Principal.class))));
        String princ = searchResult.getAttributeValue(
Dustin Jenkins's avatar
Dustin Jenkins committed
                userLdapAttrib.get(HttpPrincipal.class));
        if (princ != null)
        {
            user.getIdentities().add(new HttpPrincipal(princ));
        }
        String fname = searchResult.getAttributeValue(LDAP_FIRST_NAME);
        String lname = searchResult.getAttributeValue(LDAP_LAST_NAME);
        user.details.add(new PersonalDetails(fname, lname));
        return user;
    }

    DN getUserDN(User<? extends Principal> user)
Dustin Jenkins's avatar
Dustin Jenkins committed
            throws UserNotFoundException, TransientException
        String searchField = userLdapAttrib.get(user.getUserID().getClass());
        if (searchField == null)
        {
Jeff Burke's avatar
Jeff Burke committed
            throw new IllegalArgumentException(
                    "Unsupported principal type " + user.getUserID().getClass());
        // change the DN to be in the 'java' format
        if (user.getUserID() instanceof X500Principal)
        {
            X500Principal orderedPrincipal = AuthenticationUtil.getOrderedForm(
                (X500Principal) user.getUserID());
            filter = Filter.createEqualityFilter(searchField, orderedPrincipal.toString());
            filter = Filter.createEqualityFilter(searchField, user.getUserID().getName());
        SearchResultEntry searchResult = null;
        try
        {
            SearchRequest searchRequest = new SearchRequest(
                config.getUsersDN(), SearchScope.ONE, filter, LDAP_ENTRYDN);
Brian Major's avatar
Brian Major committed
            searchResult = getReadOnlyConnection().searchForEntry(searchRequest);
Dustin Jenkins's avatar
Dustin Jenkins committed
        }
        catch (LDAPException e)
            LdapDAO.checkLdapResult(e.getResultCode());
        if (searchResult == null)
        {
            String msg = "User not found " + user.getUserID().getName();
            logger.debug(msg);
            throw new UserNotFoundException(msg);
        }
        return searchResult.getAttributeValueAsDN(LDAP_ENTRYDN);
    }
    protected DN getUserDN(final String userID, final String usersDN)
            throws LDAPException, TransientException
            return new DN(LDAP_UID + "=" + userID + "," + usersDN);
        }
        catch (LDAPException e)
        {
            logger.debug("getUserDN Exception: " + e, e);
            LdapDAO.checkLdapResult(e.getResultCode());
        }
        throw new IllegalArgumentException(userID + " not a valid user ID");
    }
    private void addAttribute(List<Attribute> attributes, final String name, final String value)
    {
        if (value != null && !value.isEmpty())
        {
            attributes.add(new Attribute(name, value));
        }
    private void addModification(List<Modification> mods, final String name, final String value)
    {
        if (value != null && !value.isEmpty())
        {
            mods.add(new Modification(ModificationType.REPLACE, name, value));
        }
        else
        {
            mods.add(new Modification(ModificationType.REPLACE, name));
        }
    }

    /**
     * Checks the Ldap result code, and if the result is not SUCCESS,
     * throws an appropriate exception. This is the place to decide on
     * mapping between ldap errors and exception types
     *
     * @param code The code returned from an LDAP request.
     * @throws TransientException
     * @throws UserAlreadyExistsException
     */
    protected static void checkUserLDAPResult(final ResultCode code)
            throws TransientException, UserAlreadyExistsException
    {
        if (code == ResultCode.ENTRY_ALREADY_EXISTS)
        {
            throw new UserAlreadyExistsException("User already exists.");
        }
        else
        {
            LdapDAO.checkLdapResult(code);
        }
    }
     * Method to return a randomly generated user numeric ID. The default
     * implementation returns a value between 10000 and Integer.MAX_VALUE.
     * Services that support a different mechanism for generating numeric
     * @return
     */
    protected int genNextNumericId()
    {
        Random rand = new Random();
        return rand.nextInt(Integer.MAX_VALUE - 10000) + 10000;
    }