|
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\CommandHandlingBundle\Identity\Service; |
|
20
|
|
|
|
|
21
|
|
|
use Assert\Assertion; |
|
22
|
|
|
use Assert\AssertionFailedException; |
|
23
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Configuration\Service\EmailTemplateService; |
|
24
|
|
|
use Surfnet\StepupMiddleware\CommandHandlingBundle\Value\Sender; |
|
25
|
|
|
use Symfony\Bridge\Twig\Mime\TemplatedEmail; |
|
26
|
|
|
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
|
27
|
|
|
use Symfony\Component\Mailer\MailerInterface as Mailer; |
|
28
|
|
|
use Symfony\Component\Mime\Address; |
|
29
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
30
|
|
|
|
|
31
|
|
|
final class EmailVerificationMailService |
|
32
|
|
|
{ |
|
33
|
|
|
private readonly string $emailVerificationUrlTemplate; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @throws AssertionFailedException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct( |
|
39
|
|
|
private readonly Mailer $mailer, |
|
40
|
|
|
private readonly Sender $sender, |
|
41
|
|
|
private readonly TranslatorInterface $translator, |
|
42
|
|
|
string $emailVerificationUrlTemplate, |
|
43
|
|
|
private readonly EmailTemplateService $emailTemplateService, |
|
44
|
|
|
private readonly string $fallbackLocale, |
|
45
|
|
|
private readonly string $selfServiceUrl, |
|
46
|
|
|
) { |
|
47
|
|
|
Assertion::string( |
|
48
|
|
|
$emailVerificationUrlTemplate, |
|
49
|
|
|
'Email verification URL template "%s" expected to be string, type %s given', |
|
50
|
|
|
); |
|
51
|
|
|
$this->emailVerificationUrlTemplate = $emailVerificationUrlTemplate; |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @throws TransportExceptionInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function sendEmailVerificationEmail( |
|
58
|
|
|
string $locale, |
|
59
|
|
|
string $commonName, |
|
60
|
|
|
string $email, |
|
61
|
|
|
string $verificationNonce, |
|
62
|
|
|
): void { |
|
63
|
|
|
$subject = $this->translator->trans( |
|
64
|
|
|
'ss.mail.email_verification_email.subject', |
|
65
|
|
|
['%commonName%' => $commonName], |
|
66
|
|
|
'messages', |
|
67
|
|
|
$locale, |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$verificationUrl = str_replace( |
|
71
|
|
|
'{nonce}', |
|
72
|
|
|
urlencode($verificationNonce), |
|
73
|
|
|
$this->emailVerificationUrlTemplate, |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
// In TemplatedEmail email is a reserved keyword, we also use it as a parameter that can be used in the mail |
|
77
|
|
|
// message, to prevent having to update all templates, and prevent a 500 error from the mailer, we perform a |
|
78
|
|
|
// search and replace of the {email} parameter in the template. |
|
79
|
|
|
$emailTemplate = $this->emailTemplateService->findByName('confirm_email', $locale, $this->fallbackLocale); |
|
80
|
|
|
$emailTemplate->htmlContent = str_replace( |
|
81
|
|
|
'{email}', |
|
82
|
|
|
'{emailAddress}', |
|
83
|
|
|
$emailTemplate->htmlContent, |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
$parameters = [ |
|
87
|
|
|
'templateString' => $emailTemplate->htmlContent, |
|
88
|
|
|
'locale' => $locale, |
|
89
|
|
|
'commonName' => $commonName, |
|
90
|
|
|
'emailAddress' => $email, |
|
91
|
|
|
'verificationUrl' => $verificationUrl, |
|
92
|
|
|
'selfServiceUrl' => $this->selfServiceUrl, |
|
93
|
|
|
]; |
|
94
|
|
|
|
|
95
|
|
|
$fromAddress = new Address($this->sender->getEmail(), $this->sender->getName()); |
|
96
|
|
|
$toAddress = new Address($email, $commonName); |
|
97
|
|
|
$message = new TemplatedEmail(); |
|
98
|
|
|
$message |
|
99
|
|
|
->from($fromAddress) |
|
100
|
|
|
->to($toAddress) |
|
101
|
|
|
->subject($subject) |
|
102
|
|
|
->htmlTemplate('@SurfnetStepupMiddlewareCommandHandling/SecondFactorMailService/email.html.twig') |
|
103
|
|
|
->context($parameters); |
|
104
|
|
|
$this->mailer->send($message); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|