1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupMiddleware\CommandHandlingBundle\Identity\Service; |
20
|
|
|
|
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Surfnet\Stepup\Identity\Value\CommonName; |
23
|
|
|
use Surfnet\Stepup\Identity\Value\Email; |
24
|
|
|
use Surfnet\Stepup\Identity\Value\Locale; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Entity\RaLocation; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\InvalidArgumentException; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
30
|
|
|
use Swift_Mailer as Mailer; |
31
|
|
|
use Swift_Message as Message; |
32
|
|
|
use Symfony\Component\Templating\EngineInterface; |
33
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
37
|
|
|
*/ |
38
|
|
|
final class SecondFactorMailService |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var Mailer |
42
|
|
|
*/ |
43
|
|
|
private $mailer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var Sender |
47
|
|
|
*/ |
48
|
|
|
private $sender; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var TranslatorInterface |
52
|
|
|
*/ |
53
|
|
|
private $translator; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var EngineInterface |
57
|
|
|
*/ |
58
|
|
|
private $templateEngine; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $emailVerificationUrlTemplate; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService |
67
|
|
|
*/ |
68
|
|
|
private $emailTemplateService; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $fallbackLocale; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var LoggerInterface |
77
|
|
|
*/ |
78
|
|
|
private $logger; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var bool |
82
|
|
|
*/ |
83
|
|
|
private $warnOnMissingTemplateConfiguration; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param Mailer $mailer |
87
|
|
|
* @param Sender $sender |
88
|
|
|
* @param TranslatorInterface $translator |
89
|
|
|
* @param EngineInterface $templateEngine |
90
|
|
|
* @param string $emailVerificationUrlTemplate |
91
|
|
|
* @param EmailTemplateService $emailTemplateService |
92
|
|
|
* @param string $fallbackLocale |
93
|
|
|
* @param LoggerInterface $logger |
94
|
|
|
* @param bool $warnOnMissingTemplateConfiguration |
95
|
|
|
*/ |
96
|
|
|
public function __construct( |
97
|
|
|
Mailer $mailer, |
98
|
|
|
Sender $sender, |
99
|
|
|
TranslatorInterface $translator, |
100
|
|
|
EngineInterface $templateEngine, |
101
|
|
|
$emailVerificationUrlTemplate, |
102
|
|
|
EmailTemplateService $emailTemplateService, |
103
|
|
|
$fallbackLocale, |
104
|
|
|
LoggerInterface $logger, |
105
|
|
|
$warnOnMissingTemplateConfiguration = true |
|
|
|
|
106
|
|
|
) { |
107
|
|
|
$this->mailer = $mailer; |
108
|
|
|
$this->sender = $sender; |
109
|
|
|
$this->translator = $translator; |
110
|
|
|
$this->templateEngine = $templateEngine; |
111
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
112
|
|
|
$this->emailTemplateService = $emailTemplateService; |
113
|
|
|
$this->fallbackLocale = $fallbackLocale; |
114
|
|
|
$this->logger = $logger; |
115
|
|
|
|
116
|
|
|
if (!is_bool($warnOnMissingTemplateConfiguration)) { |
117
|
|
|
throw new InvalidArgumentException('warnOnMissingTemplateConfiguration for SecondFactorMailService is not a boolean'); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$this->warnOnMissingTemplateConfiguration = $warnOnMissingTemplateConfiguration; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $locale |
125
|
|
|
* @param string $commonName |
126
|
|
|
* @param string $email |
127
|
|
|
* @param string $verificationNonce |
128
|
|
|
*/ |
129
|
|
|
public function sendEmailVerificationEmail( |
130
|
|
|
$locale, |
131
|
|
|
$commonName, |
132
|
|
|
$email, |
133
|
|
|
$verificationNonce |
134
|
|
|
) { |
135
|
|
|
$subject = $this->translator->trans( |
136
|
|
|
'ss.mail.email_verification_email.subject', |
137
|
|
|
['%commonName%' => $commonName], |
138
|
|
|
null, |
139
|
|
|
$locale |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
$verificationUrl = str_replace( |
143
|
|
|
'{nonce}', |
144
|
|
|
urlencode($verificationNonce), |
145
|
|
|
$this->emailVerificationUrlTemplate |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$emailTemplate = $this->findEmailTemplate('confirm_email', $locale, $this->fallbackLocale); |
149
|
|
|
if (!$emailTemplate) { |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$parameters = [ |
154
|
|
|
'templateString' => $emailTemplate->htmlContent, |
155
|
|
|
'locale' => $locale, |
156
|
|
|
'commonName' => $commonName, |
157
|
|
|
'email' => $email, |
158
|
|
|
'verificationUrl' => $verificationUrl |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
// Rendering file template instead of string |
162
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
163
|
|
|
$body = $this->templateEngine->render( |
164
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
165
|
|
|
$parameters |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
/** @var Message $message */ |
169
|
|
|
$message = $this->mailer->createMessage(); |
170
|
|
|
$message |
171
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
172
|
|
|
->addTo($email, $commonName) |
173
|
|
|
->setSubject($subject) |
174
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
175
|
|
|
|
176
|
|
|
$this->mailer->send($message); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $locale |
181
|
|
|
* @param string $commonName |
182
|
|
|
* @param string $email |
183
|
|
|
* @param string $registrationCode |
184
|
|
|
* @param RegistrationAuthorityCredentials[] $ras |
185
|
|
|
*/ |
186
|
|
View Code Duplication |
public function sendRegistrationEmailWithRas( |
|
|
|
|
187
|
|
|
$locale, |
188
|
|
|
$commonName, |
189
|
|
|
$email, |
190
|
|
|
$registrationCode, |
191
|
|
|
array $ras |
192
|
|
|
) { |
193
|
|
|
$subject = $this->translator->trans( |
194
|
|
|
'ss.mail.registration_email.subject', |
195
|
|
|
['%commonName%' => $commonName], |
196
|
|
|
null, |
197
|
|
|
$locale |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$emailTemplate = $this->findEmailTemplate('registration_code_with_ras', $locale, $this->fallbackLocale); |
201
|
|
|
if (!$emailTemplate) { |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$parameters = [ |
206
|
|
|
'templateString' => $emailTemplate->htmlContent, |
207
|
|
|
'locale' => $locale, |
208
|
|
|
'commonName' => $commonName, |
209
|
|
|
'email' => $email, |
210
|
|
|
'registrationCode' => $registrationCode, |
211
|
|
|
'ras' => $ras, |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
// Rendering file template instead of string |
215
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
216
|
|
|
$body = $this->templateEngine->render( |
217
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
218
|
|
|
$parameters |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
/** @var Message $message */ |
222
|
|
|
$message = $this->mailer->createMessage(); |
223
|
|
|
$message |
224
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
225
|
|
|
->addTo($email, $commonName) |
226
|
|
|
->setSubject($subject) |
227
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
228
|
|
|
|
229
|
|
|
$this->mailer->send($message); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param string $locale |
234
|
|
|
* @param string $commonName |
235
|
|
|
* @param string $email |
236
|
|
|
* @param string $registrationCode |
237
|
|
|
* @param RaLocation[] $raLocations |
238
|
|
|
*/ |
239
|
|
View Code Duplication |
public function sendRegistrationEmailWithRaLocations( |
|
|
|
|
240
|
|
|
$locale, |
241
|
|
|
$commonName, |
242
|
|
|
$email, |
243
|
|
|
$registrationCode, |
244
|
|
|
array $raLocations |
245
|
|
|
) { |
246
|
|
|
$subject = $this->translator->trans( |
247
|
|
|
'ss.mail.registration_email.subject', |
248
|
|
|
['%commonName%' => $commonName], |
249
|
|
|
null, |
250
|
|
|
$locale |
251
|
|
|
); |
252
|
|
|
|
253
|
|
|
$emailTemplate = $this->findEmailTemplate('registration_code_with_ra_locations', $locale, $this->fallbackLocale); |
|
|
|
|
254
|
|
|
if (!$emailTemplate) { |
255
|
|
|
return; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
$parameters = [ |
259
|
|
|
'templateString' => $emailTemplate->htmlContent, |
260
|
|
|
'locale' => $locale, |
261
|
|
|
'commonName' => $commonName, |
262
|
|
|
'email' => $email, |
263
|
|
|
'registrationCode' => $registrationCode, |
264
|
|
|
'raLocations' => $raLocations, |
265
|
|
|
]; |
266
|
|
|
|
267
|
|
|
// Rendering file template instead of string |
268
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
269
|
|
|
$body = $this->templateEngine->render( |
270
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
271
|
|
|
$parameters |
272
|
|
|
); |
273
|
|
|
|
274
|
|
|
/** @var Message $message */ |
275
|
|
|
$message = $this->mailer->createMessage(); |
276
|
|
|
$message |
277
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
278
|
|
|
->addTo($email, $commonName) |
279
|
|
|
->setSubject($subject) |
280
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
281
|
|
|
|
282
|
|
|
$this->mailer->send($message); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @param Locale $locale |
287
|
|
|
* @param CommonName $commonName |
288
|
|
|
* @param Email $email |
289
|
|
|
*/ |
290
|
|
|
public function sendVettedEmail( |
291
|
|
|
Locale $locale, |
292
|
|
|
CommonName $commonName, |
293
|
|
|
Email $email |
294
|
|
|
) { |
295
|
|
|
$subject = $this->translator->trans( |
296
|
|
|
'ss.mail.vetted_email.subject', |
297
|
|
|
['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()], |
298
|
|
|
null, |
299
|
|
|
$locale->getLocale() |
300
|
|
|
); |
301
|
|
|
|
302
|
|
|
$emailTemplate = $this->findEmailTemplate('vetted', $locale->getLocale(), $this->fallbackLocale); |
303
|
|
|
if (!$emailTemplate) { |
304
|
|
|
return; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
$parameters = [ |
308
|
|
|
'templateString' => $emailTemplate->htmlContent, |
309
|
|
|
'locale' => $locale->getLocale(), |
310
|
|
|
'commonName' => $commonName->getCommonName(), |
311
|
|
|
'email' => $email->getEmail(), |
312
|
|
|
]; |
313
|
|
|
|
314
|
|
|
// Rendering file template instead of string |
315
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
316
|
|
|
$body = $this->templateEngine->render( |
317
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
318
|
|
|
$parameters |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
/** @var Message $message */ |
322
|
|
|
$message = $this->mailer->createMessage(); |
323
|
|
|
$message |
324
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
325
|
|
|
->addTo($email->getEmail(), $commonName->getCommonName()) |
326
|
|
|
->setSubject($subject) |
327
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
328
|
|
|
|
329
|
|
|
$this->mailer->send($message); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @param string $name |
334
|
|
|
* @param string $locale |
335
|
|
|
* @param string $fallbackLocale |
336
|
|
|
* @return null|\Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Dto\EmailTemplate |
337
|
|
|
*/ |
338
|
|
|
private function findEmailTemplate($name, $locale, $fallbackLocale) |
339
|
|
|
{ |
340
|
|
|
$emailTemplate = $this->emailTemplateService->findByName($name, $locale, $this->fallbackLocale); |
341
|
|
|
|
342
|
|
|
if ($emailTemplate) { |
343
|
|
|
return $emailTemplate; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
if ($this->warnOnMissingTemplateConfiguration) { |
347
|
|
|
$this->logger->warning( |
348
|
|
|
sprintf( |
349
|
|
|
'Skipping sending mail because template configuration for "%s" in locale "%s" or "%s" is missing', |
350
|
|
|
$name, |
351
|
|
|
$locale, |
352
|
|
|
$fallbackLocale |
353
|
|
|
) |
354
|
|
|
); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
return null; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.