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\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
|
|
|
* @param Mailer $mailer |
81
|
|
|
* @param Sender $sender |
82
|
|
|
* @param TranslatorInterface $translator |
83
|
|
|
* @param EngineInterface $templateEngine |
84
|
|
|
* @param string $emailVerificationUrlTemplate |
85
|
|
|
* @param EmailTemplateService $emailTemplateService |
86
|
|
|
* @param string $fallbackLocale |
87
|
|
|
* @param LoggerInterface $logger |
88
|
|
|
*/ |
89
|
|
|
public function __construct( |
90
|
|
|
Mailer $mailer, |
91
|
|
|
Sender $sender, |
92
|
|
|
TranslatorInterface $translator, |
93
|
|
|
EngineInterface $templateEngine, |
94
|
|
|
$emailVerificationUrlTemplate, |
95
|
|
|
EmailTemplateService $emailTemplateService, |
96
|
|
|
$fallbackLocale, |
97
|
|
|
LoggerInterface $logger |
98
|
|
|
) { |
99
|
|
|
$this->mailer = $mailer; |
100
|
|
|
$this->sender = $sender; |
101
|
|
|
$this->translator = $translator; |
102
|
|
|
$this->templateEngine = $templateEngine; |
103
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
104
|
|
|
$this->emailTemplateService = $emailTemplateService; |
105
|
|
|
$this->fallbackLocale = $fallbackLocale; |
106
|
|
|
$this->logger = $logger; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $locale |
111
|
|
|
* @param string $commonName |
112
|
|
|
* @param string $email |
113
|
|
|
* @param string $verificationNonce |
114
|
|
|
*/ |
115
|
|
|
public function sendEmailVerificationEmail( |
116
|
|
|
$locale, |
117
|
|
|
$commonName, |
118
|
|
|
$email, |
119
|
|
|
$verificationNonce |
120
|
|
|
) { |
121
|
|
|
$subject = $this->translator->trans( |
122
|
|
|
'ss.mail.email_verification_email.subject', |
123
|
|
|
['%commonName%' => $commonName], |
124
|
|
|
null, |
125
|
|
|
$locale |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$verificationUrl = str_replace( |
129
|
|
|
'{nonce}', |
130
|
|
|
urlencode($verificationNonce), |
131
|
|
|
$this->emailVerificationUrlTemplate |
132
|
|
|
); |
133
|
|
|
$emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale); |
134
|
|
|
|
135
|
|
|
$parameters = [ |
136
|
|
|
'templateString' => $emailTemplate->htmlContent, |
137
|
|
|
'locale' => $locale, |
138
|
|
|
'commonName' => $commonName, |
139
|
|
|
'email' => $email, |
140
|
|
|
'verificationUrl' => $verificationUrl |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
// Rendering file template instead of string |
144
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
145
|
|
|
$body = $this->templateEngine->render( |
146
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
147
|
|
|
$parameters |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
/** @var Message $message */ |
151
|
|
|
$message = $this->mailer->createMessage(); |
152
|
|
|
$message |
153
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
154
|
|
|
->addTo($email, $commonName) |
155
|
|
|
->setSubject($subject) |
156
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
157
|
|
|
|
158
|
|
|
$this->mailer->send($message); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $locale |
163
|
|
|
* @param string $commonName |
164
|
|
|
* @param string $email |
165
|
|
|
* @param string $registrationCode |
166
|
|
|
* @param RegistrationAuthorityCredentials[] $ras |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function sendRegistrationEmailWithRas( |
|
|
|
|
169
|
|
|
$locale, |
170
|
|
|
$commonName, |
171
|
|
|
$email, |
172
|
|
|
$registrationCode, |
173
|
|
|
array $ras |
174
|
|
|
) { |
175
|
|
|
$subject = $this->translator->trans( |
176
|
|
|
'ss.mail.registration_email.subject', |
177
|
|
|
['%commonName%' => $commonName], |
178
|
|
|
null, |
179
|
|
|
$locale |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
183
|
|
|
'registration_code_with_ras', |
184
|
|
|
$locale, |
185
|
|
|
$this->fallbackLocale |
186
|
|
|
); |
187
|
|
|
|
188
|
|
|
$parameters = [ |
189
|
|
|
'templateString' => $emailTemplate->htmlContent, |
190
|
|
|
'locale' => $locale, |
191
|
|
|
'commonName' => $commonName, |
192
|
|
|
'email' => $email, |
193
|
|
|
'registrationCode' => $registrationCode, |
194
|
|
|
'ras' => $ras, |
195
|
|
|
]; |
196
|
|
|
|
197
|
|
|
// Rendering file template instead of string |
198
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
199
|
|
|
$body = $this->templateEngine->render( |
200
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
201
|
|
|
$parameters |
202
|
|
|
); |
203
|
|
|
|
204
|
|
|
/** @var Message $message */ |
205
|
|
|
$message = $this->mailer->createMessage(); |
206
|
|
|
$message |
207
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
208
|
|
|
->addTo($email, $commonName) |
209
|
|
|
->setSubject($subject) |
210
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
211
|
|
|
|
212
|
|
|
$this->mailer->send($message); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param string $locale |
217
|
|
|
* @param string $commonName |
218
|
|
|
* @param string $email |
219
|
|
|
* @param string $registrationCode |
220
|
|
|
* @param RaLocation[] $raLocations |
221
|
|
|
*/ |
222
|
|
View Code Duplication |
public function sendRegistrationEmailWithRaLocations( |
|
|
|
|
223
|
|
|
$locale, |
224
|
|
|
$commonName, |
225
|
|
|
$email, |
226
|
|
|
$registrationCode, |
227
|
|
|
array $raLocations |
228
|
|
|
) { |
229
|
|
|
$subject = $this->translator->trans( |
230
|
|
|
'ss.mail.registration_email.subject', |
231
|
|
|
['%commonName%' => $commonName], |
232
|
|
|
null, |
233
|
|
|
$locale |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
237
|
|
|
'registration_code_with_ra_locations', |
238
|
|
|
$locale, |
239
|
|
|
$this->fallbackLocale |
240
|
|
|
); |
241
|
|
|
|
242
|
|
|
$parameters = [ |
243
|
|
|
'templateString' => $emailTemplate->htmlContent, |
244
|
|
|
'locale' => $locale, |
245
|
|
|
'commonName' => $commonName, |
246
|
|
|
'email' => $email, |
247
|
|
|
'registrationCode' => $registrationCode, |
248
|
|
|
'raLocations' => $raLocations, |
249
|
|
|
]; |
250
|
|
|
|
251
|
|
|
// Rendering file template instead of string |
252
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
253
|
|
|
$body = $this->templateEngine->render( |
254
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
255
|
|
|
$parameters |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
/** @var Message $message */ |
259
|
|
|
$message = $this->mailer->createMessage(); |
260
|
|
|
$message |
261
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
262
|
|
|
->addTo($email, $commonName) |
263
|
|
|
->setSubject($subject) |
264
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
265
|
|
|
|
266
|
|
|
$this->mailer->send($message); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param Locale $locale |
271
|
|
|
* @param CommonName $commonName |
272
|
|
|
* @param Email $email |
273
|
|
|
*/ |
274
|
|
|
public function sendVettedEmail( |
275
|
|
|
Locale $locale, |
276
|
|
|
CommonName $commonName, |
277
|
|
|
Email $email |
278
|
|
|
) { |
279
|
|
|
$subject = $this->translator->trans( |
280
|
|
|
'ss.mail.vetted_email.subject', |
281
|
|
|
['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()], |
282
|
|
|
null, |
283
|
|
|
$locale->getLocale() |
284
|
|
|
); |
285
|
|
|
|
286
|
|
|
$emailTemplate = $this->emailTemplateService->findByName('vetted', $locale->getLocale(), $this->fallbackLocale); |
287
|
|
|
$parameters = [ |
288
|
|
|
'templateString' => $emailTemplate->htmlContent, |
289
|
|
|
'locale' => $locale->getLocale(), |
290
|
|
|
'commonName' => $commonName->getCommonName(), |
291
|
|
|
'email' => $email->getEmail(), |
292
|
|
|
]; |
293
|
|
|
|
294
|
|
|
// Rendering file template instead of string |
295
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
296
|
|
|
$body = $this->templateEngine->render( |
297
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
298
|
|
|
$parameters |
299
|
|
|
); |
300
|
|
|
|
301
|
|
|
/** @var Message $message */ |
302
|
|
|
$message = $this->mailer->createMessage(); |
303
|
|
|
$message |
304
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
305
|
|
|
->addTo($email->getEmail(), $commonName->getCommonName()) |
306
|
|
|
->setSubject($subject) |
307
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
308
|
|
|
|
309
|
|
|
$this->mailer->send($message); |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.