|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLAPI\GraphQLAPI\Blocks; |
|
6
|
|
|
|
|
7
|
|
|
use PoP\AccessControl\Schema\SchemaModes; |
|
8
|
|
|
use PoP\AccessControl\ComponentConfiguration; |
|
9
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\AbstractControlBlock; |
|
10
|
|
|
use GraphQLAPI\GraphQLAPI\Facades\ModuleRegistryFacade; |
|
11
|
|
|
use GraphQLAPI\GraphQLAPI\Blocks\GraphQLByPoPBlockTrait; |
|
12
|
|
|
use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
|
13
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AbstractBlockCategory; |
|
14
|
|
|
use GraphQLAPI\GraphQLAPI\BlockCategories\AccessControlBlockCategory; |
|
15
|
|
|
use GraphQLAPI\GraphQLAPI\ModuleResolvers\AccessControlFunctionalityModuleResolver; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Access Control block |
|
19
|
|
|
*/ |
|
20
|
|
|
class AccessControlBlock extends AbstractControlBlock |
|
21
|
|
|
{ |
|
22
|
|
|
use GraphQLByPoPBlockTrait; |
|
23
|
|
|
|
|
24
|
|
|
public const ATTRIBUTE_NAME_SCHEMA_MODE = 'schemaMode'; |
|
25
|
|
|
|
|
26
|
|
|
protected function getBlockName(): string |
|
27
|
|
|
{ |
|
28
|
|
|
return 'access-control'; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function getBlockCategory(): ?AbstractBlockCategory |
|
32
|
|
|
{ |
|
33
|
|
|
$instanceManager = InstanceManagerFacade::getInstance(); |
|
34
|
|
|
return $instanceManager->getInstance(AccessControlBlockCategory::class); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function registerEditorCSS(): bool |
|
38
|
|
|
{ |
|
39
|
|
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
protected function registerCommonStyleCSS(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
protected function enableIndividualControlForPublicPrivateSchemaMode(): bool |
|
47
|
|
|
{ |
|
48
|
|
|
$moduleRegistry = ModuleRegistryFacade::getInstance(); |
|
49
|
|
|
return |
|
50
|
|
|
$moduleRegistry->isModuleEnabled(AccessControlFunctionalityModuleResolver::PUBLIC_PRIVATE_SCHEMA) |
|
51
|
|
|
&& ComponentConfiguration::enableIndividualControlForPublicPrivateSchemaMode(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function getBlockDataTitle(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return \__('Define access for:', 'graphql-api'); |
|
57
|
|
|
} |
|
58
|
|
|
protected function getBlockContentTitle(): string |
|
59
|
|
|
{ |
|
60
|
|
|
if ($this->enableIndividualControlForPublicPrivateSchemaMode()) { |
|
61
|
|
|
return \__('Access Control Rules:', 'graphql-api'); |
|
62
|
|
|
} |
|
63
|
|
|
return \__('Who can access:', 'graphql-api'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Pass localized data to the block |
|
68
|
|
|
* |
|
69
|
|
|
* @return array |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function getLocalizedData(): array |
|
72
|
|
|
{ |
|
73
|
|
|
return array_merge( |
|
74
|
|
|
parent::getLocalizedData(), |
|
75
|
|
|
[ |
|
76
|
|
|
'isIndividualControlForSchemaModeEnabled' => $this->enableIndividualControlForPublicPrivateSchemaMode(), |
|
77
|
|
|
] |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Return the nested blocks' content |
|
83
|
|
|
* |
|
84
|
|
|
* @param array $attributes |
|
85
|
|
|
* @param string $content |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getBlockContent(array $attributes, string $content): string |
|
89
|
|
|
{ |
|
90
|
|
|
$maybeSchemaModeContent = ''; |
|
91
|
|
|
if ($this->enableIndividualControlForPublicPrivateSchemaMode()) { |
|
92
|
|
|
$blockContentPlaceholder = <<<EOT |
|
93
|
|
|
<p><strong>%s</strong> %s</p> |
|
94
|
|
|
<h4 class="%s">%s</h4> |
|
95
|
|
|
EOT; |
|
96
|
|
|
$className = $this->getBlockClassName() . '-front'; |
|
97
|
|
|
$schemaModeLabels = [ |
|
98
|
|
|
SchemaModes::PUBLIC_SCHEMA_MODE => \__('Public', 'graphql-api'), |
|
99
|
|
|
SchemaModes::PRIVATE_SCHEMA_MODE => \__('Private', 'graphql-api'), |
|
100
|
|
|
]; |
|
101
|
|
|
$maybeSchemaModeContent = sprintf( |
|
102
|
|
|
$blockContentPlaceholder, |
|
103
|
|
|
\__('Public/Private Schema:', 'graphql-api'), |
|
104
|
|
|
$attributes[self::ATTRIBUTE_NAME_SCHEMA_MODE] ? |
|
105
|
|
|
$schemaModeLabels[$attributes[self::ATTRIBUTE_NAME_SCHEMA_MODE]] |
|
106
|
|
|
: \__('Default', 'graphql-api'), |
|
107
|
|
|
$className . '__title', |
|
108
|
|
|
\__('Who can access:', 'graphql-api') |
|
109
|
|
|
); |
|
110
|
|
|
} |
|
111
|
|
|
if ($content) { |
|
112
|
|
|
return $maybeSchemaModeContent . $content; |
|
113
|
|
|
} |
|
114
|
|
|
return $maybeSchemaModeContent . sprintf( |
|
115
|
|
|
'<em>%s</em>', |
|
116
|
|
|
\__('(not set)', 'graphql-api') |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|