public RequestScope getRequestScope() { if (record != null) { return record.getRequestScope(); } return null; }
public RequestScope getRequestScope() { if (record != null) { return record.getRequestScope(); } return null; }
boolean isMutation = resource.getRequestScope().isMutatingMultipleEntities(); if (!isMutation || !resource.getRequestScope().getNewResources().contains(resource)) { resource.checkFieldAwarePermissions(permission);
boolean isMutation = resource.getRequestScope().isMutatingMultipleEntities(); if (!isMutation || !resource.getRequestScope().getNewResources().contains(resource)) { resource.checkFieldAwarePermissions(permission);
/** * Adds all the relation resources for a given relation path to the included block of the * JsonApiDocument. */ private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath) { //Pop off a relation of relation path String relation = relationPath.remove(0); Optional<FilterExpression> filterExpression = rec.getRequestScope().getExpressionForRelation(rec, relation); Set<PersistentResource> collection; try { collection = rec.getRelationCheckedFiltered(relation, filterExpression, Optional.empty(), Optional.empty()); } catch (ForbiddenAccessException e) { return; } collection.forEach(resource -> { jsonApiDocument.addIncluded(resource.toResource()); //If more relations left in the path, process a level deeper if (!relationPath.isEmpty()) { //Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath)); } }); }
/** * Check if adding or updating a relation is allowed. * * @param resourceIdentifiers The persistent resources that are being added */ protected void checkSharePermission(Set<PersistentResource> resourceIdentifiers) { if (resourceIdentifiers == null) { return; } final Set<PersistentResource> newResources = getRequestScope().getNewPersistentResources(); for (PersistentResource persistentResource : resourceIdentifiers) { if (!newResources.contains(persistentResource) && !lineage.getRecord(persistentResource.getType()).contains(persistentResource)) { checkPermission(SharePermission.class, persistentResource); } } }
/** * Adds all the relation resources for a given relation path to the included block of the * JsonApiDocument. */ private void addResourcesForPath(JsonApiDocument jsonApiDocument, PersistentResource<?> rec, List<String> relationPath) { //Pop off a relation of relation path String relation = relationPath.remove(0); Optional<FilterExpression> filterExpression = rec.getRequestScope().getExpressionForRelation(rec, relation); Set<PersistentResource> collection; try { collection = rec.getRelationCheckedFiltered(relation, filterExpression, Optional.empty(), Optional.empty()); } catch (ForbiddenAccessException e) { return; } collection.forEach(resource -> { jsonApiDocument.addIncluded(resource.toResource()); //If more relations left in the path, process a level deeper if (!relationPath.isEmpty()) { //Use a copy of the relationPath to preserve the path for remaining branches of the relationship tree addResourcesForPath(jsonApiDocument, resource, new ArrayList<>(relationPath)); } }); }
/** * Check if adding or updating a relation is allowed. * * @param resourceIdentifiers The persistent resources that are being added */ protected void checkSharePermission(Set<PersistentResource> resourceIdentifiers) { if (resourceIdentifiers == null) { return; } final Set<PersistentResource> newResources = getRequestScope().getNewPersistentResources(); for (PersistentResource persistentResource : resourceIdentifiers) { if (!newResources.contains(persistentResource) && !lineage.getRecord(persistentResource.getType()).contains(persistentResource)) { checkPermission(SharePermission.class, persistentResource); } } }
/** * Audit an action on an entity. * * @param action the action * @param changeSpec the change that occurred */ protected void auditClass(Audit.Action action, ChangeSpec changeSpec) { Audit[] annotations = getResourceClass().getAnnotationsByType(Audit.class); if (annotations == null) { return; } for (Audit annotation : annotations) { for (Audit.Action auditAction : annotation.action()) { if (auditAction == action) { // compare object reference LogMessage message = new LogMessage(annotation, this, Optional.ofNullable(changeSpec)); getRequestScope().getAuditLogger().log(message); } } } }
@Override public void onNext(CRUDEvent event) { ArrayList<LifeCycleHook> hooks = new ArrayList<>(); //Collect all the hooks that are keyed on a specific field. hooks.addAll(dictionary.getTriggers( event.getResource().getResourceClass(), this.annotation, event.getFieldName())); //Collect all the hooks that are keyed on any field. if (!event.getFieldName().isEmpty()) { hooks.addAll(dictionary.getTriggers(event.getResource().getResourceClass(), this.annotation)); } try { //Invoke all the hooks hooks.forEach((hook) -> { hook.execute( event.getResource().getObject(), event.getResource().getRequestScope(), event.getChanges()); }); } catch (RuntimeException e) { exception = Optional.of(e); if (throwsExceptions) { throw e; } } }
/** * Audit an action on an entity. * * @param action the action * @param changeSpec the change that occurred */ protected void auditClass(Audit.Action action, ChangeSpec changeSpec) { Audit[] annotations = getResourceClass().getAnnotationsByType(Audit.class); if (annotations == null) { return; } for (Audit annotation : annotations) { for (Audit.Action auditAction : annotation.action()) { if (auditAction == action) { // compare object reference LogMessage message = new LogMessage(annotation, this, Optional.ofNullable(changeSpec)); getRequestScope().getAuditLogger().log(message); } } } }
@Override public void onNext(CRUDEvent event) { ArrayList<LifeCycleHook> hooks = new ArrayList<>(); //Collect all the hooks that are keyed on a specific field. hooks.addAll(dictionary.getTriggers( event.getResource().getResourceClass(), this.annotation, event.getFieldName())); //Collect all the hooks that are keyed on any field. if (!event.getFieldName().isEmpty()) { hooks.addAll(dictionary.getTriggers(event.getResource().getResourceClass(), this.annotation)); } try { //Invoke all the hooks hooks.forEach((hook) -> { hook.execute( event.getResource().getObject(), event.getResource().getRequestScope(), event.getChanges()); }); } catch (RuntimeException e) { exception = Optional.of(e); if (throwsExceptions) { throw e; } } }
Optional<String> filters, boolean generateTotals) { EntityDictionary dictionary = parentResource.getRequestScope().getDictionary(); Class entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName); String typeName = dictionary.getJsonAliasFor(entityClass); Optional<FilterExpression> filter = buildFilter(typeName, filters, parentResource.getRequestScope());
Optional<String> filters, boolean generateTotals) { EntityDictionary dictionary = parentResource.getRequestScope().getDictionary(); Class entityClass = dictionary.getParameterizedType(parentResource.getObject(), fieldName); String typeName = dictionary.getJsonAliasFor(entityClass); Optional<FilterExpression> filter = buildFilter(typeName, filters, parentResource.getRequestScope());
/** * Audit an action on field. * * @param changeSpec Change spec for audit */ protected void auditField(final ChangeSpec changeSpec) { final String fieldName = changeSpec.getFieldName(); Audit[] annotations = dictionary.getAttributeOrRelationAnnotations(getResourceClass(), Audit.class, fieldName ); if (annotations == null || annotations.length == 0) { // Default to class-level annotation for action auditClass(Audit.Action.UPDATE, changeSpec); return; } for (Audit annotation : annotations) { if (annotation.action().length == 1 && annotation.action()[0] == Audit.Action.UPDATE) { LogMessage message = new LogMessage(annotation, this, Optional.of(changeSpec)); getRequestScope().getAuditLogger().log(message); } else { throw new InvalidSyntaxException("Only Audit.Action.UPDATE is allowed on fields."); } } }
/** * Audit an action on field. * * @param changeSpec Change spec for audit */ protected void auditField(final ChangeSpec changeSpec) { final String fieldName = changeSpec.getFieldName(); Audit[] annotations = dictionary.getAttributeOrRelationAnnotations(getResourceClass(), Audit.class, fieldName ); if (annotations == null || annotations.length == 0) { // Default to class-level annotation for action auditClass(Audit.Action.UPDATE, changeSpec); return; } for (Audit annotation : annotations) { if (annotation.action().length == 1 && annotation.action()[0] == Audit.Action.UPDATE) { LogMessage message = new LogMessage(annotation, this, Optional.of(changeSpec)); getRequestScope().getAuditLogger().log(message); } else { throw new InvalidSyntaxException("Only Audit.Action.UPDATE is allowed on fields."); } } }