1 | <?php |
||
11 | class Checker |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var AuthenticationService |
||
16 | */ |
||
17 | protected $authService; |
||
18 | |||
19 | public function __construct(AuthenticationService $authService) |
||
23 | |||
24 | public function __invoke(MvcEvent $event) |
||
25 | { |
||
26 | if ($event->getRequest() instanceof ConsoleRequest) { |
||
27 | return; |
||
28 | } |
||
29 | |||
30 | $match = $event->getRouteMatch(); |
||
31 | |||
32 | // No route match, this is a 404 |
||
33 | if (!$match instanceof RouteMatch) { |
||
34 | return; |
||
35 | } |
||
36 | |||
37 | if ($match->getMatchedRouteName() == 'auth-login' && $this->authService->hasIdentity()) { |
||
38 | $response = $this->redirectTo($event, 'home'); |
||
39 | |||
40 | return $response; |
||
41 | } |
||
42 | |||
43 | /** @var Application $app */ |
||
44 | $app = $event->getParam('application'); |
||
45 | $config = $app->getConfig(); |
||
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, 'auth-login'); |
||
60 | |||
61 | return $response; |
||
62 | } |
||
63 | |||
64 | private function redirectTo(MvcEvent $event, $routeName, $reasonPhrase = 'Unauthorized') |
||
77 | } |
||
78 |