use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitor method visitPredicate.
/**
* Enforce ReadPermission on provided query filter.
*
* @return true if allowed, false if rejected
*/
@Override
public Boolean visitPredicate(FilterPredicate filterPredicate) {
RequestScope requestScope = resource.getRequestScope();
Set<PersistentResource> val = Collections.singleton(resource);
PermissionExecutor permissionExecutor = requestScope.getPermissionExecutor();
ExpressionResult result = permissionExecutor.evaluateFilterJoinUserChecks(resource, filterPredicate);
if (result == ExpressionResult.UNEVALUATED) {
result = evaluateUserChecks(filterPredicate, permissionExecutor);
}
if (result == ExpressionResult.PASS) {
return true;
}
if (result == ExpressionResult.FAIL) {
return false;
}
for (PathElement element : filterPredicate.getPath().getPathElements()) {
String fieldName = element.getFieldName();
if ("this".equals(fieldName)) {
continue;
}
try {
val = val.stream().filter(Objects::nonNull).flatMap(x -> getValueChecked(x, fieldName, requestScope).toList(LinkedHashSet::new).blockingGet().stream()).filter(Objects::nonNull).collect(Collectors.toSet());
} catch (ForbiddenAccessException e) {
result = permissionExecutor.handleFilterJoinReject(filterPredicate, element, e);
if (result == ExpressionResult.DEFERRED) {
continue;
}
// pass or fail
return result == ExpressionResult.PASS;
}
}
return true;
}
use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.
the class FilterPredicate method toString.
@Override
public String toString() {
List<PathElement> elements = path.getPathElements();
StringBuilder formattedPath = new StringBuilder();
if (!elements.isEmpty()) {
formattedPath.append(StringUtils.uncapitalize(EntityDictionary.getSimpleName(elements.get(0).getType())));
}
for (PathElement element : elements) {
formattedPath.append(PERIOD).append(element.getFieldName());
}
return formattedPath.append(' ').append(operator).append(' ').append(values).toString();
}
use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.
the class FilterPredicate method getEntityType.
public Type getEntityType() {
List<PathElement> elements = path.getPathElements();
PathElement first = elements.get(0);
return first.getType();
}
use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.
the class JoinExpressionExtractor method visitJoinReference.
@Override
public Set<String> visitJoinReference(JoinReference reference) {
JoinPath joinPath = reference.getPath();
List<PathElement> pathElements = joinPath.getPathElements();
ColumnContext currentCtx = this.context;
for (int i = 0; i < pathElements.size() - 1; i++) {
PathElement pathElement = pathElements.get(i);
Type<?> joinClass = pathElement.getFieldType();
String joinFieldName = pathElement.getFieldName();
SQLJoin sqlJoin = currentCtx.getQueryable().getJoin(joinFieldName);
ColumnContext joinCtx;
String onClause;
JoinType joinType;
String fullExpression;
if (sqlJoin != null) {
joinType = sqlJoin.getJoinType();
joinCtx = (ColumnContext) currentCtx.get(joinFieldName);
if (joinType.equals(JoinType.CROSS)) {
onClause = EMPTY;
} else {
onClause = ON + currentCtx.resolve(sqlJoin.getJoinExpression());
}
} else {
joinType = JoinType.LEFT;
SQLTable table = metaDataStore.getTable(joinClass);
joinCtx = ColumnContext.builder().queryable(table).alias(appendAlias(currentCtx.getAlias(), joinFieldName)).metaDataStore(currentCtx.getMetaDataStore()).column(currentCtx.getColumn()).tableArguments(mergedArgumentMap(table.getArguments(), currentCtx.getTableArguments())).build();
onClause = ON + String.format("%s.%s = %s.%s", currentCtx.getAlias(), dictionary.getAnnotatedColumnName(pathElement.getType(), joinFieldName), joinCtx.getAlias(), dictionary.getAnnotatedColumnName(joinClass, dictionary.getIdFieldName(joinClass)));
}
SQLDialect sqlDialect = currentCtx.getQueryable().getDialect();
String joinAlias = applyQuotes(joinCtx.getAlias(), sqlDialect);
String joinKeyword = currentCtx.getQueryable().getDialect().getJoinKeyword(joinType);
String joinSource = constructTableOrSubselect(joinCtx, joinClass);
if (sqlDialect.useASBeforeTableAlias()) {
fullExpression = String.format("%s %s AS %s %s", joinKeyword, joinSource, joinAlias, onClause);
} else {
fullExpression = String.format("%s %s %s %s", joinKeyword, joinSource, joinAlias, onClause);
}
joinExpressions.add(fullExpression);
/**
* If this `for` loop runs more than once, context should be switched to join context.
*/
currentCtx = joinCtx;
}
// If reference within current join reference is of type PhysicalReference, then below visitor doesn't matter.
// If it is of type LogicalReference, then visitLogicalReference method will recreate visitor with correct
// value of ColumnProjection in context.
JoinExpressionExtractor visitor = new JoinExpressionExtractor(currentCtx);
joinExpressions.addAll(reference.getReference().accept(visitor));
return joinExpressions;
}
use of com.yahoo.elide.core.Path.PathElement in project elide by yahoo.
the class InMemoryFilterExecutorTest method negativeTests.
@Test
public void negativeTests() throws Exception {
author = new Author();
author.setId(10L);
PathElement pathElement = new PathElement(Author.class, Long.class, "id");
expression = new NotFilterExpression(new LTPredicate(pathElement, listEleven));
fn = expression.accept(visitor);
assertFalse(fn.test(author));
expression = new NotFilterExpression(new LEPredicate(pathElement, listTen));
fn = expression.accept(visitor);
assertFalse(fn.test(author));
expression = new NotFilterExpression(new GTPredicate(pathElement, listNine));
fn = expression.accept(visitor);
assertFalse(fn.test(author));
expression = new NotFilterExpression(new GEPredicate(pathElement, listTen));
fn = expression.accept(visitor);
assertFalse(fn.test(author));
expression = new NotFilterExpression(new LTPredicate(pathElement, listTen));
fn = expression.accept(visitor);
assertTrue(fn.test(author));
expression = new NotFilterExpression(new LEPredicate(pathElement, listNine));
fn = expression.accept(visitor);
assertTrue(fn.test(author));
expression = new NotFilterExpression(new GTPredicate(pathElement, listTen));
fn = expression.accept(visitor);
assertTrue(fn.test(author));
expression = new NotFilterExpression(new GEPredicate(pathElement, listEleven));
fn = expression.accept(visitor);
assertTrue(fn.test(author));
}
Aggregations