Skip to content

Commit 632fc05

Browse files
committed
Merge branch 'dev'
2 parents 7cfb311 + 137e297 commit 632fc05

File tree

12 files changed

+164
-43
lines changed

12 files changed

+164
-43
lines changed

Todo.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ with particular height and/or width. If there isn't a thumbnail variation with s
2020
size, then create it.
2121
- Mind permissions for thumbnail creation.
2222

23-
### Add support for LanguageFields
24-
By default the content of the LanguageFields should resolve to the `$user->language`.
25-
In addition to that there will be a top level field called `language` which will
26-
allow the client to choose the language for each request.
23+
#### Make GraphQL processing a hookable method for extra user control
2724

2825
[n1-problem]: https://secure.phabricator.com/book/phabcontrib/article/n_plus_one/

src/Config.php

Lines changed: 48 additions & 26 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,34 +14,38 @@ 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)
3326
{
27+
$super = Utils::user()->isSuperuser();
3428
switch ($key) {
3529
case 'maxLimit':
3630
case 'fullWidthGraphiQL':
3731
case 'legalPageFields':
3832
case 'legalPageFileFields':
3933
return $this->module->$key;
40-
case 'legalTemplates':
41-
return $this->getLegalTemplates();
42-
case 'legalFields':
43-
return $this->getLegalFields();
34+
case 'legalViewTemplates':
35+
if ($super) return $this->getLegalTemplates();
36+
return $this->getLegalTemplatesForPermission('page-view');
37+
case 'legalCreateTemplates':
38+
if ($super) return $this->getLegalTemplates();
39+
return $this->getLegalTemplatesForPermission('page-create');
40+
case 'legalEditTemplates':
41+
if ($super) return $this->getLegalTemplates();
42+
return $this->getLegalTemplatesForPermission('page-edit');
43+
case 'legalViewFields':
44+
if ($super) return $this->getLegalFields();
45+
return $this->getLegalFieldsForPermission('view');
46+
case 'legalEditFields':
47+
if ($super) return $this->getLegalFields();
48+
return $this->getLegalFieldsForPermission('edit');
4449
default:
4550
return parent::get($key);
4651
}
@@ -49,28 +54,45 @@ public function get($key)
4954
protected function getLegalTemplates()
5055
{
5156
$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("");
57+
return Utils::templates()->find("name=" . implode('|', $legalTemplates));
58+
}
5859

60+
protected function getLegalTemplatesForPermission($permission = 'page-view')
61+
{
62+
$templates = $this->getLegalTemplates()->find("useRoles=1");
5963
foreach ($templates as $template) {
60-
// We serve only those that user has permission to view
61-
if (!$user->hasTemplatePermission('page-view', $template)) {
64+
if (!Utils::user()->hasTemplatePermission($permission, $template)) {
6265
$templates->remove($template);
6366
}
6467
}
65-
6668
return $templates;
6769
}
6870

6971
protected function getLegalFields()
7072
{
7173
$legalFields = $this->module->legalFields;
72-
$fields = \ProcessWire\wire('fields')->find("name=" . implode('|', $legalFields));
74+
return Utils::fields()->find("name=" . implode('|', $legalFields));
75+
}
76+
77+
protected function getLegalFieldsForPermission($permission = 'view')
78+
{
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);
84+
}
85+
}
7386
return $fields;
7487
}
7588

89+
protected function userHasPermission($rolesID)
90+
{
91+
$userRolesID = Utils::user()->roles->explode('id');
92+
foreach ($userRolesID as $userRoleID) {
93+
if (in_array($userRoleID, $rolesID)) return true;
94+
}
95+
return false;
96+
}
97+
7698
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\PageImage;
4+
5+
use ProcessWire\WireData;
6+
use ProcessWire\WireArray;
7+
8+
class EmptyPageImage extends WireData {
9+
10+
public function getVariations()
11+
{
12+
return new WireArray();
13+
}
14+
15+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\PageImage;
4+
5+
use Youshido\GraphQL\Field\AbstractField;
6+
use Youshido\GraphQL\Config\Field\FieldConfig;
7+
use Youshido\GraphQL\Type\Scalar\IntType;
8+
use ProcessWire\GraphQL\Type\Object\PageImageType;
9+
use Youshido\GraphQL\Execution\ResolveInfo;
10+
use ProcessWire\GraphQL\Utils;
11+
use ProcessWire\GraphQL\Field\PageImage\EmptyPageImage;
12+
13+
class PageImageSizeField extends AbstractField{
14+
15+
public function getType()
16+
{
17+
return new PageImageType();
18+
}
19+
20+
public function getName()
21+
{
22+
return 'size';
23+
}
24+
25+
public function getDescription()
26+
{
27+
return 'Create a thumbnail of the PageImage with the desired size.';
28+
}
29+
30+
public function build(FieldConfig $config)
31+
{
32+
$config->addArgument('width', [
33+
'type' => new IntType(),
34+
'description' => 'Target width of the new image',
35+
]);
36+
$config->addArgument('height', [
37+
'type' => new IntType(),
38+
'description' => 'Target height of the new image',
39+
]);
40+
}
41+
42+
public function resolve($value, array $args, ResolveInfo $info)
43+
{
44+
$canCreate = Utils::moduleConfig()->legalEditFields->has($value->field);
45+
$width = isset($args['width']) ? $args['width'] : null;
46+
$height = isset($args['height']) ? $args['height'] : null;
47+
48+
// if there neither width nor heigth is given then we return empty image
49+
if (!$width && !$height) return new EmptyPageImage();
50+
51+
// we create the image if user have rights for it
52+
if ($canCreate) return $value->size($width, $height);
53+
54+
// if user has no rights to create the image then she
55+
// might be asking for variation already created
56+
$options = [];
57+
if ($width) $options['width'] = $width;
58+
if ($height) $options['height'] = $height;
59+
$thumbs = $value->getVariations($options);
60+
61+
// If we find the variation then return it
62+
// otherwise return empty
63+
if ($thumbs->count()) return $thumbs->first();
64+
return new EmptyPageImage();
65+
}
66+
}

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/PageFileInterfaceType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function getPageFileFields()
5353
'type' => new StringType(),
5454
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
5555
'resolve' => function ($value) {
56-
return (string) $value->httpUrl();
56+
return (string) $value->httpUrl;
5757
}
5858
],
5959

@@ -133,4 +133,4 @@ public static function getPageFileFields()
133133
}
134134

135135

136-
}
136+
}

src/Type/InterfaceType/PageInterfaceType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ public function build($config)
2323
{
2424
$fields = self::getPageFields();
2525
$legalPageFields = Utils::moduleConfig()->legalPageFields;
26-
26+
2727
foreach ($fields as $fieldName => $fieldClassName) {
2828
if (!in_array($fieldName, $legalPageFields)) continue;
2929
$className = "ProcessWire\\GraphQL\\Field\\Page\\$fieldClassName";
3030
$config->addField(new $className());
3131
}
3232

3333
// add global fields too
34-
$legalFields = Utils::moduleConfig()->legalFields;
34+
$legalFields = Utils::moduleConfig()->legalViewFields;
3535
foreach ($legalFields as $field) {
3636
if ($field->flags & Field::flagGlobal) {
3737
$className = "\\ProcessWire\\GraphQL\\Field\\Page\\Fieldtype\\" . $field->type->className();
@@ -72,4 +72,4 @@ public static function getPageFields()
7272
];
7373
}
7474

75-
}
75+
}

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/Object/PageImageType.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use Youshido\GraphQL\Type\Object\AbstractObjectType;
66
use Youshido\GraphQL\Type\Scalar\StringType;
77
use Youshido\GraphQL\Type\Scalar\IntType;
8+
use Youshido\GraphQL\Type\ListType\ListType;
89
use ProcessWire\GraphQL\Type\InterfaceType\PageFileInterfaceType;
10+
use ProcessWire\GraphQL\Field\PageImage\PageImageSizeField;
911

1012
class PageImageType extends AbstractObjectType {
1113

@@ -21,7 +23,7 @@ public function getDescription()
2123

2224
public function build($config)
2325
{
24-
26+
2527
$config->applyInterface(new PageFileInterfaceType());
2628

2729
$config->addfield('width', [
@@ -39,11 +41,21 @@ public function build($config)
3941
return (integer) $value->height;
4042
}
4143
]);
44+
45+
$config->addField('variations', [
46+
'type' => new ListType(new PageImageType()),
47+
'description' => 'Returns all size variations of the image.',
48+
'resolve' => function ($value) {
49+
return $value->getVariations();
50+
}
51+
]);
52+
53+
$config->addField(new PageImageSizeField());
4254
}
4355

4456
public function getInterfaces()
4557
{
4658
return [ new PageFileInterfaceType() ];
4759
}
4860

49-
}
61+
}

src/Type/Object/TemplatedPageType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getDescription()
3737

3838
public function build($config)
3939
{
40-
$legalFields = Utils::moduleConfig()->legalFields;
40+
$legalFields = Utils::moduleConfig()->legalViewFields;
4141
$config->applyInterface(new PageInterfaceType());
4242
foreach ($this->template->fields as $field) {
4343
if (!$legalFields->has($field)) continue;
@@ -53,4 +53,4 @@ public function getInterfaces()
5353
return [new PageInterfaceType()];
5454
}
5555

56-
}
56+
}

0 commit comments

Comments
 (0)