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 eZ \Publish \API \Repository \Values \User \Limitation \ContentTypeLimitation ;
11+ use GraphQL \Error \UserError ;
12+
13+ class CanUser
14+ {
15+ /**
16+ * @var PermissionResolver
17+ */
18+ private $ permissionResolver ;
19+
20+ const MODULE = 'graphql ' ;
21+
22+ const PERMISSION_CONTENT_TYPE_VIEW = 'content_type_view ' ;
23+ /**
24+ * @var ContentTypeService
25+ */
26+ private $ contentTypeService ;
27+
28+ public function __construct (ContentTypeService $ contentTypeService , PermissionResolver $ permissionResolver )
29+ {
30+ $ this ->permissionResolver = $ permissionResolver ;
31+ $ this ->contentTypeService = $ contentTypeService ;
32+ }
33+
34+ public function viewContentType ($ identifier )
35+ {
36+ try {
37+ $ contentType = $ this ->contentTypeService ->loadContentTypeByIdentifier ($ identifier );
38+ } catch (NotFoundException $ e ) {
39+ throw new UserError ("Content type ' $ identifier' not found' " );
40+ }
41+
42+ $ contentInfo = new ContentInfo (['contentTypeId ' => $ contentType ->id ]);
43+ try {
44+ return $ this ->permissionResolver ->canUser (self ::MODULE , self ::PERMISSION_CONTENT_TYPE_VIEW , $ contentInfo );
45+ } catch (BadStateException $ e ) {;
46+ throw new UserError ($ e ->getMessage (), 0 , $ e );
47+ } catch (InvalidArgumentException $ e ) {
48+ throw new UserError ($ e ->getMessage (), 0 , $ e );
49+ }
50+ }
51+
52+ public function hasAccessToContentType ($ identifier )
53+ {
54+ try {
55+ $ access = $ this ->permissionResolver ->hasAccess (self ::MODULE , self ::PERMISSION_CONTENT_TYPE_VIEW );
56+ } catch (InvalidArgumentException $ e ) {
57+ throw new UserError ("oops " , 0 , $ e );
58+ }
59+
60+ if (is_bool ($ access )) {
61+ return $ access ;
62+ }
63+
64+ if (!is_array ($ access )) {
65+ throw new UserError ("Invalid hasAccess() return type " );
66+ }
67+
68+ try {
69+ $ contentType = $ this ->contentTypeService ->loadContentTypeByIdentifier ($ identifier );
70+ } catch (NotFoundException $ e ) {
71+ throw new UserError ("Unknown content type $ identifier " );
72+ }
73+
74+ /** @var \eZ\Publish\API\Repository\Values\User\Policy $policy */
75+ foreach ($ access as $ limitation ) {
76+ foreach ($ limitation ['policies ' ] as $ policy ) {
77+ if ($ policy ->module !== self ::MODULE || $ policy ->function !== self ::PERMISSION_CONTENT_TYPE_VIEW ) {
78+ continue ;
79+ }
80+ foreach ($ policy ->getLimitations () as $ limitation ) {
81+ if (!$ limitation instanceof ContentTypeLimitation) {
82+ continue ;
83+ }
84+
85+ if (in_array ($ contentType ->identifier , $ limitation ->limitationValues )) {
86+ return true ;
87+ }
88+ }
89+ }
90+ }
91+
92+ return false ;
93+ }
94+ }
0 commit comments