Search in sources :

Example 1 with AccountNotFoundException

use of org.pac4j.core.exception.AccountNotFoundException in project cas by apereo.

the class BaseUseAttributesAuthorizationGenerator method generate.

@Override
public CommonProfile generate(final WebContext context, final CommonProfile profile) {
    Assert.notNull(this.connectionFactory, "connectionFactory must not be null");
    Assert.notNull(this.userSearchExecutor, "userSearchExecutor must not be null");
    final String username = profile.getId();
    final SearchResult userResult;
    try {
        LOGGER.debug("Attempting to get details for user [{}].", username);
        final Response<SearchResult> response = this.userSearchExecutor.search(this.connectionFactory, Beans.newLdaptiveSearchFilter(this.userSearchExecutor.getSearchFilter().getFilter(), Beans.LDAP_SEARCH_FILTER_DEFAULT_PARAM_NAME, Arrays.asList(username)));
        LOGGER.debug("LDAP user search response: [{}]", response);
        userResult = response.getResult();
        if (userResult.size() == 0) {
            throw new RuntimeException(new AccountNotFoundException(username + " not found."));
        }
        if (userResult.size() > 1 && !this.allowMultipleResults) {
            throw new IllegalStateException("Found multiple results for user which is not allowed (allowMultipleResults=false).");
        }
        final LdapEntry userEntry = userResult.getEntry();
        return generateAuthorizationForLdapEntry(profile, userEntry);
    } catch (final LdapException e) {
        throw new RuntimeException("LDAP error fetching details for user.", e);
    }
}
Also used : SearchResult(org.ldaptive.SearchResult) LdapEntry(org.ldaptive.LdapEntry) AccountNotFoundException(org.pac4j.core.exception.AccountNotFoundException) LdapException(org.ldaptive.LdapException)

Aggregations

LdapEntry (org.ldaptive.LdapEntry)1 LdapException (org.ldaptive.LdapException)1 SearchResult (org.ldaptive.SearchResult)1 AccountNotFoundException (org.pac4j.core.exception.AccountNotFoundException)1