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());
}
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();
}
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);
}
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);
}
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()));
}
Aggregations