Search in sources :

Example 1 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class FilterExpressionCheckEvaluationVisitor method visitOrExpression.

@Override
public Boolean visitOrExpression(OrFilterExpression expression) {
    FilterExpression left = expression.getLeft();
    FilterExpression right = expression.getRight();
    return left.accept(this) || right.accept(this);
}
Also used : NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Example 2 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class FilterExpressionNormalizationVisitor method visitOrExpression.

@Override
public FilterExpression visitOrExpression(OrFilterExpression expression) {
    FilterExpression left = expression.getLeft();
    FilterExpression right = expression.getRight();
    return new OrFilterExpression(left.accept(this), right.accept(this));
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Example 3 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class FilterExpressionNormalizationVisitor method visitNotExpression.

@Override
public FilterExpression visitNotExpression(NotFilterExpression fe) {
    FilterExpression inner = fe.getNegated();
    if (inner instanceof AndFilterExpression) {
        AndFilterExpression and = (AndFilterExpression) inner;
        FilterExpression left = new NotFilterExpression(and.getLeft()).accept(this);
        FilterExpression right = new NotFilterExpression(and.getRight()).accept(this);
        return new OrFilterExpression(left, right);
    }
    if (inner instanceof OrFilterExpression) {
        OrFilterExpression or = (OrFilterExpression) inner;
        FilterExpression left = new NotFilterExpression(or.getLeft()).accept(this);
        FilterExpression right = new NotFilterExpression(or.getRight()).accept(this);
        return new AndFilterExpression(left, right);
    }
    if (inner instanceof NotFilterExpression) {
        NotFilterExpression not = (NotFilterExpression) inner;
        return (not.getNegated()).accept(this);
    }
    if (inner instanceof FilterPredicate) {
        FilterPredicate filter = (FilterPredicate) inner;
        return filter.negate();
    }
    return inner;
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Example 4 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class RSQLFilterDialect method parseTypedExpression.

@Override
public Map<String, FilterExpression> parseTypedExpression(String path, MultivaluedMap<String, String> filterParams, String apiVersion) throws ParseException {
    Map<String, FilterExpression> expressionByType = new HashMap<>();
    for (MultivaluedMap.Entry<String, List<String>> entry : filterParams.entrySet()) {
        String paramName = entry.getKey();
        List<String> paramValues = entry.getValue();
        Matcher matcher = TYPED_FILTER_PATTERN.matcher(paramName);
        if (matcher.find()) {
            String typeName = matcher.group(1);
            if (paramValues.size() != 1) {
                throw new ParseException("Exactly one RSQL expression must be defined for type : " + typeName);
            }
            Type entityType = dictionary.getEntityClass(typeName, apiVersion);
            if (entityType == null) {
                throw new ParseException(INVALID_QUERY_PARAMETER + paramName);
            }
            String expressionText = paramValues.get(0);
            FilterExpression filterExpression = parseFilterExpression(expressionText, entityType, true);
            expressionByType.put(typeName, filterExpression);
        } else {
            throw new ParseException(INVALID_QUERY_PARAMETER + paramName);
        }
    }
    return expressionByType;
}
Also used : ArgumentType(com.yahoo.elide.core.dictionary.ArgumentType) TypeHelper.isPrimitiveNumberType(com.yahoo.elide.core.utils.TypeHelper.isPrimitiveNumberType) Type(com.yahoo.elide.core.type.Type) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) List(java.util.List) ArrayList(java.util.ArrayList) Argument.getArgumentsFromString(com.yahoo.elide.core.request.Argument.getArgumentsFromString) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 5 with FilterExpression

use of com.yahoo.elide.core.filter.expression.FilterExpression in project elide by yahoo.

the class PermissionExpressionBuilder method buildAnyFieldFilterExpression.

/**
 * Build an expression representing any field on an entity.
 *
 * @param forType   Resource class
 * @param requestScope requestScope
 * @return Expressions
 */
public FilterExpression buildAnyFieldFilterExpression(Type<?> forType, RequestScope requestScope, Set<String> requestedFields) {
    Class<? extends Annotation> annotationClass = ReadPermission.class;
    ParseTree classPermissions = entityDictionary.getPermissionsForClass(forType, annotationClass);
    FilterExpression entityFilter = filterExpressionFromParseTree(classPermissions, forType, requestScope);
    // case where the permissions does not have ANY filterExpressionCheck
    if (entityFilter == FALSE_USER_CHECK_EXPRESSION || entityFilter == NO_EVALUATION_EXPRESSION || entityFilter == TRUE_USER_CHECK_EXPRESSION) {
        entityFilter = null;
    }
    FilterExpression allFieldsFilterExpression = entityFilter;
    List<String> fields = entityDictionary.getAllExposedFields(forType).stream().filter(field -> requestedFields == null || requestedFields.contains(field)).collect(Collectors.toList());
    for (String field : fields) {
        ParseTree fieldPermissions = entityDictionary.getPermissionsForField(forType, field, annotationClass);
        FilterExpression fieldExpression = filterExpressionFromParseTree(fieldPermissions, forType, requestScope);
        if (fieldExpression == null && entityFilter == null) {
            // this field will be visible across all instances
            return null;
        }
        if (fieldExpression == null || fieldExpression == FALSE_USER_CHECK_EXPRESSION) {
            // In either case this field is not useful for filtering when loading records
            continue;
        }
        if (fieldExpression == NO_EVALUATION_EXPRESSION || fieldExpression == TRUE_USER_CHECK_EXPRESSION) {
            // When the expression is TRUE_USER_CHECK_EXPRESSION all records can be loaded
            return null;
        }
        if (allFieldsFilterExpression != null) {
            allFieldsFilterExpression = new OrFilterExpression(allFieldsFilterExpression, fieldExpression);
        } else {
            allFieldsFilterExpression = fieldExpression;
        }
    }
    return allFieldsFilterExpression;
}
Also used : CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) PermissionExpressionNormalizationVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionNormalizationVisitor) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) Function(java.util.function.Function) FAILURE(com.yahoo.elide.core.security.permissions.expressions.Expression.Results.FAILURE) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) SpecificFieldExpression(com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression) PersistentResource(com.yahoo.elide.core.PersistentResource) PermissionExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionExpressionVisitor) ParseTree(org.antlr.v4.runtime.tree.ParseTree) NO_EVALUATION_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.NO_EVALUATION_EXPRESSION) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) RequestScope(com.yahoo.elide.core.RequestScope) ChangeSpec(com.yahoo.elide.core.security.ChangeSpec) Check(com.yahoo.elide.core.security.checks.Check) PermissionToFilterExpressionVisitor(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) Set(java.util.Set) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) List(java.util.List) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Type(com.yahoo.elide.core.type.Type) Annotation(java.lang.annotation.Annotation) FALSE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.FALSE_USER_CHECK_EXPRESSION) TRUE_USER_CHECK_EXPRESSION(com.yahoo.elide.core.security.visitors.PermissionToFilterExpressionVisitor.TRUE_USER_CHECK_EXPRESSION) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) ReadPermission(com.yahoo.elide.annotation.ReadPermission) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)214 Test (org.junit.jupiter.api.Test)161 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)91 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)72 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)49 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)46 Path (com.yahoo.elide.core.Path)44 Query (com.yahoo.elide.datastores.aggregation.query.Query)42 Argument (com.yahoo.elide.core.request.Argument)39 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)39 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)34 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)34 HashMap (java.util.HashMap)29 Book (example.Book)28 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)24 EntityProjection (com.yahoo.elide.core.request.EntityProjection)22 Date (java.util.Date)21 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)20 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)19 HashSet (java.util.HashSet)18