Search in sources :

Example 81 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method testReadCheckExpressionForNewlyCreatedObject.

@Test
public void testReadCheckExpressionForNewlyCreatedObject() {
    @Entity
    @Include(rootLevel = false)
    @ReadPermission(expression = "FailOp")
    class Model {
    }
    PersistentResource resource = newResource(new Model(), Model.class, true);
    RequestScope requestScope = resource.getRequestScope();
    requestScope.getDictionary().bindEntity(Model.class);
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkPermission(ReadPermission.class, resource));
    assertThrows(ForbiddenAccessException.class, () -> requestScope.getPermissionExecutor().executeCommitChecks());
}
Also used : Entity(javax.persistence.Entity) PersistentResource(com.yahoo.elide.core.PersistentResource) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 82 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method testSuccessfulCommitChecks.

@Test
public void testSuccessfulCommitChecks() throws Exception {
    @Entity
    @Include(rootLevel = false)
    @UpdatePermission(expression = "sampleOperation")
    class Model implements SampleOperationModel {
    }
    PersistentResource resource = newResource(new Model(), Model.class, true);
    RequestScope requestScope = resource.getRequestScope();
    // Because the object is newly created, the check is DEFERRED.
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkPermission(UpdatePermission.class, resource, ALL_FIELDS));
    requestScope.getPermissionExecutor().executeCommitChecks();
}
Also used : Entity(javax.persistence.Entity) PersistentResource(com.yahoo.elide.core.PersistentResource) Include(com.yahoo.elide.annotation.Include) RequestScope(com.yahoo.elide.core.RequestScope) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) Test(org.junit.jupiter.api.Test)

Example 83 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method testSpecificFieldCommitCheckFailByOveriddenField.

@Test
public void testSpecificFieldCommitCheckFailByOveriddenField() {
    PersistentResource resource = newResource(CheckedEntity.class, true);
    RequestScope requestScope = resource.getRequestScope();
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkSpecificFieldPermissions(resource, new ChangeSpec(null, null, null, null), UpdatePermission.class, "hello"));
    assertThrows(ForbiddenAccessException.class, () -> requestScope.getPermissionExecutor().executeCommitChecks());
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) RequestScope(com.yahoo.elide.core.RequestScope) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) Test(org.junit.jupiter.api.Test)

Example 84 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExpressionBuilderTest method testAnyFieldExpressionText.

@Test
public void testAnyFieldExpressionText() {
    @Entity
    @Include(rootLevel = false)
    @ReadPermission(expression = "user has all access AND user has no access")
    class Model {
    }
    dictionary.bindEntity(Model.class);
    PersistentResource resource = newResource(new Model(), Model.class);
    Expression expression = builder.buildAnyFieldExpressions(resource, ReadPermission.class, null, null);
    assertEquals("READ PERMISSION WAS INVOKED ON PersistentResource{type=model, id=null}  " + "FOR EXPRESSION [((user has all access \u001B[34mWAS UNEVALUATED\u001B[m)) " + "AND ((user has no access \u001B[34mWAS UNEVALUATED\u001B[m))]", expression.toString());
    expression.evaluate(Expression.EvaluationMode.ALL_CHECKS);
    assertEquals("READ PERMISSION WAS INVOKED ON PersistentResource{type=model, id=null}  " + "FOR EXPRESSION [((user has all access PASSED)) " + "AND ((user has no access FAILED))]", expression.toString());
}
Also used : Entity(javax.persistence.Entity) PersistentResource(com.yahoo.elide.core.PersistentResource) Expression(com.yahoo.elide.core.security.permissions.expressions.Expression) Include(com.yahoo.elide.annotation.Include) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Test(org.junit.jupiter.api.Test)

Example 85 with PersistentResource

use of com.yahoo.elide.core.PersistentResource in project elide by yahoo.

the class PermissionExecutorTest method testUpdateFieldAwareSuccessAny.

@Test
public void testUpdateFieldAwareSuccessAny() {
    SampleBean sampleBean = new SampleBean();
    sampleBean.id = 1L;
    PersistentResource resource = newResource(sampleBean, SampleBean.class, true);
    RequestScope requestScope = resource.getRequestScope();
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkSpecificFieldPermissions(resource, new ChangeSpec(null, null, null, null), UpdatePermission.class, "mayFailInCommit"));
    requestScope.getPermissionExecutor().executeCommitChecks();
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) RequestScope(com.yahoo.elide.core.RequestScope) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) Test(org.junit.jupiter.api.Test)

Aggregations

PersistentResource (com.yahoo.elide.core.PersistentResource)100 Test (org.junit.jupiter.api.Test)71 RequestScope (com.yahoo.elide.core.RequestScope)60 ReadPermission (com.yahoo.elide.annotation.ReadPermission)18 UpdatePermission (com.yahoo.elide.annotation.UpdatePermission)18 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)17 Include (com.yahoo.elide.annotation.Include)16 Entity (javax.persistence.Entity)16 Resource (com.yahoo.elide.jsonapi.models.Resource)13 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 NotFilterExpression (com.yahoo.elide.core.filter.expression.NotFilterExpression)10 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)10 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)10 JsonApiDocument (com.yahoo.elide.jsonapi.models.JsonApiDocument)10 Book (example.Book)10 LinkedHashSet (java.util.LinkedHashSet)9 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)8 BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)8 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)8 RSQLFilterDialect (com.yahoo.elide.core.filter.dialect.RSQLFilterDialect)7