Skip to content

Commit c37a4be

Browse files
committed
Refactor Config for more granular permissions control.
1 parent 8cd73f0 commit c37a4be

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

src/Config.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ProcessWire\WireData;
66
use ProcessWire\ProcessGraphQL;
7+
use ProcessWire\GraphQL\Utils;
78

89
class Config extends WireData {
910

@@ -13,20 +14,12 @@ public function __construct(ProcessGraphQL $module)
1314
{
1415
$this->module = $module;
1516

16-
// cache reference for useful ProcessWire API variables
17-
$apiVars = [
18-
'templates',
19-
'fields',
20-
'roles',
21-
'users',
22-
'pages',
23-
];
24-
foreach ($apiVars as $varName) {
25-
$this->$varName = \ProcessWire\wire($varName);
26-
}
27-
2817
// Assign Config to module so we can access it easily accross the module codebase.
2918
$module->Config = $this;
19+
20+
// Wierd behavior with ProcessWire. $user->hasPermission() does not
21+
// work if you do not load the required roles beforehand.
22+
Utils::roles()->find("");
3023
}
3124

3225
public function get($key)
@@ -39,8 +32,18 @@ public function get($key)
3932
return $this->module->$key;
4033
case 'legalTemplates':
4134
return $this->getLegalTemplates();
35+
case 'legalViewTemplates':
36+
return $this->getLegalTemplatesForPermission('page-view');
37+
case 'legalCreateTemplates':
38+
return $this->getLegalTemplatesForPermission('page-create');
39+
case 'legalEditTemplates':
40+
return $this->getLegalTemplatesForPermission('page-edit');
4241
case 'legalFields':
4342
return $this->getLegalFields();
43+
case 'legalViewFields':
44+
return $this->getLegalFieldsForPermission('view');
45+
case 'legalEditFields':
46+
return $this->getLegalFieldsForPermission('edit');
4447
default:
4548
return parent::get($key);
4649
}
@@ -49,28 +52,48 @@ public function get($key)
4952
protected function getLegalTemplates()
5053
{
5154
$legalTemplates = $this->module->legalTemplates;
52-
$templates = \ProcessWire\wire('templates')->find("name=" . implode('|', $legalTemplates));
53-
$user = \ProcessWire\wire('user');
54-
55-
// Wierd behavior with ProcessWire. $user->hasPermission() does not
56-
// work if you do not load the required roles beforehand.
57-
\ProcessWire\wire('roles')->find("");
55+
$templates = Utils::templates()->find("name=" . implode('|', $legalTemplates));
56+
return $templates;
57+
}
5858

59+
protected function getLegalTemplatesForPermission($permission = 'page-view')
60+
{
61+
$templates = $this->getLegalTemplates();
5962
foreach ($templates as $template) {
60-
// We serve only those that user has permission to view
61-
if (!$user->hasTemplatePermission('page-view', $template)) {
63+
if (!Utils::user()->hasTemplatePermission($permission, $template)) {
6264
$templates->remove($template);
6365
}
6466
}
65-
6667
return $templates;
6768
}
6869

6970
protected function getLegalFields()
7071
{
7172
$legalFields = $this->module->legalFields;
72-
$fields = \ProcessWire\wire('fields')->find("name=" . implode('|', $legalFields));
73+
$fields = Utils::fields()->find("name=" . implode('|', $legalFields));
74+
if (Utils::user()->isSuperuser()) return $fields;
75+
return $fields->find("useRoles=1");
76+
}
77+
78+
protected function getLegalFieldsForPermission($permission = 'view')
79+
{
80+
$fields = $this->getLegalFields();
81+
$rolesType = $permission . "Roles";
82+
foreach ($fields as $field) {
83+
if (!$this->userHasPermission($field->$rolesType)) {
84+
$fields->remove($field);
85+
}
86+
}
7387
return $fields;
7488
}
7589

90+
protected function userHasPermission($rolesID)
91+
{
92+
$userRolesID = Utils::user()->roles->explode('id');
93+
foreach ($userRolesID as $userRoleID) {
94+
if (in_array($userRoleID, $rolesID)) return true;
95+
}
96+
return false;
97+
}
98+
7699
}

src/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function build(SchemaConfig $config)
2727
$query->addField(new PagesField());
2828

2929
// $templates
30-
foreach (Utils::moduleConfig()->legalTemplates as $template) {
30+
foreach (Utils::moduleConfig()->legalViewTemplates as $template) {
3131
$query->addField(new TemplatedPageArrayField($template));
3232
}
3333

@@ -54,7 +54,7 @@ public function build(SchemaConfig $config)
5454
$mutation = $config->getMutation();
5555

5656
// CreatePage
57-
foreach (Utils::moduleConfig()->legalTemplates as $template) {
57+
foreach (Utils::moduleConfig()->legalCreateTemplates as $template) {
5858
$mutation->addField(new CreateTemplatedPage($template));
5959
}
6060

src/Type/InterfaceType/PaginatedArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function resolveType($pageArray)
6868

6969
// if there is only one template selected then we can assume it is a TemplatedPageArray
7070
if (count($templateSelector->values) === 1) {
71-
$template = Utils::moduleConfig()->legalTemplates->get($templateSelector->values[0]);
71+
$template = Utils::moduleConfig()->legalViewTemplates->get($templateSelector->values[0]);
7272
return new TemplatedPageArrayType($template);
7373
}
7474
return new PageArrayType();

src/Type/Scalar/SelectorType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function serialize($selectors)
4040

4141
// make sure to limit the search to legal templates only
4242
$templateSelector = self::findSelectorByField($selectors, 'template');
43-
$legalTemplates = Utils::moduleConfig()->legalTemplates;
43+
$legalTemplates = Utils::moduleConfig()->legalViewTemplates;
4444
$names = [];
4545

4646
if ($templateSelector instanceof Selector) {

src/Utils.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ public static function modules()
5050
return self::wire('modules');
5151
}
5252

53+
/**
54+
* Shortcut for wire('templates')
55+
* @return \ProcessWire\Fields The ProcessWire $templates API variable.
56+
*/
57+
public static function templates()
58+
{
59+
return self::wire('templates');
60+
}
61+
5362
/**
5463
* Shortcut for wire('fields')
5564
* @return \ProcessWire\Fields The ProcessWire $fields API variable.

0 commit comments

Comments
 (0)