1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2016 SURFnet B.V. |
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\MiddlewareBundle\Service; |
20
|
|
|
|
21
|
|
|
use Assert\Assertion; |
22
|
|
|
use DateTime; |
23
|
|
|
use Surfnet\Stepup\Configuration\Value\Institution; |
24
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\InstitutionConfigurationOptionsService; |
25
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Configuration\Service\RaLocationService; |
26
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Service\RaListingService; |
27
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\RegistrationAuthorityCredentials; |
28
|
|
|
use Surfnet\StepupMiddleware\ApiBundle\Identity\Value\VerifiedTokenInformation; |
29
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
30
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
31
|
|
|
use Swift_Mailer as Mailer; |
32
|
|
|
use Swift_Message as Message; |
33
|
|
|
use Symfony\Component\Templating\EngineInterface; |
34
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
35
|
|
|
|
36
|
|
|
class VerifiedSecondFactorReminderMailService |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Mailer |
40
|
|
|
*/ |
41
|
|
|
private $mailer; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Sender |
45
|
|
|
*/ |
46
|
|
|
private $sender; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var TranslatorInterface |
50
|
|
|
*/ |
51
|
|
|
private $translator; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var EngineInterface |
55
|
|
|
*/ |
56
|
|
|
private $templateEngine; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var EmailTemplateService |
60
|
|
|
*/ |
61
|
|
|
private $emailTemplateService; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var InstitutionConfigurationOptionsService |
65
|
|
|
*/ |
66
|
|
|
private $institutionConfigurationOptionsService; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var RaListingService |
70
|
|
|
*/ |
71
|
|
|
private $raListingService; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var RaLocationService |
75
|
|
|
*/ |
76
|
|
|
private $raLocationService; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
private $fallbackLocale; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Mailer $mailer |
85
|
|
|
* @param Sender $sender |
86
|
|
|
* @param TranslatorInterface $translator |
87
|
|
|
* @param EngineInterface $templateEngine |
88
|
|
|
* @param EmailTemplateService $emailTemplateService |
89
|
|
|
* @param InstitutionConfigurationOptionsService $institutionConfigurationOptionsService |
90
|
|
|
* @param RaListingService $raListingService |
91
|
|
|
* @param RaLocationService $raLocationService |
92
|
|
|
* @param string $fallbackLocale |
93
|
|
|
*/ |
94
|
|
|
public function __construct( |
95
|
|
|
Mailer $mailer, |
96
|
|
|
Sender $sender, |
97
|
|
|
TranslatorInterface $translator, |
98
|
|
|
EngineInterface $templateEngine, |
99
|
|
|
EmailTemplateService $emailTemplateService, |
100
|
|
|
InstitutionConfigurationOptionsService $institutionConfigurationOptionsService, |
|
|
|
|
101
|
|
|
RaListingService $raListingService, |
102
|
|
|
RaLocationService $raLocationService, |
103
|
|
|
$fallbackLocale |
104
|
|
|
) { |
105
|
|
|
Assertion::string($fallbackLocale, 'Fallback locale "%s" expected to be string, type %s given'); |
106
|
|
|
$this->mailer = $mailer; |
107
|
|
|
$this->sender = $sender; |
108
|
|
|
$this->translator = $translator; |
109
|
|
|
$this->templateEngine = $templateEngine; |
110
|
|
|
$this->emailTemplateService = $emailTemplateService; |
111
|
|
|
$this->institutionConfigurationOptionsService = $institutionConfigurationOptionsService; |
112
|
|
|
$this->raListingService = $raListingService; |
113
|
|
|
$this->raLocationService = $raLocationService; |
114
|
|
|
$this->fallbackLocale = $fallbackLocale; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param VerifiedTokenInformation $tokenInformation |
119
|
|
|
* @return int |
120
|
|
|
*/ |
121
|
|
|
public function sendReminder(VerifiedTokenInformation $tokenInformation) |
122
|
|
|
{ |
123
|
|
|
$institution = new Institution((string) $tokenInformation->getInstitution()); |
124
|
|
|
$institutionConfigurationOptions = $this->institutionConfigurationOptionsService |
|
|
|
|
125
|
|
|
->findInstitutionConfigurationOptionsFor($institution); |
126
|
|
|
if ($institutionConfigurationOptions->useRaLocationsOption->isEnabled()) { |
127
|
|
|
return $this->sendReminderWithInstitution( |
128
|
|
|
$tokenInformation->getPreferredLocale(), |
129
|
|
|
$tokenInformation->getCommonName(), |
130
|
|
|
$tokenInformation->getEmail(), |
131
|
|
|
$tokenInformation->getRequestedAt(), |
132
|
|
|
$tokenInformation->getRegistrationCode(), |
133
|
|
|
$this->raLocationService->listRaLocationsFor($institution) |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$ras = $this->raListingService->listRegistrationAuthoritiesFor($tokenInformation->getInstitution()); |
138
|
|
|
|
139
|
|
|
if ($institutionConfigurationOptions->showRaaContactInformationOption->isEnabled()) { |
140
|
|
|
return $this->sendReminderWithRas( |
141
|
|
|
$tokenInformation->getPreferredLocale(), |
142
|
|
|
$tokenInformation->getCommonName(), |
143
|
|
|
$tokenInformation->getEmail(), |
144
|
|
|
$tokenInformation->getRequestedAt(), |
145
|
|
|
$tokenInformation->getRegistrationCode(), |
146
|
|
|
$ras |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$rasWithoutRaas = array_filter($ras, function (RegistrationAuthorityCredentials $ra) { |
151
|
|
|
return !$ra->isRaa(); |
152
|
|
|
}); |
153
|
|
|
|
154
|
|
|
return $this->sendReminderWithRas( |
155
|
|
|
$tokenInformation->getPreferredLocale(), |
156
|
|
|
$tokenInformation->getCommonName(), |
157
|
|
|
$tokenInformation->getEmail(), |
158
|
|
|
$tokenInformation->getRequestedAt(), |
159
|
|
|
$tokenInformation->getRegistrationCode(), |
160
|
|
|
$rasWithoutRaas |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $locale |
166
|
|
|
* @param string $commonName |
167
|
|
|
* @param string $email |
168
|
|
|
* @param DateTime $requestedAt |
169
|
|
|
* @param $registrationCode |
170
|
|
|
* @return int |
171
|
|
|
*/ |
172
|
|
View Code Duplication |
private function sendReminderWithInstitution( |
|
|
|
|
173
|
|
|
$locale, |
174
|
|
|
$commonName, |
175
|
|
|
$email, |
176
|
|
|
$requestedAt, |
177
|
|
|
$registrationCode, |
178
|
|
|
$raLocations |
179
|
|
|
) { |
180
|
|
|
$subject = $this->translator->trans( |
181
|
|
|
'ss.mail.registration_email.subject', |
182
|
|
|
['%commonName%' => $commonName], |
183
|
|
|
'messages', |
184
|
|
|
$locale |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
188
|
|
|
'second_factor_verification_reminder_with_ra_locations', |
189
|
|
|
$locale, |
190
|
|
|
$this->fallbackLocale |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
$parameters = [ |
194
|
|
|
'templateString' => $emailTemplate->htmlContent, |
195
|
|
|
'locale' => $locale, |
196
|
|
|
'commonName' => $commonName, |
197
|
|
|
'expirationDate' => $requestedAt, |
198
|
|
|
'registrationCode' => $registrationCode, |
199
|
|
|
'raLocations' => $raLocations, |
200
|
|
|
]; |
201
|
|
|
|
202
|
|
|
// Rendering file template instead of string |
203
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
204
|
|
|
$body = $this->templateEngine->render( |
205
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
206
|
|
|
$parameters |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
/** @var Message $message */ |
210
|
|
|
$message = $this->mailer->createMessage(); |
211
|
|
|
$message |
212
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
213
|
|
|
->addTo($email, $commonName) |
214
|
|
|
->setSubject($subject) |
215
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
216
|
|
|
|
217
|
|
|
return $this->mailer->send($message); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
View Code Duplication |
private function sendReminderWithRas( |
|
|
|
|
221
|
|
|
$locale, |
222
|
|
|
$commonName, |
223
|
|
|
$email, |
224
|
|
|
$requestedAt, |
225
|
|
|
$registrationCode, |
226
|
|
|
array $ras |
227
|
|
|
) { |
228
|
|
|
$subject = $this->translator->trans( |
229
|
|
|
'ss.mail.registration_email.subject', |
230
|
|
|
['%commonName%' => $commonName], |
231
|
|
|
'messages', |
232
|
|
|
$locale |
233
|
|
|
); |
234
|
|
|
|
235
|
|
|
$emailTemplate = $this->emailTemplateService->findByName( |
236
|
|
|
'second_factor_verification_reminder_with_ras', |
237
|
|
|
$locale, |
238
|
|
|
$this->fallbackLocale |
239
|
|
|
); |
240
|
|
|
|
241
|
|
|
$parameters = [ |
242
|
|
|
'templateString' => $emailTemplate->htmlContent, |
243
|
|
|
'locale' => $locale, |
244
|
|
|
'commonName' => $commonName, |
245
|
|
|
'expirationDate' => $requestedAt, |
246
|
|
|
'registrationCode' => $registrationCode, |
247
|
|
|
'ras' => $ras, |
248
|
|
|
]; |
249
|
|
|
|
250
|
|
|
// Rendering file template instead of string |
251
|
|
|
// (https://github.com/symfony/symfony/issues/10865#issuecomment-42438248) |
252
|
|
|
$body = $this->templateEngine->render( |
253
|
|
|
'SurfnetStepupMiddlewareCommandHandlingBundle:SecondFactorMailService:email.html.twig', |
254
|
|
|
$parameters |
255
|
|
|
); |
256
|
|
|
|
257
|
|
|
/** @var Message $message */ |
258
|
|
|
$message = $this->mailer->createMessage(); |
259
|
|
|
$message |
260
|
|
|
->setFrom($this->sender->getEmail(), $this->sender->getName()) |
261
|
|
|
->addTo($email, $commonName) |
262
|
|
|
->setSubject($subject) |
263
|
|
|
->setBody($body, 'text/html', 'utf-8'); |
264
|
|
|
|
265
|
|
|
return $this->mailer->send($message); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.