-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathModule.php
62 lines (54 loc) · 2.04 KB
/
Module.php
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
<?php
declare(strict_types=1);
namespace User;
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Storage\NonPersistent as NonPersistentStorage;
use Laminas\Authentication\Storage\Session as SessionStorage;
use Laminas\Mvc\MvcEvent;
use User\Adapter\ApiPrincipalAdapter;
use User\Listener\AuthenticationListener;
use User\Listener\AuthorizationListener;
use User\Listener\DispatchErrorFormatterListener;
use User\Service\ApiAuthenticationService;
class Module
{
/**
* On bootstrap.
*/
public function onBootstrap(MvcEvent $event): void
{
$sm = $event->getApplication()->getServiceManager();
$eventManager = $event->getApplication()->getEventManager();
$authService = $sm->get(AuthenticationService::class);
$authService->setStorage(new SessionStorage('gewisdb'));
$apiAuthService = $sm->get(ApiAuthenticationService::class);
$apiAuthService->setStorage(new NonPersistentStorage());
$apiPrincipalAdapter = $sm->get(ApiPrincipalAdapter::class);
/**
* Establish an identity of the user using the authentication listener
*/
$authenticationListener = new AuthenticationListener(
$authService,
$apiAuthService,
$apiPrincipalAdapter,
);
$eventManager->attach(MvcEvent::EVENT_ROUTE, $authenticationListener, -100);
/**
* Catch authorization exceptions
*/
$authorizationListener = new AuthorizationListener();
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, $authorizationListener, 10);
/**
* Format errors in case of dispatch errors after authentication
*/
$dispatchErrorFormatterListener = new DispatchErrorFormatterListener();
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, $dispatchErrorFormatterListener, 5);
}
/**
* Get the configuration for this module.
*/
public function getConfig(): array
{
return include __DIR__ . '/../config/module.config.php';
}
}