|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Surfnet\StepupRa\RaBundle\EventListener; |
|
4
|
|
|
|
|
5
|
|
|
use Surfnet\StepupMiddlewareClientBundle\Identity\Dto\Identity; |
|
6
|
|
|
use Surfnet\StepupRa\RaBundle\Security\Authentication\Token\SamlToken; |
|
7
|
|
|
use Surfnet\StepupRa\RaBundle\Service\InstitutionWithPersonalRaLocationsService; |
|
8
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
10
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
11
|
|
|
use Symfony\Component\Security\Core\SecurityContext; |
|
12
|
|
|
use Twig_Environment; |
|
13
|
|
|
|
|
14
|
|
|
final class HasPersonalRaLocationsListener implements EventSubscriberInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Twig_Environment |
|
18
|
|
|
*/ |
|
19
|
|
|
private $twig; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var InstitutionWithPersonalRaLocationsService |
|
23
|
|
|
*/ |
|
24
|
|
|
private $service; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var SecurityContext |
|
28
|
|
|
*/ |
|
29
|
|
|
private $securityContext; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct( |
|
32
|
|
|
Twig_Environment $twig, |
|
33
|
|
|
InstitutionWithPersonalRaLocationsService $institutionWithPersonalRaLocationsService, |
|
|
|
|
|
|
34
|
|
|
SecurityContext $securityContext |
|
35
|
|
|
) { |
|
36
|
|
|
$this->twig = $twig; |
|
37
|
|
|
$this->service = $institutionWithPersonalRaLocationsService; |
|
38
|
|
|
$this->securityContext = $securityContext; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
$token = $this->securityContext->getToken(); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if (!$token instanceof SamlToken) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$user = $token->getUser(); |
|
50
|
|
|
|
|
51
|
|
|
if (!$user instanceof Identity) { |
|
52
|
|
|
return; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$hasPersonalRaLocations = $this->service->institutionHasPersonalRaLocations($user->institution); |
|
56
|
|
|
$this->twig->addGlobal('hasPersonalRaLocations', $hasPersonalRaLocations); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function getSubscribedEvents() |
|
60
|
|
|
{ |
|
61
|
|
|
return [ |
|
62
|
|
|
// The firewall, which makes the token available, listens at P8 |
|
63
|
|
|
KernelEvents::REQUEST => ['onKernelRequest', 0], |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.