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 Surfnet\Stepup\Identity\Value\CommonName; |
22
|
|
|
use Surfnet\Stepup\Identity\Value\Email; |
23
|
|
|
use Surfnet\Stepup\Identity\Value\Locale; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials; |
25
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
26
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
27
|
|
|
use Swift_Mailer as Mailer; |
28
|
|
|
use Swift_Message as Message; |
29
|
|
|
use Symfony\Component\Templating\EngineInterface; |
30
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
31
|
|
|
|
32
|
|
|
class SecondFactorMailService |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Mailer |
36
|
|
|
*/ |
37
|
|
|
private $mailer; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Sender |
41
|
|
|
*/ |
42
|
|
|
private $sender; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var TranslatorInterface |
46
|
|
|
*/ |
47
|
|
|
private $translator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var EngineInterface |
51
|
|
|
*/ |
52
|
|
|
private $templateEngine; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $emailVerificationUrlTemplate; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var \Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService |
61
|
|
|
*/ |
62
|
|
|
private $emailTemplateService; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $fallbackLocale; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Mailer $mailer |
71
|
|
|
* @param Sender $sender |
72
|
|
|
* @param TranslatorInterface $translator |
73
|
|
|
* @param EngineInterface $templateEngine |
74
|
|
|
* @param string $emailVerificationUrlTemplate |
75
|
|
|
* @param EmailTemplateService $emailTemplateService |
76
|
|
|
* @param string $fallbackLocale |
77
|
|
|
*/ |
78
|
|
|
public function __construct( |
79
|
|
|
Mailer $mailer, |
80
|
|
|
Sender $sender, |
81
|
|
|
TranslatorInterface $translator, |
82
|
|
|
EngineInterface $templateEngine, |
83
|
|
|
$emailVerificationUrlTemplate, |
84
|
|
|
EmailTemplateService $emailTemplateService, |
85
|
|
|
$fallbackLocale |
86
|
|
|
) { |
87
|
|
|
$this->mailer = $mailer; |
88
|
|
|
$this->sender = $sender; |
89
|
|
|
$this->translator = $translator; |
90
|
|
|
$this->templateEngine = $templateEngine; |
91
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
92
|
|
|
$this->emailTemplateService = $emailTemplateService; |
93
|
|
|
$this->fallbackLocale = $fallbackLocale; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $locale |
98
|
|
|
* @param string $commonName |
99
|
|
|
* @param string $email |
100
|
|
|
* @param string $verificationNonce |
101
|
|
|
*/ |
102
|
|
|
public function sendEmailVerificationEmail( |
103
|
|
|
$locale, |
104
|
|
|
$commonName, |
105
|
|
|
$email, |
106
|
|
|
$verificationNonce |
107
|
|
|
) { |
108
|
|
|
$subject = $this->translator->trans( |
109
|
|
|
'ss.mail.email_verification_email.subject', |
110
|
|
|
['%commonName%' => $commonName], |
111
|
|
|
null, |
112
|
|
|
$locale |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
$verificationUrl = str_replace( |
116
|
|
|
'{nonce}', |
117
|
|
|
urlencode($verificationNonce), |
118
|
|
|
$this->emailVerificationUrlTemplate |
119
|
|
|
); |
120
|
|
|
$emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale); |
121
|
|
|
|
122
|
|
|
$parameters = [ |
123
|
|
|
'templateString' => $emailTemplate->htmlContent, |
124
|
|
|
'locale' => $locale, |
125
|
|
|
'commonName' => $commonName, |
126
|
|
|
'email' => $email, |
127
|
|
|
'verificationUrl' => $verificationUrl |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
// Rendering file template instead of string |
131
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
132
|
|
|
$body = $this->templateEngine->render( |
133
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
134
|
|
|
$parameters |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
/** @var Message $message */ |
138
|
|
|
$message = $this->mailer->createMessage(); |
139
|
|
|
$message |
140
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
141
|
|
|
->addTo($email, $commonName) |
142
|
|
|
->setSubject($subject) |
143
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
144
|
|
|
|
145
|
|
|
$this->mailer->send($message); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param string $locale |
150
|
|
|
* @param string $commonName |
151
|
|
|
* @param string $email |
152
|
|
|
* @param string $registrationCode |
153
|
|
|
* @param RegistrationAuthorityCredentials[] $ras |
154
|
|
|
*/ |
155
|
|
|
public function sendRegistrationEmail( |
156
|
|
|
$locale, |
157
|
|
|
$commonName, |
158
|
|
|
$email, |
159
|
|
|
$registrationCode, |
160
|
|
|
array $ras |
161
|
|
|
) { |
162
|
|
|
$subject = $this->translator->trans( |
163
|
|
|
'ss.mail.registration_email.subject', |
164
|
|
|
['%commonName%' => $commonName], |
165
|
|
|
null, |
166
|
|
|
$locale |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
170
|
|
|
'registration_code', |
171
|
|
|
$locale, |
172
|
|
|
$this->fallbackLocale |
173
|
|
|
); |
174
|
|
|
$parameters = [ |
175
|
|
|
'templateString' => $emailTemplate->htmlContent, |
176
|
|
|
'locale' => $locale, |
177
|
|
|
'commonName' => $commonName, |
178
|
|
|
'email' => $email, |
179
|
|
|
'registrationCode' => $registrationCode, |
180
|
|
|
'ras' => $ras, |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
// Rendering file template instead of string |
184
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
185
|
|
|
$body = $this->templateEngine->render( |
186
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
187
|
|
|
$parameters |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
/** @var Message $message */ |
191
|
|
|
$message = $this->mailer->createMessage(); |
192
|
|
|
$message |
193
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
194
|
|
|
->addTo($email, $commonName) |
195
|
|
|
->setSubject($subject) |
196
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
197
|
|
|
|
198
|
|
|
$this->mailer->send($message); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param Locale $locale |
203
|
|
|
* @param CommonName $commonName |
204
|
|
|
* @param Email $email |
205
|
|
|
*/ |
206
|
|
|
public function sendVettedEmail( |
207
|
|
|
Locale $locale, |
208
|
|
|
CommonName $commonName, |
209
|
|
|
Email $email |
210
|
|
|
) { |
211
|
|
|
$subject = $this->translator->trans( |
212
|
|
|
'ss.mail.vetted_email.subject', |
213
|
|
|
['%commonName%' => $commonName->getCommonName(), '%email%' => $email->getEmail()], |
214
|
|
|
null, |
215
|
|
|
$locale->getLocale() |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
219
|
|
|
'vetted', |
220
|
|
|
$locale->getLocale(), |
221
|
|
|
$this->fallbackLocale |
222
|
|
|
); |
223
|
|
|
$parameters = [ |
224
|
|
|
'templateString' => $emailTemplate->htmlContent, |
225
|
|
|
'locale' => $locale->getLocale(), |
226
|
|
|
'commonName' => $commonName->getCommonName(), |
227
|
|
|
'email' => $email->getEmail(), |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
// Rendering file template instead of string |
231
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
232
|
|
|
$body = $this->templateEngine->render( |
233
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
234
|
|
|
$parameters |
235
|
|
|
); |
236
|
|
|
|
237
|
|
|
/** @var Message $message */ |
238
|
|
|
$message = $this->mailer->createMessage(); |
239
|
|
|
$message |
240
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
241
|
|
|
->addTo($email->getEmail(), $commonName->getCommonName()) |
242
|
|
|
->setSubject($subject) |
243
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
244
|
|
|
|
245
|
|
|
$this->mailer->send($message); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|