Skip to content

Commit 48b76a0

Browse files
author
Bertrand Dunogier
committed
Custom graphql/content policy
Allows to restrict which content types a user is allowed to see over graphql. ``` DomainGroupContent: type: object fields: articles: type: "[ArticleContent]" public: '@=service("ezplatform_graphql.can_user").viewContentOfType("article")' ``` fixup! Custom graphql/content_type_view policy
1 parent 8f15573 commit 48b76a0

File tree

5 files changed

+82
-2
lines changed

5 files changed

+82
-2
lines changed

BDEzPlatformGraphQLBundle.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,29 @@
11
<?php
2-
32
namespace BD\EzPlatformGraphQLBundle;
43

54
use BD\EzPlatformGraphQLBundle\DependencyInjection\Compiler;
5+
use BD\EzPlatformGraphQLBundle\DependencyInjection\Security\PolicyProvider;
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Symfony\Component\HttpKernel\Bundle\Bundle;
88

99
class BDEzPlatformGraphQLBundle extends Bundle
1010
{
1111
public function build(ContainerBuilder $container)
1212
{
13-
parent::build($container); // TODO: Change the autogenerated stub
13+
parent::build($container);
1414

1515
$container->addCompilerPass(new Compiler\FieldValueTypesPass());
1616
$container->addCompilerPass(new Compiler\FieldValueBuildersPass());
1717
$container->addCompilerPass(new Compiler\SchemaWorkersPass());
1818
$container->addCompilerPass(new Compiler\SchemaBuildersPass());
19+
20+
$this->loadPolicyProviders($container);
21+
}
22+
23+
private function loadPolicyProviders(ContainerBuilder $container)
24+
{
25+
$extension = $container->getExtension('ezpublish');
26+
// Add the policy provider.
27+
$extension->addPolicyProvider(new PolicyProvider());
1928
}
2029
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace BD\EzPlatformGraphQLBundle\DependencyInjection\Security;
3+
4+
use eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Security\PolicyProvider\YamlPolicyProvider;
5+
6+
class PolicyProvider extends YamlPolicyProvider
7+
{
8+
protected function getFiles()
9+
{
10+
return [__DIR__ . '/../../Resources/config/policies.yml'];
11+
}
12+
}

Resources/config/policies.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
graphql:
2+
content_type_view:
3+
- Class
4+
content:
5+
- Class

Resources/config/services.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ services:
2828

2929
BD\EzPlatformGraphQLBundle\GraphQL\ExpressionLanguage\Access\HasEzAccessToOneOfFunction:
3030
tags: ['overblog_graphql.expression_function']
31+
32+
ezplatform_graphql.can_user:
33+
autowire: true
34+
class: 'BD\EzPlatformGraphQLBundle\Security\CanUser'

Security/CanUser.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace BD\EzPlatformGraphQLBundle\Security;
3+
4+
use eZ\Publish\API\Repository\ContentTypeService;
5+
use eZ\Publish\API\Repository\Exceptions\BadStateException;
6+
use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
7+
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
8+
use eZ\Publish\API\Repository\PermissionResolver;
9+
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
10+
use GraphQL\Error\UserError;
11+
12+
class CanUser
13+
{
14+
/**
15+
* @var PermissionResolver
16+
*/
17+
private $permissionResolver;
18+
19+
const MODULE = 'graphql';
20+
21+
const FUNCTION_CONTENT = 'content';
22+
/**
23+
* @var ContentTypeService
24+
*/
25+
private $contentTypeService;
26+
27+
public function __construct(ContentTypeService $contentTypeService, PermissionResolver $permissionResolver)
28+
{
29+
$this->permissionResolver = $permissionResolver;
30+
$this->contentTypeService = $contentTypeService;
31+
}
32+
33+
public function viewContentOfType($identifier)
34+
{
35+
try {
36+
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($identifier);
37+
} catch (NotFoundException $e) {
38+
throw new UserError("Content type '$identifier' not found'");
39+
}
40+
41+
$contentInfo = new ContentInfo(['contentTypeId' => $contentType->id]);
42+
try {
43+
return $this->permissionResolver->canUser(self::MODULE, self::FUNCTION_CONTENT, $contentInfo);
44+
} catch (BadStateException $e) {;
45+
throw new UserError($e->getMessage(), 0, $e);
46+
} catch (InvalidArgumentException $e) {
47+
throw new UserError($e->getMessage(), 0, $e);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)