-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFancyAdminExtension.php
More file actions
250 lines (218 loc) · 10.4 KB
/
Copy pathFancyAdminExtension.php
File metadata and controls
250 lines (218 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php
declare(strict_types=1);
namespace ADT\FancyAdmin\DI;
use ADT\FancyAdmin\Console\CreateIdentityCommand;
use ADT\FancyAdmin\Console\GenerateMissingAclResourcesCommand;
use ADT\FancyAdmin\Core\FancyAdminRouter;
use ADT\FancyAdmin\Model\Entities\AclResource;
use ADT\FancyAdmin\Model\Entities\AclResourceTrait;
use ADT\FancyAdmin\Model\Entities\Enums\AclResourceNameEnum;
use ADT\FancyAdmin\Model\Entities\AclRole;
use ADT\FancyAdmin\Model\Entities\AclRoleTrait;
use ADT\FancyAdmin\Model\Entities\Identity;
use ADT\FancyAdmin\Model\Entities\IdentityPasskeysTrait;
use ADT\FancyAdmin\Model\Entities\IdentityTrait;
use ADT\FancyAdmin\Model\Entities\Traits\HasPasskeys;
use ADT\FancyAdmin\Model\Entities\Profile;
use ADT\FancyAdmin\Model\Entities\ProfileTrait;
use ADT\FancyAdmin\Model\FancyAdmin;
use ADT\FancyAdmin\Model\Queries\Factories\PasskeyQueryFactory;
use ADT\FancyAdmin\Model\Security\Authenticator;
use ADT\FancyAdmin\Model\Security\Keycloak\KeycloakManager;
use ADT\FancyAdmin\Model\Security\Passkey\PasskeyService;
use ADT\FancyAdmin\Model\Security\SecurityUser;
use ADT\FancyAdmin\Model\Services\JsComponents;
use ADT\FancyAdmin\UI\Components\Controls\SidePanel\SidePanelControl;
use ADT\FancyAdmin\UI\Components\Controls\SidePanel\SidePanelControlFactory;
use Contributte\Translation\DI\TranslationProviderInterface;
use Nette\DI\CompilerExtension;
use Nette\DI\Config\Loader;
use Nette\DI\Definitions\Statement;
use Nette\Loaders\RobotLoader;
use Nette\Schema\Expect;
use Nette\Schema\Processor;
use Nette\Schema\Schema;
use Nette\Security\Resource;
use ReflectionClass;
use RuntimeException;
class FancyAdminExtension extends CompilerExtension implements TranslationProviderInterface
{
public function getConfigSchema(): Schema
{
return Expect::structure([
'project' => Expect::string()->default(null),
'projectName' => Expect::string()->default(null),
'adminHostPath' => Expect::string()->default(null),
'defaultCustomerRoute' => Expect::string()->default(':PortalCustomer:Home:'),
'defaultBackofficeRoute' => Expect::string()->default(':PortalBackoffice:Home:'),
'lostPasswordEnabled' => Expect::bool()->default(true),
'logoPublicPath' => Expect::string()->default(null),
'logoMenuPath' => Expect::string()->default(null),
'loginPageLogoPath' => Expect::string()->default(null),
'logoBitmapPublicPath' => Expect::string()->default(null),
'emailBackgroundColor' => Expect::string()->default(null),
'faviconFileNamePng' => Expect::string()->nullable()->default(null),
'faviconFileNameSvg' => Expect::string()->nullable()->default(null),
'hmr' => Expect::bool()->default(false),
'customerAclResource' => Expect::type(Resource::class)->default(AclResourceNameEnum::CUSTOMER_DASHBOARD),
'backofficeAclResource' => Expect::type(Resource::class)->default(AclResourceNameEnum::BACKOFFICE_DASHBOARD),
'fullDataAclResource' => Expect::type(Resource::class)->default(AclResourceNameEnum::FULL_DATA),
'context' => Expect::string()->default(null),
'jsComponentsConfig' => Expect::array()->default([]),
'locksDir' => Expect::string()->required(),
'keycloakEnabled' => Expect::bool()->default(false),
// Vypnutí validace TLS certifikátu Keycloak serveru — POUZE pro lokální vývoj (self-signed cert)
'keycloakVerifySsl' => Expect::bool()->default(true),
'passkeyEnabled' => Expect::bool()->default(false),
// WebAuthn Relying Party ID (doména) — když není nastaveno, odvodí se za běhu host z adminHostPath
'passkeyRpId' => Expect::string()->nullable()->default(null),
// WebAuthn Relying Party name — když není nastaveno, použije se projectName
'passkeyRpName' => Expect::string()->nullable()->default(null),
'colors' => Expect::structure([
'backgroundColor' => Expect::string()->required(),
'dashboardAccentColor' => Expect::string()->required(),
'primaryColor' => Expect::string()->required(),
'primaryColorDark' => Expect::string()->required(),
'primaryColorDark20' => Expect::string()->required(),
'secondaryColor' => Expect::string()->required(),
'secondaryColorDark' => Expect::string()->required(),
'secondaryColorDarker' => Expect::string()->required(),
'ternaryColor' => Expect::string()->required(),
'ternaryTextColor' => Expect::string()->required(),
'loginBackground' => Expect::string()->required(),
'loginInputTextColor' => Expect::string()->required(),
'loginBackgroundInput' => Expect::string()->required(),
'loginBackgroundInputFocus' => Expect::string()->required(),
'inputBorder' => Expect::string()->required(),
'inputFocusBorder' => Expect::string()->required(),
'inputFocusBackground' => Expect::string()->required(),
]),
]);
}
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$this->config = (new Processor)->process($this->getConfigSchema(), $this->config);
$builder->addFactoryDefinition($this->prefix('sidePanelControlFactory'))
->setImplement(SidePanelControlFactory::class)
->getResultDefinition()
->setFactory(SidePanelControl::class);
$builder->addDefinition($this->prefix('fancyAdminRouter'))
->setFactory(FancyAdminRouter::class);
$builder->addDefinition($this->prefix('administration'))
->setFactory(FancyAdmin::class, [
'project' => $this->config->project,
'projectName' => $this->config->projectName,
'adminHostPath' => $this->config->adminHostPath,
'logoPublicPath' => $this->config->logoPublicPath,
'logoMenuPath' => $this->config->logoMenuPath,
'loginPageLogoPath' => $this->config->loginPageLogoPath,
'logoBitmapPublicPath' => $this->config->logoBitmapPublicPath,
'lostPasswordEnabled' => $this->config->lostPasswordEnabled,
'defaultCustomerRoute' => $this->config->defaultCustomerRoute,
'defaultBackofficeRoute' => $this->config->defaultBackofficeRoute,
'emailBackgroundColor' => $this->config->emailBackgroundColor,
'faviconFileNamePng' => $this->config->faviconFileNamePng,
'faviconFileNameSvg' => $this->config->faviconFileNameSvg,
'hmr' => $this->config->hmr,
'customerAclResource' => $this->config->customerAclResource,
'backofficeAclResource' => $this->config->backofficeAclResource,
'fullDataAclResource' => $this->config->fullDataAclResource,
'jsComponentsConfig' => $this->config->jsComponentsConfig,
'context' => $this->config->context,
'colors' => (array) $this->config->colors,
'keycloakEnabled' => $this->config->keycloakEnabled,
'passkeyEnabled' => $this->config->passkeyEnabled,
'passkeyRpId' => $this->config->passkeyRpId,
'passkeyRpName' => $this->config->passkeyRpName,
]);
$builder->addDefinition($this->prefix('jsComponents'))
->setFactory(JsComponents::class);
$builder->addDefinition($this->prefix('passkeyService'))
->setFactory(PasskeyService::class);
// Keycloak — registrace KeycloakManager (instance se vytváří lazy z DB)
if ($this->config->keycloakEnabled) {
$builder->addDefinition($this->prefix('keycloakManager'))
->setFactory(KeycloakManager::class)
->setArgument('verifySsl', $this->config->keycloakVerifySsl);
}
//$this->validateTraitInterfaceCompliance();
// command registration
$defs[] = $builder->addDefinition($this->prefix('createIdentity'))
->setFactory(CreateIdentityCommand::class)
->setAutowired(false);
$defs[] = $builder->addDefinition($this->prefix('generateMissingAclResources'))
->setFactory(GenerateMissingAclResourcesCommand::class, [
'appDir' => $builder->parameters['appDir'],
])
->setAutowired(false);
foreach ($defs as $_def) {
$_def->addSetup('setLocksDir', [$this->config->locksDir]);
}
}
public function beforeCompile(): void
{
$builder = $this->getContainerBuilder();
$securityUserDef = $builder->getDefinitionByType(SecurityUser::class);
$securityUserDef->addSetup('setFullDataAclResource', [$this->config->fullDataAclResource]);
$securityUserDef->addSetup('setBackofficeAclResource', [$this->config->backofficeAclResource]);
$authenticatorDef = $builder->getDefinitionByType(Authenticator::class);
$authenticatorDef->addSetup('setFancyAdmin', [$this->prefix('@administration')]);
$authenticatorDef->addSetup('setExpirationCallback', [
new Statement('Closure::fromCallable', ['@fancyadmin.sessionExpirationCallback']),
]);
if ($this->config->keycloakEnabled) {
$fancyAdminDef = $builder->getDefinition($this->prefix('administration'));
$fancyAdminDef->addSetup('setKeycloakManager', [$this->prefix('@keycloakManager')]);
}
// passkeyEnabled vyžaduje passkey infrastrukturu v projektu — srozumitelná chyba
// při kompilaci kontejneru místo kryptické autowiring hlášky za běhu
if ($this->config->passkeyEnabled && $builder->getByType(PasskeyQueryFactory::class) === null) {
throw new RuntimeException('fancyadmin: passkeyEnabled je zapnuté, ale v projektu chybí implementace ' . PasskeyQueryFactory::class . '. Vytvořte entitu Passkey, PasskeyQuery, PasskeyQueryFactory, PasskeyForm a PasskeyGrid podle README (sekce 19), nebo passkeys vypněte.');
}
}
private function validateTraitInterfaceCompliance(): void
{
$traitInterfaceMap = [
AclResourceTrait::class => AclResource::class,
AclRoleTrait::class => AclRole::class,
IdentityTrait::class => Identity::class,
IdentityPasskeysTrait::class => HasPasskeys::class,
ProfileTrait::class => Profile::class,
];
$loader = new RobotLoader();
$loader->addDirectory(__DIR__ . '/../../../../../app/Model/Entities');
$loader->acceptFiles = ['*.php'];
$loader->rebuild();
foreach (array_keys($loader->getIndexedClasses()) as $class) {
if (!class_exists($class)) {
continue;
}
$reflection = new ReflectionClass($class);
if (!$reflection->isInstantiable() || $reflection->isAbstract()) {
continue;
}
$usedTraits = $this->class_uses_recursive($class);
foreach ($traitInterfaceMap as $trait => $interface) {
if (in_array($trait, $usedTraits, true) && !$reflection->implementsInterface($interface)) {
throw new RuntimeException("Třída $class používá $trait, ale neimplementuje požadované rozhraní $interface.");
}
}
}
}
private function class_uses_recursive(string $class): array
{
$results = [];
do {
$results += class_uses($class);
} while ($class = get_parent_class($class));
foreach ($results as $trait) {
$results += $this->class_uses_recursive($trait);
}
return array_unique($results);
}
public function getTranslationResources(): array
{
return [__DIR__ . '/../lang'];
}
}