1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Surfnet\StepupBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Surfnet\StepupBundle\Exception\InvalidArgumentException; |
7
|
|
|
use Symfony\Component\HttpFoundation\Cookie; |
8
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
10
|
|
|
|
11
|
|
|
final class LocaleCookieListener |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TokenStorageInterface |
15
|
|
|
*/ |
16
|
|
|
private $tokenStorage; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var LoggerInterface |
20
|
|
|
*/ |
21
|
|
|
private $logger; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $name; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $domain; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $expire; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $path; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var bool |
45
|
|
|
*/ |
46
|
|
|
private $secure; |
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
private $httpOnly; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $name |
54
|
|
|
* @param string $domain |
55
|
|
|
* @param bool $secure |
56
|
|
|
* @param bool $httpOnly |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
TokenStorageInterface $token, |
60
|
|
|
LoggerInterface $logger, |
61
|
|
|
$name, |
62
|
|
|
$domain, |
63
|
|
|
$expire, |
64
|
|
|
$path, |
65
|
|
|
$secure, |
66
|
|
|
$httpOnly |
67
|
|
|
) { |
68
|
|
|
$this->tokenStorage = $token; |
69
|
|
|
$this->logger = $logger; |
70
|
|
|
|
71
|
|
|
if (!is_string($name)) { |
72
|
|
|
throw InvalidArgumentException::invalidType('string', 'name', $name); |
73
|
|
|
} |
74
|
|
|
if (empty($name)) { |
75
|
|
|
throw new InvalidArgumentException('Empty name provided to ' . __CLASS__); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->name = $name; |
79
|
|
|
|
80
|
|
|
if (!is_string($domain)) { |
81
|
|
|
throw InvalidArgumentException::invalidType('string', 'domain', $domain); |
82
|
|
|
} |
83
|
|
|
if (empty($domain)) { |
84
|
|
|
throw new InvalidArgumentException(sprintf('Empty domain provided to ' . __CLASS__)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->domain = $domain; |
88
|
|
|
|
89
|
|
|
if (!is_integer($domain)) { |
90
|
|
|
throw InvalidArgumentException::invalidType('integer', 'expire', $expire); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$this->expire = $expire; |
94
|
|
|
|
95
|
|
|
if (!is_string($path)) { |
96
|
|
|
throw InvalidArgumentException::invalidType('string', 'path', $path); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$this->path = $path; |
100
|
|
|
|
101
|
|
|
if (!is_bool($secure)) { |
102
|
|
|
throw InvalidArgumentException::invalidType('bool', 'secure', $secure); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$this->secure = $secure; |
106
|
|
|
|
107
|
|
|
if (!is_bool($httpOnly)) { |
108
|
|
|
throw InvalidArgumentException::invalidType('bool', 'httpOnly', $httpOnly); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$this->httpOnly = $httpOnly; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* If there is a logged in user with a preferred language, set it as a cookie. |
116
|
|
|
* |
117
|
|
|
* @param FilterResponseEvent $event |
118
|
|
|
*/ |
119
|
|
|
public function onKernelResponse(FilterResponseEvent $event) |
120
|
|
|
{ |
121
|
|
|
$token = $this->tokenStorage->getToken(); |
122
|
|
|
|
123
|
|
|
// No token, no logged in user, nothing to do. |
124
|
|
|
if (!$token) { |
125
|
|
|
return; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$user = $token->getUser(); |
129
|
|
|
|
130
|
|
|
if (empty($user)) { |
131
|
|
|
$this->logger->warning('Unable to set locale cookie because we have a token without a user'); |
132
|
|
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if (!$user instanceof LocaleIdentity) { |
136
|
|
|
$this->logger->warning( |
137
|
|
|
'Unable to set locale cookie because we have a token with a user that cannot provide a locale' |
138
|
|
|
); |
139
|
|
|
return; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$event->getResponse()->headers->setCookie( |
143
|
|
|
new Cookie( |
144
|
|
|
$this->name, |
145
|
|
|
$user->getPreferredLocale(), |
146
|
|
|
$this->expire, |
147
|
|
|
$this->path, |
148
|
|
|
$this->domain, |
149
|
|
|
$this->secure, |
150
|
|
|
$this->httpOnly |
151
|
|
|
) |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|