Search in sources :

Example 91 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SpannerCustomMultiValuedTypesSample method main.

public static void main(String[] args) {
    // Prepare sample connection details
    SpannerEntryManagerSample sqlEntryManagerSample = new SpannerEntryManagerSample();
    // Create SQL entry manager
    SpannerEntryManager sqlEntryManager = sqlEntryManagerSample.createSpannerEntryManager();
    // Add dummy user
    SimpleUser newUser = new SimpleUser();
    newUser.setDn(String.format("inum=%s,ou=people,o=jans", System.currentTimeMillis()));
    newUser.setUserId("sample_user_" + System.currentTimeMillis());
    newUser.setUserPassword("test");
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansOptOuts", Arrays.asList("London", "Texas", "Kiev")));
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansExtUid", "test_value").multiValued());
    newUser.getCustomAttributes().add(new CustomObjectAttribute("jansPPID", "test_value").multiValued());
    newUser.setMemberOf(Arrays.asList("group_1", "group_2", "group_3"));
    newUser.setAttributeValue("givenName", "john");
    sqlEntryManager.persist(newUser);
    LOG.info("Added User '{}' with uid '{}' and key '{}'", newUser, newUser.getUserId(), newUser.getDn());
    LOG.info("Persisted custom attributes '{}'", newUser.getCustomAttributes());
    // Find added dummy user
    SimpleUser foundUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUser, foundUser.getUserId(), foundUser.getDn());
    LOG.info("Custom attributes '{}'", foundUser.getCustomAttributes());
    // Dump custom attributes
    for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
        System.out.println(attr.getName() + " - " + attr.getValues());
    }
    // Update custom attributes
    foundUser.setAttributeValues("jansOptOuts", Arrays.asList("London", "Texas", "Kiev", "Dublin"));
    foundUser.setAttributeValues("jansExtUid", Arrays.asList("test_value_11", "test_value_22", "test_value_33", "test_value_44"));
    foundUser.setAttributeValues("jansExtUid", Arrays.asList(11, 22, 33, 44));
    foundUser.setAttributeValues("jansPPID", Arrays.asList("fuzzy_value_1", "fuzzy_value_2"));
    foundUser.setAttributeValue("jansGuid", "simple");
    CustomObjectAttribute multiValuedSingleValue = new CustomObjectAttribute("jansAssociatedClnt", "multivalued_single_valued");
    multiValuedSingleValue.setMultiValued(true);
    foundUser.getCustomAttributes().add(multiValuedSingleValue);
    sqlEntryManager.merge(foundUser);
    LOG.info("Updated custom attributes '{}'", foundUser.getCustomAttributes());
    // Find updated dummy user
    SimpleUser foundUpdatedUser = sqlEntryManager.find(SimpleUser.class, newUser.getDn());
    LOG.info("Found User '{}' with uid '{}' and key '{}'", foundUpdatedUser, foundUpdatedUser.getUserId(), foundUpdatedUser.getDn());
    LOG.info("Cusom attributes '{}'", foundUpdatedUser.getCustomAttributes());
    // Dump custom attributes
    for (CustomObjectAttribute attr : foundUser.getCustomAttributes()) {
        System.out.println(attr.getName() + " - " + attr.getValues());
    }
    Filter filter = Filter.createEqualityFilter(Filter.createLowercaseFilter("givenName"), StringHelper.toLowerCase("john"));
    List<SimpleUser> foundUpdatedUsers = sqlEntryManager.findEntries("o=jans", SimpleUser.class, filter);
    System.out.println(foundUpdatedUsers);
}
Also used : CustomObjectAttribute(io.jans.orm.model.base.CustomObjectAttribute) SimpleUser(io.jans.orm.cloud.spanner.model.SimpleUser) Filter(io.jans.orm.search.filter.Filter) SpannerEntryManagerSample(io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample) SpannerEntryManager(io.jans.orm.cloud.spanner.impl.SpannerEntryManager)

Example 92 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class ScimFilterParserService method createFilter.

public Filter createFilter(String filter, Filter defaultFilter, Class<? extends BaseScimResource> clazz) throws SCIMException {
    try {
        Filter ldapFilter;
        if (StringUtils.isEmpty(filter))
            ldapFilter = defaultFilter;
        else {
            FilterListener filterListener = new FilterListener(clazz, ldapBackend);
            walkTree(FilterUtil.preprocess(filter, clazz), filterListener);
            ldapFilter = filterListener.getFilter();
            if (ldapFilter == null)
                throw new Exception("An error occurred when building LDAP filter: " + filterListener.getError());
        }
        return ldapFilter;
    } catch (Exception e) {
        throw new SCIMException(e.getMessage(), e);
    }
}
Also used : SCIMException(io.jans.scim.model.exception.SCIMException) Filter(io.jans.orm.search.filter.Filter) SCIMException(io.jans.scim.model.exception.SCIMException)

Example 93 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class FilterListener method enterAttrexp.

@Override
public void enterAttrexp(ScimFilterParser.AttrexpContext ctx) {
    if (StringUtils.isEmpty(error)) {
        log.trace("enterAttrexp.");
        String path = ctx.attrpath().getText();
        ScimFilterParser.CompvalueContext compValueCtx = ctx.compvalue();
        boolean isPrRule = compValueCtx == null && ctx.getChild(1).getText().equals("pr");
        Type attrType = null;
        Attribute attrAnnot = IntrospectUtil.getFieldAnnotation(path, resourceClass, Attribute.class);
        String ldapAttribute = null;
        boolean isNested = false;
        Boolean multiValued = false;
        if (attrAnnot == null) {
            ExtensionField field = extService.getFieldOfExtendedAttribute(resourceClass, path);
            if (field == null) {
                error = String.format("Attribute path '%s' is not recognized in %s", path, resourceClass.getSimpleName());
            } else {
                attrType = field.getAttributeDefinitionType();
                multiValued = field.isMultiValued();
                ldapAttribute = path.substring(path.lastIndexOf(":") + 1);
            }
        } else {
            attrType = attrAnnot.type();
            Pair<String, Boolean> pair = FilterUtil.getLdapAttributeOfResourceAttribute(path, resourceClass);
            ldapAttribute = pair.getFirst();
            isNested = pair.getSecond();
            multiValued = computeMultivaluedForCoreAttribute(path, attrAnnot, ldapAttribute);
        }
        if (error != null) {
        // Intentionally left empty
        } else if (attrType == null) {
            error = String.format("Could not determine type of attribute path '%s' in %s", path, resourceClass.getSimpleName());
        } else if (ldapAttribute == null) {
            error = String.format("Could not determine LDAP attribute for path '%s' in %s", path, resourceClass.getSimpleName());
        } else {
            String subattr = isNested ? path.substring(path.lastIndexOf(".") + 1) : null;
            CompValueType type;
            ScimOperator operator;
            if (isPrRule) {
                type = CompValueType.NULL;
                operator = ScimOperator.NOT_EQUAL;
            } else {
                type = FilterUtil.getCompValueType(compValueCtx);
                operator = ScimOperator.getByValue(ctx.compareop().getText());
            }
            error = FilterUtil.checkFilterConsistency(path, attrType, type, operator);
            if (error == null) {
                Pair<Filter, String> subf = subFilterGenerator.build(subattr, ldapAttribute, isPrRule ? null : compValueCtx.getText(), attrType, type, operator, multiValued);
                Filter subFilth = subf.getFirst();
                error = subf.getSecond();
                if (subFilth == null) {
                    if (error == null) {
                        error = String.format("Operator '%s' is not supported for attribute %s", operator.getValue(), path);
                    }
                } else {
                    filter.push(subFilth);
                }
            }
        }
    }
}
Also used : Attribute(io.jans.scim.model.scim2.annotations.Attribute) ScimOperator(io.jans.scim.service.antlr.scimFilter.enums.ScimOperator) Type(io.jans.scim.model.scim2.AttributeDefinition.Type) NullType(javax.lang.model.type.NullType) CompValueType(io.jans.scim.service.antlr.scimFilter.enums.CompValueType) ExtensionField(io.jans.scim.model.scim2.extensions.ExtensionField) Filter(io.jans.orm.search.filter.Filter) ScimFilterParser(io.jans.scim.service.antlr.scimFilter.antlr4.ScimFilterParser) CompValueType(io.jans.scim.service.antlr.scimFilter.enums.CompValueType)

Example 94 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SubFilterGenerator method getSubFilterDateTime.

private Filter getSubFilterDateTime(String subAttribute, String attribute, String value, ScimOperator operator, Boolean multivalued) {
    Filter subfilter = null;
    log.trace("getSubFilterDateTime");
    String stringDate = ldapBackend ? DateUtil.ISOToGeneralizedStringDate(value) : DateUtil.gluuCouchbaseISODate(value);
    if (stringDate == null) {
        error = String.format("Value passed for date comparison '%s' is not in ISO format", value);
        return null;
    }
    switch(operator) {
        case EQUAL:
        case NOT_EQUAL:
            if (subAttribute == null) {
                // attribute=stringDate
                // attribute="stringDate"
                subfilter = Filter.createEqualityFilter(attribute, stringDate).multiValued(multivalued);
            } else {
                // attribute=*"subAttribute":stringDate*
                // attribute LIKE "%\"subAttribute\":\"stringDate\"%"
                String sub = String.format("\"%s\":%s", subAttribute, stringDate);
                subfilter = Filter.createSubstringFilter(attribute, null, new String[] { sub }, null).multiValued(multivalued);
            }
            subfilter = negateIf(subfilter, operator.equals(ScimOperator.NOT_EQUAL));
            break;
        case GREATER_THAN:
            if (subAttribute == null) {
                // &(!(attribute=value))(attribute>=value)  --> LDAP does not support greater than operator
                // attribute > "value"
                subfilter = Filter.createANDFilter(Filter.createNOTFilter(Filter.createEqualityFilter(attribute, stringDate).multiValued(multivalued)), Filter.createGreaterOrEqualFilter(attribute, stringDate).multiValued(multivalued));
            }
            break;
        case GREATER_THAN_OR_EQUAL:
            if (subAttribute == null) {
                // attribute>=value
                // attribute >= "value"
                subfilter = Filter.createGreaterOrEqualFilter(attribute, stringDate).multiValued(multivalued);
            }
            break;
        case LESS_THAN:
            if (subAttribute == null) {
                // &(!(attribute=value))(attribute<=value)  --> LDAP does not support less than operator
                // attribute < "value"
                subfilter = Filter.createANDFilter(Filter.createNOTFilter(Filter.createEqualityFilter(attribute, stringDate).multiValued(multivalued)), Filter.createLessOrEqualFilter(attribute, stringDate).multiValued(multivalued));
            }
            break;
        case LESS_THAN_OR_EQUAL:
            if (subAttribute == null) {
                // attribute<=value
                // attribute <= "value"
                subfilter = Filter.createLessOrEqualFilter(attribute, stringDate).multiValued(multivalued);
            }
            break;
        default:
            error = FilterUtil.getOperatorInconsistencyError(operator.getValue(), Type.DATETIME.toString(), attribute);
    }
    return subfilter;
}
Also used : Filter(io.jans.orm.search.filter.Filter)

Example 95 with Filter

use of io.jans.orm.search.filter.Filter in project jans by JanssenProject.

the class SubFilterGenerator method getSubFilterBoolean.

private Filter getSubFilterBoolean(String subAttribute, String attribute, String value, ScimOperator operator, Boolean multivalued) {
    Filter subfilter = null;
    log.trace("getSubFilterBoolean");
    if (operator.equals(ScimOperator.EQUAL) || operator.equals(ScimOperator.NOT_EQUAL)) {
        if (subAttribute == null) {
            // attribute=value
            subfilter = Filter.createEqualityFilter(attribute, Boolean.valueOf(value)).multiValued(multivalued);
        } else {
            // attribute=*"subAttribute":value*
            // attribute LIKE "%\"subAttribute\":value%"
            String sub = String.format("\"%s\":%s", subAttribute, value);
            subfilter = Filter.createSubstringFilter(attribute, null, new String[] { sub }, null).multiValued(multivalued);
        }
        subfilter = negateIf(subfilter, operator.equals(ScimOperator.NOT_EQUAL));
    } else {
        error = FilterUtil.getOperatorInconsistencyError(operator.getValue(), Type.BOOLEAN.toString(), attribute);
    }
    return subfilter;
}
Also used : Filter(io.jans.orm.search.filter.Filter)

Aggregations

Filter (io.jans.orm.search.filter.Filter)188 Test (org.testng.annotations.Test)50 EntryPersistenceException (io.jans.orm.exception.EntryPersistenceException)32 ConvertedExpression (io.jans.orm.couchbase.model.ConvertedExpression)28 SearchException (io.jans.orm.exception.operation.SearchException)25 ConvertedExpression (io.jans.orm.sql.model.ConvertedExpression)24 MappingException (io.jans.orm.exception.MappingException)22 ArrayList (java.util.ArrayList)21 AuthenticationException (io.jans.orm.exception.AuthenticationException)20 PropertyAnnotation (io.jans.orm.reflect.property.PropertyAnnotation)19 EntryDeleteException (io.jans.orm.exception.EntryDeleteException)18 Date (java.util.Date)14 List (java.util.List)14 SqlEntryManager (io.jans.orm.sql.impl.SqlEntryManager)11 SqlEntryManagerSample (io.jans.orm.sql.persistence.SqlEntryManagerSample)11 CustomAttribute (io.jans.orm.model.base.CustomAttribute)9 CustomObjectAttribute (io.jans.orm.model.base.CustomObjectAttribute)9 DateTimeParseException (java.time.format.DateTimeParseException)9 SpannerEntryManager (io.jans.orm.cloud.spanner.impl.SpannerEntryManager)8 SpannerEntryManagerSample (io.jans.orm.cloud.spanner.persistence.SpannerEntryManagerSample)8