1 | <?php |
||
11 | class Checker |
||
12 | { |
||
13 | /** |
||
14 | * @var AuthenticationService |
||
15 | */ |
||
16 | protected $authService; |
||
17 | |||
18 | public function __construct(AuthenticationService $authService) |
||
22 | |||
23 | public function __invoke(MvcEvent $event) |
||
24 | { |
||
25 | if ($event->getRequest() instanceof ConsoleRequest) { |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | $match = $event->getRouteMatch(); |
||
30 | |||
31 | // No route match, this is a 404 |
||
32 | if (!$match instanceof RouteMatch) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | /** @var Application $app */ |
||
37 | $app = $event->getParam('application'); |
||
38 | $config = $app->getConfig(); |
||
39 | |||
40 | $disableForAuthorizedCallback = $config['authorized-redirect-to-route']; |
||
41 | $redirectTo = $disableForAuthorizedCallback($match, $this->authService); |
||
42 | if ($this->authService->hasIdentity() && !empty($redirectTo)) { |
||
43 | $response = $this->redirectTo($event, $redirectTo); |
||
44 | return $response; |
||
45 | } |
||
46 | |||
47 | $checkCallback = $config['need-authorization-callback']; |
||
48 | |||
49 | // if true = authorization needed |
||
50 | if (!$checkCallback($match, $this->authService)) { |
||
51 | return; |
||
52 | } |
||
53 | |||
54 | // User is authenticated |
||
55 | if ($this->authService->hasIdentity()) { |
||
56 | return; |
||
57 | } |
||
58 | |||
59 | $response = $this->redirectTo($event, $config['not-authorized-redirect-to-route']); |
||
60 | |||
61 | return $response; |
||
62 | } |
||
63 | |||
64 | private function redirectTo(MvcEvent $event, $routeName, $reasonPhrase = 'Unauthorized') |
||
78 | } |
||
79 |