Skip to content

Commit 01ce7cc

Browse files
committed
Disable most of the built in page fields and add option to enable them manually.
1 parent 7cef197 commit 01ce7cc

File tree

3 files changed

+75
-41
lines changed

3 files changed

+75
-41
lines changed

ProcessGraphQLConfig.php

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<?php namespace ProcessWire;
22

3+
use \ProcessWire\GraphQL\Type\InterfaceType\PageInterfaceType;
4+
5+
require_once $this->config->paths->site . 'modules/ProcessGraphQL/vendor/autoload.php';
6+
37
class ProcessGraphQLConfig extends Moduleconfig {
48

59
public function getDefaults()
@@ -9,6 +13,14 @@ public function getDefaults()
913
'debug' => false,
1014
'legalTemplates' => [],
1115
'legalFields' => [],
16+
'legalPageFields' => [
17+
'created',
18+
'modified',
19+
'url',
20+
'id',
21+
'name',
22+
'httpUrl',
23+
],
1224
'fullWidthGraphiql' => false,
1325
);
1426
}
@@ -17,6 +29,7 @@ public function getInputFields()
1729
{
1830
$inputfields = parent::getInputFields();
1931

32+
// maxLimit
2033
$f = $this->modules->get('InputfieldInteger');
2134
$f->attr('name', 'maxLimit');
2235
$f->label = 'Max Limit';
@@ -25,43 +38,51 @@ public function getInputFields()
2538
$f->columnWidth = 35;
2639
$inputfields->add($f);
2740

28-
$f = $this->modules->get('InputfieldCheckbox');
29-
$f->attr('name', 'debug');
30-
$f->label = 'Debug';
31-
$f->description = 'When you turn on debug mode some extra fields will be available. Like `dbQueryCount` etc.';
32-
$f->columnWidth = 35;
33-
$inputfields->add($f);
34-
41+
// GraphiQL full width
3542
$f = $this->modules->get('InputfieldCheckbox');
3643
$f->attr('name', 'fullWidthGraphiQL');
3744
$f->label = 'Full width GraphiQL';
3845
$f->description = 'Check this if you want GraphiQL on the backend to stretch to full width.';
3946
$f->columnWidth = 30;
4047
$inputfields->add($f);
4148

49+
// legalTemplates
4250
$f = $this->modules->get('InputfieldCheckboxes');
43-
foreach (\ProcessWire\wire('templates') as $template) {
44-
$f->addOption($template->name, $template->flags & Template::flagSystem ? "{$template->name} (system)" : $template->name);
45-
}
4651
$f->optionColumns = 4;
4752
$f->attr('name', 'legalTemplates');
4853
$f->label = 'Legal Templates';
4954
$f->description = 'The pages with the templates that you select below will be available via your GraphQL api.';
50-
$f->notes = 'Please be careful with what you are exposing to the public. Choosing templates marked as system can lead to security issues.';
55+
$f->notes = 'Please be careful with what you are exposing to the public. Choosing templates marked as `system` can lead to security vulnerabilities.';
56+
foreach (\ProcessWire\wire('templates') as $template) {
57+
$f->addOption($template->name, $template->flags & Template::flagSystem ? "{$template->name} `(system)`" : $template->name);
58+
}
5159
$inputfields->add($f);
5260

61+
// legalFields
5362
$f = $this->modules->get('InputfieldCheckboxes');
54-
foreach (\ProcessWire\wire('fields')->find("name!=pass") as $field) {
63+
$f->optionColumns = 4;
64+
$f->attr('name', 'legalFields');
65+
$f->label = 'Legal Fields';
66+
$f->description = 'The fields that you select below will be available via your GraphQL api.';
67+
$f->notes = 'Please be careful with what you are exposing to the public. Choosing fields marked as `system` can to lead security vulnerabilities.';
68+
foreach (\ProcessWire\wire('fields')->find("name!=pass") as $field) {
5569
if ($field->type instanceof FieldtypeFieldsetOpen) continue;
5670
if ($field->type instanceof FieldtypeFieldsetClose) continue;
5771
if ($field->type instanceof FieldtypeFieldsetTabOpen) continue;
58-
$f->addOption($field->name, $field->flags & Field::flagSystem ? "{$field->name} (system)" : $field->name);
72+
$f->addOption($field->name, $field->flags & Field::flagSystem ? "{$field->name} `(system)`" : $field->name);
5973
}
74+
$inputfields->add($f);
75+
76+
// legalPageFields
77+
$f = $this->modules->get('InputfieldCheckboxes');
6078
$f->optionColumns = 4;
61-
$f->attr('name', 'legalFields');
62-
$f->label = 'Legal Fields';
63-
$f->description = 'The fields that you select below will be available via your GraphQL api.';
64-
$f->notes = 'Please be careful with what you are exposing to the public. Choosing fields marked as system can to lead security issues.';
79+
$f->attr('name', 'legalPageFields');
80+
$f->label = 'Legal Page Fields';
81+
$f->description = 'Choose which built in page fields you wish to be available via GraphQL api.';
82+
$f->notes = 'Be careful with fields like `parents` & `children` that will allow user to construct deeply nested queries that might be very expensive for your server to fulfill.';
83+
foreach (PageInterfaceType::getPageFields() as $fieldName => $fieldClassName) {
84+
$f->addOption($fieldName);
85+
}
6586
$inputfields->add($f);
6687

6788
return $inputfields;

src/Settings.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@ public static function getLegalFields()
3939
return $fields;
4040
}
4141

42+
public static function getLegalPageFields()
43+
{
44+
return self::module()->legalPageFields;
45+
}
46+
4247
}

src/Type/InterfaceType/PageInterfaceType.php

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,12 @@ public function getDescription()
2121

2222
public function build($config)
2323
{
24-
$pageTypeFieldClassNames = [
25-
'PageChildField',
26-
'PageChildrenField',
27-
'PageCreatedField',
28-
'PageCreatedUserField',
29-
'PageFindField',
30-
'PageHttpUrlField',
31-
'PageIdField',
32-
'PageModifiedField',
33-
'PageModifiedUserField',
34-
'PageNameField',
35-
'PageNextField',
36-
'PageNumChildrenField',
37-
'PageParentField',
38-
'PageParentIdField',
39-
'PageParentsField',
40-
'PagePathField',
41-
'PagePrevField',
42-
'PageRootParentField',
43-
'PageSiblingsField',
44-
'PageUrlField',
45-
];
46-
foreach ($pageTypeFieldClassNames as $pageTypeFieldClassName) {
47-
$className = "ProcessWire\\GraphQL\\Field\\Page\\$pageTypeFieldClassName";
24+
$fields = self::getPageFields();
25+
$legalPageFields = Settings::getLegalPageFields();
26+
27+
foreach ($fields as $fieldName => $fieldClassName) {
28+
if (!in_array($fieldName, $legalPageFields)) continue;
29+
$className = "ProcessWire\\GraphQL\\Field\\Page\\$fieldClassName";
4830
$config->addField(new $className());
4931
}
5032

@@ -64,4 +46,30 @@ public function resolveType($page)
6446
return new TemplatedPageType($page->template);
6547
}
6648

49+
public static function getPageFields()
50+
{
51+
return [
52+
'child' => 'PageChildField',
53+
'children' => 'PageChildrenField',
54+
'created' => 'PageCreatedField',
55+
'createdUser' => 'PageCreatedUserField',
56+
'find' => 'PageFindField',
57+
'httpUrl' => 'PageHttpUrlField',
58+
'id' => 'PageIdField',
59+
'modified' => 'PageModifiedField',
60+
'modifiedUser' => 'PageModifiedUserField',
61+
'name' => 'PageNameField',
62+
'next' => 'PageNextField',
63+
'numChildren' => 'PageNumChildrenField',
64+
'parent' => 'PageParentField',
65+
'parentId' => 'PageParentIdField',
66+
'parents' => 'PageParentsField',
67+
'path' => 'PagePathField',
68+
'prev' => 'PagePrevField',
69+
'rootParent' => 'PageRootParentField',
70+
'siblings' => 'PageSiblingsField',
71+
'url' => 'PageUrlField',
72+
];
73+
}
74+
6775
}

0 commit comments

Comments
 (0)