Search in sources :

Example 1 with OrExpression

use of com.yahoo.elide.core.security.permissions.expressions.OrExpression in project elide by yahoo.

the class PermissionExpressionBuilder method buildAnyFieldExpression.

/**
 * Build an expression representing any field on an entity.
 *
 * @param condition       The condition which triggered this permission expression check
 * @param checkFn         check function
 * @param scope           RequestScope
 * @param requestedFields The list of requested fields
 * @return Expressions
 */
private Expression buildAnyFieldExpression(final PermissionCondition condition, final Function<Check, Expression> checkFn, final Set<String> requestedFields, final RequestScope scope) {
    Type<?> resourceClass = condition.getEntityClass();
    Class<? extends Annotation> annotationClass = condition.getPermission();
    ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass);
    Expression entityExpression = normalizedExpressionFromParseTree(classPermissions, checkFn);
    OrExpression allFieldsExpression = new OrExpression(FAILURE, null);
    List<String> fields = entityDictionary.getAllExposedFields(resourceClass);
    boolean entityExpressionUsed = false;
    boolean fieldExpressionUsed = false;
    for (String field : fields) {
        if (requestedFields != null && !requestedFields.contains(field)) {
            continue;
        }
        ParseTree fieldPermissions = entityDictionary.getPermissionsForField(resourceClass, field, annotationClass);
        Expression fieldExpression = normalizedExpressionFromParseTree(fieldPermissions, checkFn);
        if (fieldExpression == null) {
            if (entityExpressionUsed) {
                continue;
            }
            if (entityExpression == null) {
                // One field had no permissions set - so we allow the action.
                return SUCCESSFUL_EXPRESSION;
            }
            fieldExpression = entityExpression;
            entityExpressionUsed = true;
        } else {
            fieldExpressionUsed = true;
        }
        allFieldsExpression = new OrExpression(allFieldsExpression, fieldExpression);
    }
    if (!fieldExpressionUsed) {
        // If there are no permissions, allow access...
        if (entityExpression == null) {
            return SUCCESSFUL_EXPRESSION;
        }
        return new AnyFieldExpression(condition, entityExpression);
    }
    return new AnyFieldExpression(condition, allFieldsExpression);
}
Also used : AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) SpecificFieldExpression(com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with OrExpression

use of com.yahoo.elide.core.security.permissions.expressions.OrExpression in project elide by yahoo.

the class PermissionExpressionNormalizationVisitor method visitNotExpression.

@Override
public Expression visitNotExpression(NotExpression notExpression) {
    Expression inner = notExpression.getLogical();
    if (inner instanceof AndExpression) {
        AndExpression and = (AndExpression) inner;
        Expression left = new NotExpression(and.getLeft()).accept(this);
        Expression right = new NotExpression(and.getRight()).accept(this);
        return new OrExpression(left, right);
    }
    if (inner instanceof OrExpression) {
        OrExpression or = (OrExpression) inner;
        Expression left = new NotExpression(or.getLeft()).accept(this);
        Expression right = new NotExpression(or.getRight()).accept(this);
        return new AndExpression(left, right);
    }
    if (inner instanceof NotExpression) {
        NotExpression not = (NotExpression) inner;
        return (not.getLogical()).accept(this);
    }
    return notExpression;
}
Also used : AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) NotExpression(com.yahoo.elide.core.security.permissions.expressions.NotExpression) NotExpression(com.yahoo.elide.core.security.permissions.expressions.NotExpression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression)

Example 3 with OrExpression

use of com.yahoo.elide.core.security.permissions.expressions.OrExpression in project elide by yahoo.

the class PermissionExpressionNormalizationVisitor method visitOrExpression.

@Override
public Expression visitOrExpression(OrExpression orExpression) {
    Expression left = orExpression.getLeft();
    Expression right = orExpression.getRight();
    return new OrExpression(left.accept(this), right.accept(this));
}
Also used : CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) NotExpression(com.yahoo.elide.core.security.permissions.expressions.NotExpression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression)

Example 4 with OrExpression

use of com.yahoo.elide.core.security.permissions.expressions.OrExpression in project elide by yahoo.

the class PermissionExpressionBuilder method buildAnyFieldOnlyExpression.

/**
 * Builds disjunction of permission expression of all requested fields.
 * If the field permission is null, then return default SUCCESSFUL_EXPRESSION.
 * expression = (field1Rule OR field2Rule ... OR fieldNRule)
 * @param condition The condition which triggered this permission expression check
 * @param checkFn check function
 * @param requestedFields The list of requested fields
 * @return Expression
 */
private Expression buildAnyFieldOnlyExpression(final PermissionCondition condition, final Function<Check, Expression> checkFn, final Set<String> requestedFields) {
    Type<?> resourceClass = condition.getEntityClass();
    Class<? extends Annotation> annotationClass = condition.getPermission();
    OrExpression allFieldsExpression = new OrExpression(FAILURE, null);
    List<String> fields = entityDictionary.getAllExposedFields(resourceClass);
    boolean fieldExpressionUsed = false;
    for (String field : fields) {
        if (requestedFields != null && !requestedFields.contains(field)) {
            continue;
        }
        ParseTree fieldPermissions = entityDictionary.getPermissionsForField(resourceClass, field, annotationClass);
        Expression fieldExpression = normalizedExpressionFromParseTree(fieldPermissions, checkFn);
        if (fieldExpression == null) {
            return SUCCESSFUL_EXPRESSION;
        }
        fieldExpressionUsed = true;
        allFieldsExpression = new OrExpression(allFieldsExpression, fieldExpression);
    }
    if (!fieldExpressionUsed) {
        return SUCCESSFUL_EXPRESSION;
    }
    return new AnyFieldExpression(condition, allFieldsExpression);
}
Also used : AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) CheckExpression(com.yahoo.elide.core.security.permissions.expressions.CheckExpression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) SpecificFieldExpression(com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) AndExpression(com.yahoo.elide.core.security.permissions.expressions.AndExpression) AnyFieldExpression(com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) OrExpression(com.yahoo.elide.core.security.permissions.expressions.OrExpression) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Aggregations

AndExpression (com.yahoo.elide.core.security.permissions.expressions.AndExpression)4 CheckExpression (com.yahoo.elide.core.security.permissions.expressions.CheckExpression)4 Expression (com.yahoo.elide.core.security.permissions.expressions.Expression)4 OrExpression (com.yahoo.elide.core.security.permissions.expressions.OrExpression)4 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)2 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)2 AnyFieldExpression (com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression)2 NotExpression (com.yahoo.elide.core.security.permissions.expressions.NotExpression)2 SpecificFieldExpression (com.yahoo.elide.core.security.permissions.expressions.SpecificFieldExpression)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2