use of com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression 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);
}
use of com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression in project elide by yahoo.
the class PermissionExpressionBuilder method buildUserCheckEntityAndAnyFieldExpression.
/**
* Build an expression that strictly evaluates UserCheck's and ignores other checks for an entity.
* expression = (entityRule AND (field1Rule OR field2Rule ... OR fieldNRule))
* <p>
* NOTE: This method returns _NO_ commit checks.
*
* @param resourceClass Resource class
* @param annotationClass Annotation class
* @param scope Request scope
* @param <A> type parameter
* @return User check expression to evaluate
*/
public <A extends Annotation> Expression buildUserCheckEntityAndAnyFieldExpression(final Type<?> resourceClass, final Class<A> annotationClass, Set<String> requestedFields, final RequestScope scope) {
final Function<Check, Expression> leafBuilderFn = (check) -> new CheckExpression(check, null, scope, null, cache);
ParseTree classPermissions = entityDictionary.getPermissionsForClass(resourceClass, annotationClass);
Expression entityExpression = normalizedExpressionFromParseTree(classPermissions, leafBuilderFn);
Expression anyFieldExpression = buildAnyFieldOnlyExpression(new PermissionCondition(annotationClass, resourceClass), leafBuilderFn, requestedFields);
if (entityExpression == null) {
return anyFieldExpression;
}
return new AndExpression(entityExpression, anyFieldExpression);
}
use of com.yahoo.elide.core.security.permissions.expressions.AnyFieldExpression 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);
}
Aggregations