99
1010class Config extends WireData {
1111
12+ /**
13+ * Shortcut for our ProcessGraphQL module
14+ * @var \ProcessWire\ProcessGraphQL
15+ */
1216 protected $ module ;
1317
18+ /**
19+ * Maps permission names into template access roles properties.
20+ * @var array
21+ */
22+ protected $ permissionToRoles = [
23+ 'page-view ' => 'roles ' ,
24+ 'page-edit ' => 'editRoles ' ,
25+ 'page-add ' => 'addRoles ' ,
26+ 'page-create ' => 'createRoles ' ,
27+ ];
28+
1429 public function __construct (ProcessGraphQL $ module )
1530 {
1631 $ this ->module = $ module ;
@@ -72,7 +87,7 @@ protected function getLegalTemplatesForPermission($permission = 'page-view')
7287 // permissions
7388 if (Utils::moduleConfig ()->grantTemplatesAccess ) {
7489 foreach ($ templates as $ template ) {
75- if ($ template ->useRoles && !$ user ->hasTemplatePermission ($ permission , $ template )) {
90+ if ($ template ->useRoles && !$ this ->hasTemplatePermission ($ permission, $ user , $ template )) {
7691 $ templates ->remove ($ template );
7792 }
7893 }
@@ -82,7 +97,7 @@ protected function getLegalTemplatesForPermission($permission = 'page-view')
8297 } else {
8398 $ templates ->filter ("useRoles=1 " );
8499 foreach ($ templates as $ template ) {
85- if (!$ user ->hasTemplatePermission ($ permission , $ template )) {
100+ if (!$ this ->hasTemplatePermission ($ permission, $ user , $ template )) {
86101 $ templates ->remove ($ template );
87102 }
88103 }
@@ -91,6 +106,24 @@ protected function getLegalTemplatesForPermission($permission = 'page-view')
91106 return $ templates ;
92107 }
93108
109+ /**
110+ * Checks if the user has a particular permission on the given template
111+ * @param string $name The name of the permission. E.g. 'page-view', 'page-add'.
112+ * @param User $user The ProcessWire User
113+ * @param Template $template The ProcessWire Template
114+ * @return boolean Returns true if user has a permission on the target template and false otherwise
115+ */
116+ protected function hasTemplatePermission ($ name , \ProcessWire \User $ user , \ProcessWire \Template $ template )
117+ {
118+ $ rolesName = $ this ->permissionToRoles [$ name ];
119+ $ templateRoles = $ template ->$ rolesName ;
120+ if ($ name === 'page-view ' ) $ templateRoles = $ templateRoles ->explode ('id ' );
121+ foreach ($ user ->roles as $ role ) {
122+ if (in_array ($ role ->id , $ templateRoles )) return true ;
123+ }
124+ return false ;
125+ }
126+
94127 /**
95128 * Page cannot be created without it's required field
96129 * populated with value. Therefore only templates that
0 commit comments