1 | <?php |
||
35 | abstract class AbstractPlugin extends Plugin |
||
36 | { |
||
37 | |||
38 | const SAML_CORE_HANDLE = 'saml-core'; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | public $hasCpSettings = true; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | public $hasCpSection = true; |
||
49 | |||
50 | abstract public function loadSaml2Container(): AbstractContainer; |
||
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | abstract public function getProviderRecordClass(); |
||
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | abstract public function getProviderIdentityRecordClass(); |
||
61 | |||
62 | public function init() |
||
63 | { |
||
64 | parent::init(); |
||
65 | |||
66 | $this->initCore(); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * |
||
71 | */ |
||
72 | public function initCore() |
||
73 | { |
||
74 | \Craft::setAlias('@flipbox/saml/core/web/assets', __DIR__ . '/web/assets'); |
||
75 | $this->registerTemplates(); |
||
76 | |||
77 | $this->setComponents([ |
||
78 | 'cp' => Cp::class, |
||
79 | 'psr3logger' => [ |
||
80 | 'class' => Logger::class, |
||
81 | ], |
||
82 | 'metadata' => Metadata::class, |
||
83 | 'logoutRequest' => LogoutRequest::class, |
||
84 | 'logoutResponse' => LogoutResponse::class, |
||
85 | ]); |
||
86 | |||
87 | /** |
||
88 | * Set saml plugin to the craft variable |
||
89 | */ |
||
90 | Event::on( |
||
91 | CraftVariable::class, |
||
92 | CraftVariable::EVENT_INIT, |
||
93 | function (Event $event) { |
||
94 | /** @var CraftVariable $variable */ |
||
95 | $variable = $event->sender; |
||
96 | $variable->set($this->getPluginVariableHandle(), self::getInstance()); |
||
97 | $variable->set('samlCp', $this->getCp()); |
||
98 | } |
||
99 | ); |
||
100 | |||
101 | Event::on( |
||
102 | View::class, |
||
103 | View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
||
104 | function (RegisterTemplateRootsEvent $e) { |
||
105 | if (is_dir($baseDir = (dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'))) { |
||
106 | $e->roots[static::SAML_CORE_HANDLE] = $baseDir; |
||
107 | } |
||
108 | } |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @inheritdoc |
||
114 | */ |
||
115 | public function getSettingsResponse() |
||
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | private function getSubNav() |
||
168 | |||
169 | /** |
||
170 | * @inheritdoc |
||
171 | */ |
||
172 | public function getCpNavItem() |
||
178 | |||
179 | /** |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getPluginVariableHandle() |
||
186 | |||
187 | /** |
||
188 | * Registering the core templates for SP and IDP to use. |
||
189 | */ |
||
190 | protected function registerTemplates() |
||
191 | { |
||
192 | Event::on( |
||
193 | View::class, |
||
194 | View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
||
195 | function (RegisterTemplateRootsEvent $e) { |
||
196 | if (is_dir($baseDir = $this->getTemplateRoot())) { |
||
197 | $e->roots[$this->getTemplateRootKey()] = $baseDir; |
||
198 | } |
||
199 | } |
||
200 | ); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getMyType() |
||
207 | { |
||
208 | return $this->getSettings()->getMyType(); |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getRemoteType() |
||
215 | { |
||
216 | $type = SettingsInterface::SP; |
||
217 | if ($this->getMyType() === SettingsInterface::SP) { |
||
218 | $type = SettingsInterface::IDP; |
||
219 | } |
||
220 | |||
221 | return $type; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @return string |
||
226 | */ |
||
227 | public function getTemplateRoot() |
||
231 | |||
232 | /** |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getTemplateRootKey() |
||
239 | /** |
||
240 | * EVENTs |
||
241 | */ |
||
242 | |||
243 | /** |
||
244 | * @param RegisterUrlRulesEvent $event |
||
245 | */ |
||
246 | public static function onRegisterCpUrlRules(RegisterUrlRulesEvent $event) |
||
275 | |||
276 | /** |
||
277 | * @param RegisterUrlRulesEvent $event |
||
278 | */ |
||
279 | public static function onRegisterSiteUrlRules(RegisterUrlRulesEvent $event) |
||
319 | |||
320 | /** |
||
321 | * @return AbstractSettings |
||
322 | */ |
||
323 | public function getSettings() |
||
327 | |||
328 | /** |
||
329 | * Components |
||
330 | */ |
||
331 | |||
332 | /** |
||
333 | * @noinspection PhpDocMissingThrowsInspection |
||
334 | * @returns AbstractCp |
||
335 | */ |
||
336 | public function getCp() |
||
343 | |||
344 | /** |
||
345 | * @noinspection PhpDocMissingThrowsInspection |
||
346 | * @returns ProviderServiceInterface |
||
347 | */ |
||
348 | public function getProvider() |
||
349 | { |
||
350 | |||
351 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
352 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
353 | return $this->get('provider'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * @returns ProviderIdentityServiceInterface |
||
358 | */ |
||
359 | public function getProviderIdentity() |
||
360 | { |
||
361 | return $this->get('providerIdentity'); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @noinspection PhpDocMissingThrowsInspection |
||
366 | * @return Metadata |
||
367 | */ |
||
368 | public function getMetadata() |
||
369 | { |
||
370 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
371 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
372 | return $this->get('metadata'); |
||
373 | } |
||
374 | |||
375 | /** |
||
376 | * @noinspection PhpDocMissingThrowsInspection |
||
377 | * @return LogoutRequest |
||
378 | * @throws \yii\base\InvalidConfigException |
||
379 | */ |
||
380 | public function getLogoutRequest() |
||
381 | { |
||
382 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
383 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
384 | return $this->get('logoutRequest'); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @noinspection PhpDocMissingThrowsInspection |
||
389 | * @return LogoutResponse |
||
390 | * @throws \yii\base\InvalidConfigException |
||
391 | */ |
||
392 | public function getLogoutResponse() |
||
393 | { |
||
394 | /** @noinspection PhpUnhandledExceptionInspection */ |
||
395 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
396 | return $this->get('logoutResponse'); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Bindings |
||
401 | */ |
||
402 | |||
403 | /** |
||
404 | * @return Factory |
||
405 | * @throws \yii\base\InvalidConfigException |
||
406 | */ |
||
407 | public function getBindingFactory() |
||
413 | |||
414 | /** |
||
415 | * @return Logger |
||
416 | * @throws \yii\base\InvalidConfigException |
||
417 | */ |
||
418 | public function getPsr3Logger() |
||
424 | |||
425 | |||
426 | /** |
||
427 | * Log Functions |
||
428 | */ |
||
429 | |||
430 | /** |
||
431 | * @param $message |
||
432 | */ |
||
433 | public static function error($message) |
||
437 | |||
438 | /** |
||
439 | * @param $message |
||
440 | */ |
||
441 | public static function warning($message) |
||
445 | |||
446 | /** |
||
447 | * @param $message |
||
448 | */ |
||
449 | public static function info($message) |
||
453 | |||
454 | /** |
||
455 | * @param $message |
||
456 | */ |
||
457 | public static function debug($message) |
||
461 | } |
||
462 |