Skip to content

Commit b932c6f

Browse files
author
Bertrand Dunogier
committed
Prototyped permissions (#13)
The `_repository` field will only show up for users with at least one of content/edit, class/update or role/view. Can easily be extended to more granular items.
1 parent 21f1b25 commit b932c6f

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace BD\EzPlatformGraphQLBundle\GraphQL\ExpressionLanguage;
3+
4+
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionFunction;
5+
6+
class HasAdminAccessFunction extends ExpressionFunction
7+
{
8+
public function __construct()
9+
{
10+
parent::__construct(
11+
'hasAdminAccess',
12+
function () {
13+
return $this->buildHasAccessCode(["section/view", "class/create", "role/read"]);
14+
}
15+
);
16+
}
17+
18+
private function buildHasAccessCode(array $policies)
19+
{
20+
$checks = array_map(
21+
function($policy) {
22+
list($module, $function) = explode('/', $policy);
23+
return sprintf(
24+
'(true === ($access = $pr->hasAccess("%s", "%s")) || is_array($access))',
25+
$module,
26+
$function
27+
);
28+
},
29+
$policies
30+
);
31+
32+
return sprintf('(function() use ($globalVariable) {
33+
$pr = $globalVariable->get("container")->get("eZ\Publish\API\Repository\PermissionResolver");
34+
return %s;
35+
})()', implode('||', $checks));
36+
}
37+
}

Resources/config/graphql/Platform.types.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ Platform:
55
_repository:
66
type: Repository
77
resolve: { }
8-
description: "eZ Platform repository API"
8+
description: "eZ Platform repository API"
9+
public: '@=hasAdminAccess()'
10+
public: '@=hasAdminAccess()'

Resources/config/services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ services:
2222
- { name: "overblog_graphql.mutation", alias: "DeleteSection", method: "deleteSection" }
2323

2424
BD\EzPlatformGraphQLBundle\GraphQL\InputMapper\SearchQueryMapper: ~
25+
26+
BD\EzPlatformGraphQLBundle\GraphQL\ExpressionLanguage\HasAdminAccessFunction:
27+
tags: ['overblog_graphql.expression_function']

0 commit comments

Comments
 (0)