Search in sources :

Example 1 with OrFilterExpression

use of com.yahoo.elide.core.filter.expression.OrFilterExpression 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 OrFilterExpression

use of com.yahoo.elide.core.filter.expression.OrFilterExpression 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 OrFilterExpression

use of com.yahoo.elide.core.filter.expression.OrFilterExpression 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 OrFilterExpression

use of com.yahoo.elide.core.filter.expression.OrFilterExpression 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)

Example 5 with OrFilterExpression

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

the class MatchesTemplateVisitor method matches.

private boolean matches(FilterExpression a, FilterExpression b) {
    if (!a.getClass().equals(b.getClass())) {
        return false;
    }
    if (a instanceof AndFilterExpression) {
        AndFilterExpression andA = (AndFilterExpression) a;
        AndFilterExpression andB = (AndFilterExpression) b;
        return matches(andA.getLeft(), andB.getLeft()) && matches(andA.getRight(), andB.getRight());
    }
    if (a instanceof OrFilterExpression) {
        OrFilterExpression orA = (OrFilterExpression) a;
        OrFilterExpression orB = (OrFilterExpression) b;
        return matches(orA.getLeft(), orB.getLeft()) && matches(orA.getRight(), orB.getRight());
    }
    if (a instanceof NotFilterExpression) {
        NotFilterExpression notA = (NotFilterExpression) a;
        NotFilterExpression notB = (NotFilterExpression) b;
        return matches(notA.getNegated(), notB.getNegated());
    }
    FilterPredicate predicateA = (FilterPredicate) a;
    FilterPredicate predicateB = (FilterPredicate) b;
    boolean valueMatches = predicateA.getValues().equals(predicateB.getValues());
    boolean operatorMatches = predicateA.getOperator().equals(predicateB.getOperator());
    boolean pathMatches = pathMatches(predicateA.getPath(), predicateB.getPath());
    boolean usingTemplate = false;
    if (predicateA.getValues().size() == 1) {
        String value = predicateA.getValues().get(0).toString();
        Matcher matcher = TEMPLATE_PATTERN.matcher(value);
        usingTemplate = matcher.matches();
        if (usingTemplate && pathMatches & operatorMatches) {
            String argumentName = matcher.group(1);
            arguments.put(argumentName, Argument.builder().name(argumentName).value(predicateB.getValues().size() == 1 ? predicateB.getValues().get(0) : predicateB.getValues()).build());
        }
    }
    return (operatorMatches && pathMatches && (valueMatches || usingTemplate));
}
Also used : OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Matcher(java.util.regex.Matcher) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) NotFilterExpression(com.yahoo.elide.core.filter.expression.NotFilterExpression) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression)

Aggregations

OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)28 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)23 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)20 Test (org.junit.jupiter.api.Test)19 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)16 Path (com.yahoo.elide.core.Path)13 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)8 Query (com.yahoo.elide.datastores.aggregation.query.Query)8 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)8 Author (example.Author)7 Book (example.Book)6 InPredicate (com.yahoo.elide.core.filter.predicates.InPredicate)5 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)5 Date (java.util.Date)5 PersistentResource (com.yahoo.elide.core.PersistentResource)3 Argument (com.yahoo.elide.core.request.Argument)3 EntityProjection (com.yahoo.elide.core.request.EntityProjection)3 HashSet (java.util.HashSet)3 PathElement (com.yahoo.elide.core.Path.PathElement)2