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\Identity\Value\RegistrationAuthorityCredentials; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
27
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Exception\InvalidArgumentException; |
28
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
29
|
|
|
use Swift_Mailer as Mailer; |
30
|
|
|
use Swift_Message as Message; |
31
|
|
|
use Symfony\Component\Templating\EngineInterface; |
32
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
36
|
|
|
*/ |
37
|
|
|
final class SecondFactorMailService |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var Mailer |
41
|
|
|
*/ |
42
|
|
|
private $mailer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var Sender |
46
|
|
|
*/ |
47
|
|
|
private $sender; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var TranslatorInterface |
51
|
|
|
*/ |
52
|
|
|
private $translator; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var EngineInterface |
56
|
|
|
*/ |
57
|
|
|
private $templateEngine; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $emailVerificationUrlTemplate; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService |
66
|
|
|
*/ |
67
|
|
|
private $emailTemplateService; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $fallbackLocale; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var LoggerInterface |
76
|
|
|
*/ |
77
|
|
|
private $logger; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
private $warnOnMissingTemplateConfiguration; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Mailer $mailer |
86
|
|
|
* @param Sender $sender |
87
|
|
|
* @param TranslatorInterface $translator |
88
|
|
|
* @param EngineInterface $templateEngine |
89
|
|
|
* @param string $emailVerificationUrlTemplate |
90
|
|
|
* @param EmailTemplateService $emailTemplateService |
91
|
|
|
* @param string $fallbackLocale |
92
|
|
|
* @param LoggerInterface $logger |
93
|
|
|
* @param bool $warnOnMissingTemplateConfiguration |
94
|
|
|
*/ |
95
|
|
|
public function __construct( |
96
|
|
|
Mailer $mailer, |
97
|
|
|
Sender $sender, |
98
|
|
|
TranslatorInterface $translator, |
99
|
|
|
EngineInterface $templateEngine, |
100
|
|
|
$emailVerificationUrlTemplate, |
101
|
|
|
EmailTemplateService $emailTemplateService, |
102
|
|
|
$fallbackLocale, |
103
|
|
|
LoggerInterface $logger, |
104
|
|
|
$warnOnMissingTemplateConfiguration = true |
|
|
|
|
105
|
|
|
) { |
106
|
|
|
$this->mailer = $mailer; |
107
|
|
|
$this->sender = $sender; |
108
|
|
|
$this->translator = $translator; |
109
|
|
|
$this->templateEngine = $templateEngine; |
110
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
111
|
|
|
$this->emailTemplateService = $emailTemplateService; |
112
|
|
|
$this->fallbackLocale = $fallbackLocale; |
113
|
|
|
$this->logger = $logger; |
114
|
|
|
|
115
|
|
|
if (!is_bool($warnOnMissingTemplateConfiguration)) { |
116
|
|
|
throw new InvalidArgumentException('warnOnMissingTemplateConfiguration for SecondFactorMailService is not a boolean'); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->warnOnMissingTemplateConfiguration = $warnOnMissingTemplateConfiguration; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param string $locale |
124
|
|
|
* @param string $commonName |
125
|
|
|
* @param string $email |
126
|
|
|
* @param string $verificationNonce |
127
|
|
|
*/ |
128
|
|
|
public function sendEmailVerificationEmail( |
129
|
|
|
$locale, |
130
|
|
|
$commonName, |
131
|
|
|
$email, |
132
|
|
|
$verificationNonce |
133
|
|
|
) { |
134
|
|
|
$subject = $this->translator->trans( |
135
|
|
|
'ss.mail.email_verification_email.subject', |
136
|
|
|
['%commonName%' => $commonName], |
137
|
|
|
null, |
138
|
|
|
$locale |
139
|
|
|
); |
140
|
|
|
|
141
|
|
|
$verificationUrl = str_replace( |
142
|
|
|
'{nonce}', |
143
|
|
|
urlencode($verificationNonce), |
144
|
|
|
$this->emailVerificationUrlTemplate |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$emailTemplate = $this->findEmailTemplate('confirm_email', $locale, $this->fallbackLocale); |
148
|
|
|
if (!$emailTemplate) { |
149
|
|
|
return; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$parameters = [ |
153
|
|
|
'templateString' => $emailTemplate->htmlContent, |
154
|
|
|
'locale' => $locale, |
155
|
|
|
'commonName' => $commonName, |
156
|
|
|
'email' => $email, |
157
|
|
|
'verificationUrl' => $verificationUrl |
158
|
|
|
]; |
159
|
|
|
|
160
|
|
|
// Rendering file template instead of string |
161
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
162
|
|
|
$body = $this->templateEngine->render( |
163
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
164
|
|
|
$parameters |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
/** @var Message $message */ |
168
|
|
|
$message = $this->mailer->createMessage(); |
169
|
|
|
$message |
170
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
171
|
|
|
->addTo($email, $commonName) |
172
|
|
|
->setSubject($subject) |
173
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
174
|
|
|
|
175
|
|
|
$this->mailer->send($message); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param string $locale |
180
|
|
|
* @param string $commonName |
181
|
|
|
* @param string $email |
182
|
|
|
* @param string $registrationCode |
183
|
|
|
* @param RegistrationAuthorityCredentials[] $ras |
184
|
|
|
*/ |
185
|
|
|
public function sendRegistrationEmail( |
186
|
|
|
$locale, |
187
|
|
|
$commonName, |
188
|
|
|
$email, |
189
|
|
|
$registrationCode, |
190
|
|
|
array $ras |
191
|
|
|
) { |
192
|
|
|
$subject = $this->translator->trans( |
193
|
|
|
'ss.mail.registration_email.subject', |
194
|
|
|
['%commonName%' => $commonName], |
195
|
|
|
null, |
196
|
|
|
$locale |
197
|
|
|
); |
198
|
|
|
|
199
|
|
|
$emailTemplate = $this->findEmailTemplate('registration_code', $locale, $this->fallbackLocale); |
200
|
|
|
if (!$emailTemplate) { |
201
|
|
|
return; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$parameters = [ |
205
|
|
|
'templateString' => $emailTemplate->htmlContent, |
206
|
|
|
'locale' => $locale, |
207
|
|
|
'commonName' => $commonName, |
208
|
|
|
'email' => $email, |
209
|
|
|
'registrationCode' => $registrationCode, |
210
|
|
|
'ras' => $ras, |
211
|
|
|
]; |
212
|
|
|
|
213
|
|
|
// Rendering file template instead of string |
214
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
215
|
|
|
$body = $this->templateEngine->render( |
216
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
217
|
|
|
$parameters |
218
|
|
|
); |
219
|
|
|
|
220
|
|
|
/** @var Message $message */ |
221
|
|
|
$message = $this->mailer->createMessage(); |
222
|
|
|
$message |
223
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
224
|
|
|
->addTo($email, $commonName) |
225
|
|
|
->setSubject($subject) |
226
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
227
|
|
|
|
228
|
|
|
$this->mailer->send($message); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param Locale $locale |
233
|
|
|
* @param CommonName $commonName |
234
|
|
|
* @param Email $email |
235
|
|
|
*/ |
236
|
|
|
public function sendVettedEmail( |
237
|
|
|
Locale $locale, |
238
|
|
|
CommonName $commonName, |
239
|
|
|
Email $email |
240
|
|
|
) { |
241
|
|
|
$subject = $this->translator->trans( |
242
|
|
|
'ss.mail.vetted_email.subject', |
243
|
|
|
['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()], |
244
|
|
|
null, |
245
|
|
|
$locale->getLocale() |
246
|
|
|
); |
247
|
|
|
|
248
|
|
|
$emailTemplate = $this->findEmailTemplate('vetted', $locale->getLocale(), $this->fallbackLocale); |
249
|
|
|
if (!$emailTemplate) { |
250
|
|
|
return; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$parameters = [ |
254
|
|
|
'templateString' => $emailTemplate->htmlContent, |
255
|
|
|
'locale' => $locale->getLocale(), |
256
|
|
|
'commonName' => $commonName->getCommonName(), |
257
|
|
|
'email' => $email->getEmail(), |
258
|
|
|
]; |
259
|
|
|
|
260
|
|
|
// Rendering file template instead of string |
261
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
262
|
|
|
$body = $this->templateEngine->render( |
263
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
264
|
|
|
$parameters |
265
|
|
|
); |
266
|
|
|
|
267
|
|
|
/** @var Message $message */ |
268
|
|
|
$message = $this->mailer->createMessage(); |
269
|
|
|
$message |
270
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
271
|
|
|
->addTo($email->getEmail(), $commonName->getCommonName()) |
272
|
|
|
->setSubject($subject) |
273
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
274
|
|
|
|
275
|
|
|
$this->mailer->send($message); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @param string $name |
280
|
|
|
* @param string $locale |
281
|
|
|
* @param string $fallbackLocale |
282
|
|
|
* @return null|\Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Dto\EmailTemplate |
283
|
|
|
*/ |
284
|
|
|
private function findEmailTemplate($name, $locale, $fallbackLocale) |
285
|
|
|
{ |
286
|
|
|
$emailTemplate = $this->emailTemplateService->findByName($name, $locale, $this->fallbackLocale); |
287
|
|
|
|
288
|
|
|
if ($emailTemplate) { |
289
|
|
|
return $emailTemplate; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
if ($this->warnOnMissingTemplateConfiguration) { |
293
|
|
|
$this->logger->warning( |
294
|
|
|
sprintf( |
295
|
|
|
'Skipping sending mail because template configuration for "%s" in locale "%s" or "%s" is missing', |
296
|
|
|
[$name, $locale, $fallbackLocale] |
297
|
|
|
) |
298
|
|
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
return null; |
302
|
|
|
} |
303
|
|
|
} |
304
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.