Skip to content

Commit

Permalink
Compatibility with new context providers
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Carlino committed Jan 27, 2021
1 parent fbe05df commit ebb4c8f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/GraphQL/Plugins/UnpublishOnDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use SilverStripe\Core\Extensible;
use SilverStripe\GraphQL\QueryHandler\QueryHandler;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use SilverStripe\GraphQL\Schema\Field\ModelMutation;
use SilverStripe\GraphQL\Schema\Interfaces\ModelMutationPlugin;
use SilverStripe\GraphQL\Schema\Schema;
Expand Down Expand Up @@ -65,8 +66,8 @@ public static function unpublishOnDelete(array $context)
if (!$object->hasExtension(Versioned::class) || !$object->isPublished()) {
continue;
}

if (!$object->canUnpublish($context[QueryHandler::CURRENT_USER])) {
$member = UserContextProvider::get($context);
if (!$object->canUnpublish($member)) {
throw new Exception(sprintf(
'Cannot unpublish %s with ID %s',
get_class($object),
Expand Down
9 changes: 8 additions & 1 deletion src/GraphQL/Plugins/VersionedDataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void
$type->addField('version', 'Int');

$versionType = Type::create($versionName)
->mergeWith($type)
->addField('author', ['type' => $memberTypeName] + $resolver)
->addField('publisher', ['type' => $memberTypeName] + $resolver)
->addField('published', ['type' => 'Boolean'] + $resolver)
Expand All @@ -90,6 +89,14 @@ public function apply(ModelType $type, Schema $schema, array $config = []): void
->addField('draft', ['type' => 'Boolean'] + $resolver)
->addField('latestDraftVersion', ['type' => 'Boolean'] + $resolver);

foreach ($type->getFields() as $field) {
$clone = clone $field;
$versionType->addField($clone->getName(), $clone);
}
foreach ($type->getInterfaces() as $interface) {
$versionType->addInterface($interface);
}

$schema->addType($versionType);
$type->addField('versions', '[' . $versionName . ']', function (Field $field) use ($type) {
$field->setResolver([VersionedResolver::class, 'resolveVersionList'])
Expand Down
14 changes: 9 additions & 5 deletions src/GraphQL/Resolvers/VersionedResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GraphQL\Type\Definition\ResolveInfo;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\GraphQL\QueryHandler\QueryHandler;
use SilverStripe\GraphQL\QueryHandler\UserContextProvider;
use SilverStripe\GraphQL\Resolvers\VersionFilters;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
Expand Down Expand Up @@ -73,7 +74,8 @@ public static function resolveVersionList(array $resolverContext): Closure
$sourceClass
));
}
if (!$object->canViewStage(Versioned::DRAFT, $context[QueryHandler::CURRENT_USER])) {
$member = UserContextProvider::get($context);
if (!$object->canViewStage(Versioned::DRAFT, $member)) {
throw new Exception(sprintf(
'Cannot view versions on %s',
$this->getDataObjectClass()
Expand Down Expand Up @@ -134,10 +136,11 @@ public static function resolveCopyToStage(array $context): Closure
throw new InvalidArgumentException("Record {$id} not found");
}

$member = UserContextProvider::get($context);
// Permission check object
$can = $to === Versioned::LIVE
? $record->canPublish($context[QueryHandler::CURRENT_USER])
: $record->canEdit($context[QueryHandler::CURRENT_USER]);
? $record->canPublish($member)
: $record->canEdit($member);
if (!$can) {
throw new InvalidArgumentException(sprintf(
'Copying %s from %s to %s is not allowed',
Expand Down Expand Up @@ -189,7 +192,8 @@ public static function resolvePublishOperation(array $context)
));
}
$permissionMethod = $isPublish ? 'canPublish' : 'canUnpublish';
if (!$obj->$permissionMethod($context[QueryHandler::CURRENT_USER])) {
$member = UserContextProvider::get($context);
if (!$obj->$permissionMethod($member)) {
throw new Exception(sprintf(
'Not allowed to change published state of this %s',
$dataClass
Expand Down Expand Up @@ -236,7 +240,7 @@ public static function resolveRollback(array $context)
$record = Versioned::get_latest_version($dataClass, $id);

// Assert permission
$user = $context[QueryHandler::CURRENT_USER];
$user = UserContextProvider::get($context);
if (!$record->canEdit($user)) {
throw new InvalidArgumentException('Current user does not have permission to roll back this resource');
}
Expand Down

0 comments on commit ebb4c8f

Please sign in to comment.