Search in sources :

Example 1 with EqualCondition

use of org.keycloak.storage.ldap.idm.query.internal.EqualCondition in project keycloak by keycloak.

the class LDAPIdentityStore method fetchQueryResults.

@Override
public List<LDAPObject> fetchQueryResults(LDAPQuery identityQuery) {
    if (identityQuery.getSorting() != null && !identityQuery.getSorting().isEmpty()) {
        throw new ModelException("LDAP Identity Store does not yet support sorted queries.");
    }
    List<LDAPObject> results = new ArrayList<>();
    try {
        String baseDN = identityQuery.getSearchDn();
        for (Condition condition : identityQuery.getConditions()) {
            // Check if we are searching by ID
            String uuidAttrName = getConfig().getUuidLDAPAttributeName();
            if (condition instanceof EqualCondition) {
                EqualCondition equalCondition = (EqualCondition) condition;
                if (equalCondition.getParameterName().equalsIgnoreCase(uuidAttrName)) {
                    SearchResult search = this.operationManager.lookupById(baseDN, equalCondition.getValue().toString(), identityQuery.getReturningLdapAttributes());
                    if (search != null) {
                        results.add(populateAttributedType(search, identityQuery));
                    }
                    return results;
                }
            }
        }
        StringBuilder filter = createIdentityTypeSearchFilter(identityQuery);
        List<SearchResult> search;
        if (getConfig().isPagination() && identityQuery.getLimit() > 0) {
            search = this.operationManager.searchPaginated(baseDN, filter.toString(), identityQuery);
        } else {
            search = this.operationManager.search(baseDN, filter.toString(), identityQuery.getReturningLdapAttributes(), identityQuery.getSearchScope());
        }
        for (SearchResult result : search) {
            // don't add the branch in subtree search
            if (identityQuery.getSearchScope() != SearchControls.SUBTREE_SCOPE || !result.getNameInNamespace().equalsIgnoreCase(baseDN)) {
                results.add(populateAttributedType(result, identityQuery));
            }
        }
    } catch (Exception e) {
        throw new ModelException("Querying of LDAP failed " + identityQuery, e);
    }
    return results;
}
Also used : EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition) Condition(org.keycloak.storage.ldap.idm.query.Condition) ModelException(org.keycloak.models.ModelException) ArrayList(java.util.ArrayList) LDAPObject(org.keycloak.storage.ldap.idm.model.LDAPObject) SearchResult(javax.naming.directory.SearchResult) EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition) NamingException(javax.naming.NamingException) AuthenticationException(javax.naming.AuthenticationException) AttributeInUseException(javax.naming.directory.AttributeInUseException) NoSuchAttributeException(javax.naming.directory.NoSuchAttributeException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) ModelException(org.keycloak.models.ModelException) SchemaViolationException(javax.naming.directory.SchemaViolationException)

Example 2 with EqualCondition

use of org.keycloak.storage.ldap.idm.query.internal.EqualCondition in project keycloak by keycloak.

the class CertificateLDAPStorageMapper method beforeLDAPQuery.

@Override
public void beforeLDAPQuery(LDAPQuery query) {
    super.beforeLDAPQuery(query);
    String ldapAttrName = getLdapAttributeName();
    if (isDerFormatted()) {
        for (Condition condition : query.getConditions()) {
            if (condition instanceof EqualCondition && condition.getParameterName().equalsIgnoreCase(ldapAttrName)) {
                EqualCondition equalCondition = ((EqualCondition) condition);
                equalCondition.setValue(PemUtils.pemToDer(equalCondition.getValue().toString()));
            }
        }
    }
}
Also used : EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition) Condition(org.keycloak.storage.ldap.idm.query.Condition) EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition)

Example 3 with EqualCondition

use of org.keycloak.storage.ldap.idm.query.internal.EqualCondition in project keycloak by keycloak.

the class FullNameLDAPStorageMapper method beforeLDAPQuery.

@Override
public void beforeLDAPQuery(LDAPQuery query) {
    if (isWriteOnly()) {
        return;
    }
    String ldapFullNameAttrName = getLdapFullNameAttrName();
    query.addReturningLdapAttribute(ldapFullNameAttrName);
    // Change conditions and compute condition for fullName from the conditions for firstName and lastName. Right now just "equal" condition is supported
    EqualCondition firstNameCondition = null;
    EqualCondition lastNameCondition = null;
    Set<Condition> conditionsCopy = new HashSet<Condition>(query.getConditions());
    for (Condition condition : conditionsCopy) {
        String paramName = condition.getParameterName();
        if (paramName != null) {
            if (paramName.equals(UserModel.FIRST_NAME)) {
                firstNameCondition = (EqualCondition) condition;
                query.getConditions().remove(condition);
            } else if (paramName.equals(UserModel.LAST_NAME)) {
                lastNameCondition = (EqualCondition) condition;
                query.getConditions().remove(condition);
            } else if (paramName.equals(LDAPConstants.GIVENNAME)) {
                // Some previous mapper already converted it to LDAP name
                firstNameCondition = (EqualCondition) condition;
            } else if (paramName.equals(LDAPConstants.SN)) {
                // Some previous mapper already converted it to LDAP name
                lastNameCondition = (EqualCondition) condition;
            }
        }
    }
    String fullName = null;
    if (firstNameCondition != null && lastNameCondition != null) {
        fullName = firstNameCondition.getValue() + " " + lastNameCondition.getValue();
    } else if (firstNameCondition != null) {
        fullName = (String) firstNameCondition.getValue();
    } else if (lastNameCondition != null) {
        fullName = (String) lastNameCondition.getValue();
    } else {
        return;
    }
    EscapeStrategy escapeStrategy = firstNameCondition != null ? firstNameCondition.getEscapeStrategy() : lastNameCondition.getEscapeStrategy();
    EqualCondition fullNameCondition = new EqualCondition(ldapFullNameAttrName, fullName, escapeStrategy);
    query.addWhereCondition(fullNameCondition);
}
Also used : EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition) Condition(org.keycloak.storage.ldap.idm.query.Condition) EqualCondition(org.keycloak.storage.ldap.idm.query.internal.EqualCondition) EscapeStrategy(org.keycloak.storage.ldap.idm.query.EscapeStrategy) HashSet(java.util.HashSet)

Aggregations

Condition (org.keycloak.storage.ldap.idm.query.Condition)3 EqualCondition (org.keycloak.storage.ldap.idm.query.internal.EqualCondition)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NoSuchElementException (java.util.NoSuchElementException)1 AuthenticationException (javax.naming.AuthenticationException)1 NamingException (javax.naming.NamingException)1 AttributeInUseException (javax.naming.directory.AttributeInUseException)1 NoSuchAttributeException (javax.naming.directory.NoSuchAttributeException)1 SchemaViolationException (javax.naming.directory.SchemaViolationException)1 SearchResult (javax.naming.directory.SearchResult)1 ModelException (org.keycloak.models.ModelException)1 LDAPObject (org.keycloak.storage.ldap.idm.model.LDAPObject)1 EscapeStrategy (org.keycloak.storage.ldap.idm.query.EscapeStrategy)1