Search in sources :

Example 96 with PersistentResource

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

the class PermissionExecutorTest method testFailRunAtCommitCheck.

@Test
public void testFailRunAtCommitCheck() throws Exception {
    @Entity
    @Include(rootLevel = false)
    @UpdatePermission(expression = "sampleCommit")
    class Model implements SampleOperationModel {

        @Override
        public boolean test() {
            return false;
        }
    }
    PersistentResource resource = newResource(new Model(), Model.class, false);
    RequestScope requestScope = resource.getRequestScope();
    // Because the check is runAtCommit, the check is DEFERRED.
    assertEquals(ExpressionResult.DEFERRED, requestScope.getPermissionExecutor().checkPermission(UpdatePermission.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) RequestScope(com.yahoo.elide.core.RequestScope) UpdatePermission(com.yahoo.elide.annotation.UpdatePermission) Test(org.junit.jupiter.api.Test)

Example 97 with PersistentResource

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

the class PermissionExecutorTest method testReadFieldAwareFailureAny.

@Test
public void testReadFieldAwareFailureAny() {
    PersistentResource resource = newResource(SampleBean.class, false);
    RequestScope requestScope = resource.getRequestScope();
    assertThrows(ForbiddenAccessException.class, () -> requestScope.getPermissionExecutor().checkSpecificFieldPermissions(resource, null, ReadPermission.class, "mayFailInCommit"));
    requestScope.getPermissionExecutor().executeCommitChecks();
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) ReadPermission(com.yahoo.elide.annotation.ReadPermission) RequestScope(com.yahoo.elide.core.RequestScope) Test(org.junit.jupiter.api.Test)

Example 98 with PersistentResource

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

the class PersistentResourceFetcher method upsertOrUpdateObjects.

/**
 * handle UPSERT or UPDATE operation.
 * @param context Environment encapsulating graphQL's request environment
 * @param updateFunc controls the behavior of how the update (or upsert) is performed.
 * @return Connection object.
 */
private ConnectionContainer upsertOrUpdateObjects(Environment context, Executor<?> updateFunc, RelationshipOp operation) {
    /* sanity check for id and data argument w UPSERT/UPDATE */
    if (context.ids.isPresent()) {
        throw new BadRequestException(operation + " must not include ids");
    }
    if (!context.data.isPresent()) {
        throw new BadRequestException(operation + " must include data argument");
    }
    Type<?> entityClass;
    EntityDictionary dictionary = context.requestScope.getDictionary();
    if (context.isRoot()) {
        entityClass = dictionary.getEntityClass(context.field.getName(), context.requestScope.getApiVersion());
    } else {
        assert context.parentResource != null;
        entityClass = dictionary.getParameterizedType(context.parentResource.getResourceType(), context.field.getName());
    }
    /* form entities */
    Optional<Entity> parentEntity;
    if (!context.isRoot()) {
        assert context.parentResource != null;
        parentEntity = Optional.of(new Entity(Optional.empty(), null, context.parentResource.getResourceType(), context.requestScope));
    } else {
        parentEntity = Optional.empty();
    }
    LinkedHashSet<Entity> entitySet = new LinkedHashSet<>();
    for (Map<String, Object> input : context.data.orElseThrow(IllegalStateException::new)) {
        entitySet.add(new Entity(parentEntity, input, entityClass, context.requestScope));
    }
    /* apply function to upsert/update the object */
    for (Entity entity : entitySet) {
        graphWalker(entity, updateFunc, context);
    }
    /* fixup relationships */
    for (Entity entity : entitySet) {
        graphWalker(entity, this::updateRelationship, context);
        PersistentResource<?> childResource = entity.toPersistentResource();
        if (!context.isRoot()) {
            /* add relation between parent and nested entity */
            assert context.parentResource != null;
            context.parentResource.addRelation(context.field.getName(), childResource);
        }
    }
    String entityName = dictionary.getJsonAliasFor(entityClass);
    Set<PersistentResource> resources = entitySet.stream().map(Entity::toPersistentResource).collect(Collectors.toCollection(LinkedHashSet::new));
    return new ConnectionContainer(resources, Optional.empty(), entityName);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PersistentResource(com.yahoo.elide.core.PersistentResource) ConnectionContainer(com.yahoo.elide.graphql.containers.ConnectionContainer) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary)

Example 99 with PersistentResource

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

the class PersistentResourceFetcher method updateObject.

private PersistentResource updateObject(Entity entity, Environment context) {
    Set<Entity.Attribute> attributes = entity.getAttributes();
    Optional<String> id = entity.getId();
    RequestScope requestScope = entity.getRequestScope();
    PersistentResource<?> updatedResource;
    if (!id.isPresent()) {
        throw new BadRequestException("UPDATE data objects must include ids");
    }
    Set<PersistentResource> loadedResource = fetchObject(requestScope, entity.getProjection(), Optional.of(Collections.singletonList(id.get()))).getPersistentResources();
    updatedResource = loadedResource.iterator().next();
    return updateAttributes(updatedResource, entity, attributes);
}
Also used : PersistentResource(com.yahoo.elide.core.PersistentResource) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) RequestScope(com.yahoo.elide.core.RequestScope)

Example 100 with PersistentResource

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

the class PageInfoContainer method processFetch.

@Override
public Object processFetch(Environment context) {
    String fieldName = context.field.getName();
    ConnectionContainer connectionContainer = getConnectionContainer();
    Optional<Pagination> pagination = connectionContainer.getPagination();
    List<String> ids = connectionContainer.getPersistentResources().stream().map(PersistentResource::getId).sorted().collect(Collectors.toList());
    return pagination.map(pageValue -> {
        switch(KeyWord.byName(fieldName)) {
            case PAGE_INFO_HAS_NEXT_PAGE:
                {
                    int numResults = ids.size();
                    int nextOffset = numResults + pageValue.getOffset();
                    return nextOffset < pageValue.getPageTotals();
                }
            case PAGE_INFO_START_CURSOR:
                return pageValue.getOffset();
            case PAGE_INFO_END_CURSOR:
                return pageValue.getOffset() + ids.size();
            case PAGE_INFO_TOTAL_RECORDS:
                return pageValue.getPageTotals();
            default:
                break;
        }
        throw new BadRequestException("Invalid request. Looking for field: " + fieldName + " in an pageInfo object.");
    }).orElseThrow(() -> new BadRequestException("Could not generate pagination information for type: " + connectionContainer.getTypeName()));
}
Also used : KeyWord(com.yahoo.elide.graphql.KeyWord) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Pagination(com.yahoo.elide.core.request.Pagination) Getter(lombok.Getter) Environment(com.yahoo.elide.graphql.Environment) PersistentResource(com.yahoo.elide.core.PersistentResource) Optional(java.util.Optional) Collectors(java.util.stream.Collectors) Pagination(com.yahoo.elide.core.request.Pagination) PersistentResource(com.yahoo.elide.core.PersistentResource) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

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