use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class StartState method handle.
@Override
public void handle(StateContext state, RootCollectionLoadEntityContext ctx) {
PersistentResource record = entityRecord(state, ctx.entity());
state.setState(new RecordTerminalState(record));
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class StartState method handle.
@Override
public void handle(StateContext state, RootCollectionRelationshipContext ctx) {
PersistentResource record = entityRecord(state, ctx.entity());
EntityProjection projection = state.getRequestScope().getEntityProjection();
String relationName = ctx.relationship().term().getText();
record.getRelationCheckedFiltered(projection.getRelationship(relationName).orElseThrow(IllegalStateException::new));
state.setState(new RelationshipTerminalState(record, relationName, projection));
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class DefaultJSONApiLinks method getPathSegment.
private String getPathSegment(List<ResourceLineage.LineagePath> path) {
StringBuilder result = new StringBuilder();
int pathSegmentCount = 0;
for (ResourceLineage.LineagePath pathElement : path) {
PersistentResource resource = pathElement.getResource();
if (pathSegmentCount > 0) {
result.append("/");
result.append(String.join("/", resource.getId(), pathElement.getRelationship()));
} else {
result.append(String.join("/", resource.getTypeName(), resource.getId(), pathElement.getRelationship()));
}
pathSegmentCount++;
}
return result.toString();
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testCustomFilterJoin.
@Test
public void testCustomFilterJoin() throws Exception {
RSQLFilterDialect dialect = RSQLFilterDialect.builder().dictionary(scope.getDictionary()).build();
FilterExpression expression = dialect.parseFilterExpression("genre==foo", ClassType.of(Book.class), true);
Book book = new Book();
PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
DataStoreTransaction tx = scope.getTransaction();
when(permissionExecutor.checkUserPermissions(ClassType.of(Book.class), ReadPermission.class, GENRE)).thenReturn(ExpressionResult.DEFERRED);
when(permissionExecutor.checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE)).thenThrow(new ForbiddenAccessException(ReadPermission.class));
when(permissionExecutor.evaluateFilterJoinUserChecks(any(), any())).thenReturn(ExpressionResult.DEFERRED);
when(permissionExecutor.handleFilterJoinReject(any(), any(), any())).thenAnswer(invocation -> {
FilterPredicate filterPredicate = invocation.getArgument(0);
PathElement pathElement = invocation.getArgument(1);
ForbiddenAccessException reason = invocation.getArgument(2);
assertEquals("Book", pathElement.getType().getSimpleName());
assertEquals(GENRE, filterPredicate.getField());
assertEquals("book.genre IN [foo]", filterPredicate.toString());
// custom processing
return "Book".equals(pathElement.getType().getSimpleName()) && filterPredicate.toString().matches("book.genre IN \\[\\w+\\]") && reason.getLoggedMessage().matches(".*Message=ReadPermission Denied.*\\n.*") ? ExpressionResult.DEFERRED : ExpressionResult.FAIL;
});
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertTrue(expression.accept(visitor));
verify(permissionExecutor, times(1)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(1)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, GENRE);
verify(permissionExecutor, never()).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, times(1)).handleFilterJoinReject(any(), any(), any());
verify(tx, never()).getToManyRelation(any(), any(), any(), any());
}
use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.
the class VerifyFieldAccessFilterExpressionVisitorTest method testReject.
@Test
public void testReject() {
Path p1Path = new Path(Arrays.asList(new PathElement(Book.class, Author.class, AUTHORS), new PathElement(Author.class, String.class, NAME)));
FilterPredicate p1 = new InPredicate(p1Path, "foo", "bar");
Path p2Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, HOME)));
FilterPredicate p2 = new InPredicate(p2Path, "blah");
Path p3Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
FilterPredicate p3 = new InPredicate(p3Path, SCIFI);
// P4 is a duplicate of P3
Path p4Path = new Path(Arrays.asList(new PathElement(Book.class, String.class, GENRE)));
FilterPredicate p4 = new InPredicate(p4Path, SCIFI);
OrFilterExpression or = new OrFilterExpression(p2, p3);
AndFilterExpression and1 = new AndFilterExpression(or, p1);
AndFilterExpression and2 = new AndFilterExpression(and1, p4);
NotFilterExpression not = new NotFilterExpression(and2);
Book book = new Book();
Author author = new Author();
book.setAuthors(Collections.singleton(author));
author.setBooks(Collections.singleton(book));
PersistentResource<Book> resource = new PersistentResource<>(book, "", scope);
PermissionExecutor permissionExecutor = scope.getPermissionExecutor();
when(permissionExecutor.checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME)).thenThrow(ForbiddenAccessException.class);
VerifyFieldAccessFilterExpressionVisitor visitor = new VerifyFieldAccessFilterExpressionVisitor(resource);
// restricted HOME field
assertFalse(not.accept(visitor));
assertFalse(and1.accept(visitor));
assertFalse(and2.accept(visitor));
assertFalse(or.accept(visitor));
assertFalse(p2.accept(visitor));
// unrestricted fields
assertTrue(p1.accept(visitor));
assertTrue(p3.accept(visitor));
assertTrue(p4.accept(visitor));
verify(permissionExecutor, times(8)).evaluateFilterJoinUserChecks(any(), any());
verify(permissionExecutor, times(5)).checkSpecificFieldPermissions(resource, null, ReadPermission.class, HOME);
verify(permissionExecutor, times(9)).checkUserPermissions(any(), any(), isA(String.class));
verify(permissionExecutor, times(5)).handleFilterJoinReject(any(), any(), any());
}
Aggregations