Skip to content

Commit 3ebebb7

Browse files
committed
Make sure user has rights to add children to parent page.
1 parent 71f0893 commit 3ebebb7

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

src/Config.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public function get($key)
4343
return $this->getLegalCreateTemplates();
4444
case 'legalEditTemplates':
4545
return $this->getLegalTemplatesForPermission('page-edit');
46+
case 'legalAddTemplates':
47+
return $this->getLegalTemplatesForPermission('page-add');
4648
case 'legalFields':
4749
return $this->getLegalFields();
4850
default:

src/Field/Mutation/CreateTemplatedPage.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
use Youshido\GraphQL\Exception\ResolveException;
99
use Youshido\GraphQL\Config\Field\FieldConfig;
1010
use Youshido\GraphQL\Field\InputField;
11+
use Youshido\GraphQL\Type\NonNullType;
1112

1213
use ProcessWire\Template;
1314
use ProcessWire\Page;
1415
use ProcessWire\NullPage;
1516
use ProcessWire\Field;
1617
use ProcessWire\FieldtypePage;
1718

19+
use ProcessWire\GraphQL\Utils;
1820
use ProcessWire\GraphQL\Type\Object\TemplatedPageType;
1921
use ProcessWire\GraphQL\Type\Input\TemplatedPageInputType;
2022

@@ -48,16 +50,16 @@ public function build(FieldConfig $config)
4850
{
4951
$config->addArgument(new InputField([
5052
'name' => 'page',
51-
'type' => new TemplatedPageInputType($this->template),
53+
'type' => new NonNullType(new TemplatedPageInputType($this->template)),
5254
]));
5355
}
5456

5557
public function resolve($value, array $args, ResolveInfo $info)
5658
{
5759
// prepare neccessary variables
58-
$pages = \ProcessWire\wire('pages');
59-
$sanitizer = \ProcessWire\wire('sanitizer');
60-
$fields = \ProcessWire\wire('fields');
60+
$pages = Utils::pages();
61+
$sanitizer = Utils::sanitizer();
62+
$fields = Utils::fields();
6163
$values = (array) $args['page'];
6264

6365
/*********************************************\
@@ -71,9 +73,12 @@ public function resolve($value, array $args, ResolveInfo $info)
7173
if ($this->template->noParents === -1 && !$pages->get("template={$this->template}") instanceof NullPage) throw new ValidationException("Only one page with template `{$this->template->name}` can be created.");
7274
// find the parent, make sure it exists
7375
$parentSelector = $values['parent'];
74-
$parent = $pages->find($sanitizer->selectorValue($parentSelector))->first();
76+
$parent = $pages->get($sanitizer->selectorValue($parentSelector));
7577
// if no parent then no good. No child should born without a parent!
76-
if (!$parent || $parent instanceof NullPage) throw new ValidationException("Could not find the `parent` page with `$parentSelector`.");
78+
if (!$parent || $parent instanceof NullPage) throw new ValidationException("Could not find the parent: '$parentSelector'.");
79+
// make sure user is allowed to add children to this parent
80+
$legalAddTemplates = Utils::moduleConfig()->legalAddTemplates;
81+
if (!$legalAddTemplates->has($parent->template)) throw new ValidationException("You are not allowed to add children to the parent: '$parentSelector'.");
7782
// make sure it is allowed as a parent
7883
$parentTemplates = $this->template->parentTemplates;
7984
if (count($parentTemplates) && !in_array($parent->template->id, $parentTemplates)) throw new ValidationException("`parent` is not allowed as a parent.");
@@ -114,7 +119,7 @@ public function resolve($value, array $args, ResolveInfo $info)
114119
}
115120

116121
// save the page to db
117-
if ($p->save()) return $p;
122+
if ($p->save()) return $pages->get("$p");
118123

119124
// If we did not return till now then no good!
120125
throw new ResolveException("Could not create page `$name` with template `{$this->template->name}`");

0 commit comments

Comments
 (0)