1 | <?php |
||
18 | class EmailUpdateConfirmation implements EmailUpdateConfirmationInterface |
||
19 | { |
||
20 | const EMAIL_CONFIRMED = 'email_confirmed'; |
||
21 | |||
22 | /** |
||
23 | * @var EmailUpdateConfirmationMailerInterface |
||
24 | */ |
||
25 | private $mailer; |
||
26 | |||
27 | /** |
||
28 | * @var Router |
||
29 | */ |
||
30 | private $router; |
||
31 | |||
32 | /** |
||
33 | * @var TokenGenerator |
||
34 | */ |
||
35 | private $tokenGenerator; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $encryptionMode; |
||
41 | |||
42 | /** |
||
43 | * @var ValidatorInterface |
||
44 | */ |
||
45 | private $validator; |
||
46 | |||
47 | /** |
||
48 | * @var string Route for confirmation link |
||
49 | */ |
||
50 | private $confirmationRoute = 'user_update_email_confirm'; |
||
51 | |||
52 | /** |
||
53 | * @var EventDispatcherInterface |
||
54 | */ |
||
55 | private $eventDispatcher; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $redirectRoute; |
||
61 | |||
62 | 6 | public function __construct( |
|
63 | Router $router, |
||
64 | TokenGenerator $tokenGenerator, |
||
65 | EmailUpdateConfirmationMailerInterface $mailer, |
||
66 | EventDispatcherInterface $eventDispatcher, |
||
67 | ValidatorInterface $validator, |
||
68 | $redirectRoute, |
||
69 | $mode = null |
||
70 | ) { |
||
71 | 6 | $this->router = $router; |
|
72 | 6 | $this->tokenGenerator = $tokenGenerator; |
|
73 | 6 | $this->mailer = $mailer; |
|
74 | 6 | $this->eventDispatcher = $eventDispatcher; |
|
75 | 6 | $this->validator = $validator; |
|
76 | 6 | $this->redirectRoute = $redirectRoute; |
|
77 | |||
78 | 6 | if (!$mode) { |
|
79 | $mode = openssl_get_cipher_methods(false)[0]; |
||
80 | } |
||
81 | 6 | $this->encryptionMode = $mode; |
|
82 | 6 | } |
|
83 | |||
84 | /** |
||
85 | * Get $mailer. |
||
86 | * |
||
87 | * @return MailerInterface |
||
88 | */ |
||
89 | public function getMailer() |
||
90 | { |
||
91 | return $this->mailer; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Generate new confirmation link for new email based on user confirmation |
||
96 | * token and hashed new user email. |
||
97 | * |
||
98 | * @param Request $request |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function generateConfirmationLink(Request $request, UserInterface $user, $email) |
||
103 | { |
||
104 | if (!$user->getConfirmationToken()) { |
||
|
|||
105 | $user->setConfirmationToken( |
||
106 | $this->tokenGenerator->generateToken() |
||
107 | ); |
||
108 | } |
||
109 | |||
110 | $encryptedEmail = $this->encryptEmailValue($user->getConfirmationToken(), $email); |
||
111 | |||
112 | $confirmationParams = array('token' => $user->getConfirmationToken(), 'target' => $encryptedEmail, 'redirectRoute' => $this->redirectRoute); |
||
113 | |||
114 | $event = new UserEvent($user, $request); |
||
115 | |||
116 | $this->eventDispatcher->dispatch(AzineEmailUpdateConfirmationEvents::EMAIL_UPDATE_INITIALIZE, $event); |
||
117 | |||
118 | return $this->router->generate( |
||
119 | $this->confirmationRoute, |
||
120 | $confirmationParams, |
||
121 | UrlGeneratorInterface::ABSOLUTE_URL |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Fetch email value from hashed part of confirmation link. |
||
127 | * |
||
128 | * @param UserInterface $user |
||
129 | * @param string $hashedEmail |
||
130 | * |
||
131 | * @return string Encrypted email |
||
132 | */ |
||
133 | 1 | public function fetchEncryptedEmailFromConfirmationLink(UserInterface $user, $hashedEmail) |
|
142 | |||
143 | /** |
||
144 | * Get token which indicates that email was confirmed. |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function getEmailConfirmedToken() |
||
152 | |||
153 | /** |
||
154 | * Return IV size. |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | 3 | protected function getIvSize() |
|
162 | |||
163 | /** |
||
164 | * Encrypt email value with specified user confirmation token. |
||
165 | * |
||
166 | * @param string $userConfirmationToken |
||
167 | * @param string $email |
||
168 | * |
||
169 | * @return string Encrypted email |
||
170 | */ |
||
171 | 5 | public function encryptEmailValue($userConfirmationToken, $email) |
|
200 | |||
201 | /** |
||
202 | * Decrypt email value with specified user confirmation token. |
||
203 | * |
||
204 | * @param string $userConfirmationToken |
||
205 | * @param string $encryptedEmail |
||
206 | * |
||
207 | * @return string Decrypted email |
||
208 | */ |
||
209 | 4 | public function decryptEmailValue($userConfirmationToken, $encryptedEmail) |
|
245 | } |
||
246 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: