Skip to content

Commit 01638e4

Browse files
committed
Add grantFieldAccess & grantTemplateAccess options.
1 parent 632fc05 commit 01638e4

File tree

2 files changed

+114
-15
lines changed

2 files changed

+114
-15
lines changed

ProcessGraphQLConfig.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,36 @@ class ProcessGraphQLConfig extends Moduleconfig {
1111
public function getDefaults()
1212
{
1313
return array(
14+
/**
15+
* Sets the max for ProcessWire's limit selector field.
16+
* @var integer
17+
*/
1418
'maxLimit' => 50,
19+
20+
/**
21+
* Wheather the GraphiQL GUI should be stretched to full width or centered
22+
* like other parts of the ProcessWire's admin back-end.
23+
* @var boolean
24+
*/
1525
'fullWidthGraphiQL' => false,
26+
27+
/**
28+
* An array of template names that will be concidered for schema generation.
29+
* @var array
30+
*/
1631
'legalTemplates' => [],
32+
33+
/**
34+
* An array of field names that will be considered for schema generation.
35+
* @var array
36+
*/
1737
'legalFields' => [],
38+
39+
/**
40+
* An array of built-in Page field names that will be considered for schema
41+
* generation.
42+
* @var array
43+
*/
1844
'legalPageFields' => [
1945
'created',
2046
'modified',
@@ -23,11 +49,29 @@ public function getDefaults()
2349
'name',
2450
'httpUrl',
2551
],
52+
53+
/**
54+
* An array of built-in PageFile field names that will be considered for
55+
* schema createtion.
56+
* @var array
57+
*/
2658
'legalPageFileFields' => [
2759
'url',
2860
'httpUrl',
2961
'description',
3062
],
63+
64+
/**
65+
* Grant access to everyone on a template level.
66+
* @var boolean
67+
*/
68+
'grantTemplatesAccess' => false,
69+
70+
/**
71+
* Grant access to everyone on a field level.
72+
* @var boolean
73+
*/
74+
'grantFieldsAccess' => false,
3175
);
3276
}
3377

@@ -133,6 +177,35 @@ public function getInputFields()
133177
}
134178
$inputfields->add($f);
135179

180+
$fSet = $this->modules->get('InputfieldFieldset');
181+
$fSet->label = 'Advanced';
182+
$fSet->collapsed = Inputfield::collapsedYes;
183+
184+
// templateAccessControl
185+
$f = $this->modules->get('InputfieldCheckbox');
186+
$f->attr('name', 'grantTemplatesAccess');
187+
$f->label = 'Grant Templates Access';
188+
$f->columnWidth = 50;
189+
$desc = "By default only `superuser` can access pages with template that ";
190+
$desc .= "does not have `Access` settings enabled. If you wish to grant ";
191+
$desc .= "access to pages without `Access` settings, check this field. ";
192+
$desc .= "(not recommended)";
193+
$f->description = $desc;
194+
$fSet->add($f);
195+
196+
// fieldAccessControl
197+
$f = $this->modules->get('InputfieldCheckbox');
198+
$f->attr('name', 'grantFieldsAccess');
199+
$f->label = 'Grant Fields Access';
200+
$f->columnWidth = 50;
201+
$desc = "By default only `superuser` can access fields that does not have `Access` ";
202+
$desc .= "settings enabled. If you wish to grant access to fields without `Access` ";
203+
$desc .= "settings, check this field. (not recommended)";
204+
$f->description = $desc;
205+
$fSet->add($f);
206+
207+
$inputfields->add($fSet);
208+
136209
return $inputfields;
137210
}
138211

src/Config.php

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,14 @@ public function get($key)
3232
case 'legalPageFileFields':
3333
return $this->module->$key;
3434
case 'legalViewTemplates':
35-
if ($super) return $this->getLegalTemplates();
3635
return $this->getLegalTemplatesForPermission('page-view');
3736
case 'legalCreateTemplates':
38-
if ($super) return $this->getLegalTemplates();
3937
return $this->getLegalTemplatesForPermission('page-create');
4038
case 'legalEditTemplates':
41-
if ($super) return $this->getLegalTemplates();
4239
return $this->getLegalTemplatesForPermission('page-edit');
4340
case 'legalViewFields':
44-
if ($super) return $this->getLegalFields();
4541
return $this->getLegalFieldsForPermission('view');
4642
case 'legalEditFields':
47-
if ($super) return $this->getLegalFields();
4843
return $this->getLegalFieldsForPermission('edit');
4944
default:
5045
return parent::get($key);
@@ -59,12 +54,33 @@ protected function getLegalTemplates()
5954

6055
protected function getLegalTemplatesForPermission($permission = 'page-view')
6156
{
62-
$templates = $this->getLegalTemplates()->find("useRoles=1");
63-
foreach ($templates as $template) {
64-
if (!Utils::user()->hasTemplatePermission($permission, $template)) {
65-
$templates->remove($template);
57+
$user = Utils::user();
58+
$templates = $this->getLegalTemplates();
59+
60+
// if superuser give access to everything
61+
if ($user->isSuperuser()) return $templates;
62+
63+
// if access is granted then templates are accessable by default
64+
// but if a template has Access settings, user should have relevant
65+
// permissions
66+
if (Utils::moduleConfig()->grantTemplateAccess) {
67+
foreach ($templates as $template) {
68+
if ($template->useRoles && !$user->hasTemplatePermission($permission, $template)) {
69+
$templates->remove($template);
70+
}
71+
}
72+
73+
// if access is not granted then user can see only those templates that
74+
// she has explicit access to.
75+
} else {
76+
$templates->filter("useRoles=1");
77+
foreach ($templates as $template) {
78+
if (!$user->hasTemplatePermission($permission, $template)) {
79+
$templates->remove($template);
80+
}
6681
}
6782
}
83+
6884
return $templates;
6985
}
7086

@@ -76,17 +92,27 @@ protected function getLegalFields()
7692

7793
protected function getLegalFieldsForPermission($permission = 'view')
7894
{
79-
$fields = $this->getLegalFields()->find("useRoles=1");
80-
$rolesType = $permission . "Roles";
81-
foreach ($fields as $field) {
82-
if (!$this->userHasPermission($field->$rolesType)) {
83-
$fields->remove($field);
95+
$fields = $this->getLegalFields();
96+
$roles = $permission . "Roles";
97+
98+
if (Utils::moduleConfig()->grantFieldAccess) {
99+
foreach ($fields as $field) {
100+
if ($field->useRoles && !$this->userHasRoleIn($field->$roles)) {
101+
$fields->remove($field);
102+
}
103+
}
104+
} else {
105+
$fields->find("useRoles=1");
106+
foreach ($fields as $field) {
107+
if (!$this->userHasRoleIn($field->$roles)) {
108+
$fields->remove($field);
109+
}
84110
}
85111
}
86112
return $fields;
87113
}
88114

89-
protected function userHasPermission($rolesID)
115+
protected function userHasRoleIn($rolesID)
90116
{
91117
$userRolesID = Utils::user()->roles->explode('id');
92118
foreach ($userRolesID as $userRoleID) {

0 commit comments

Comments
 (0)