/
RecoveryService.php
192 lines (169 loc) · 6.19 KB
/
RecoveryService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\FrontendLogin\Service;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use TYPO3\CMS\Core\Mail\FluidEmail;
use TYPO3\CMS\Core\Mail\Mailer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
use TYPO3\CMS\FrontendLogin\Configuration\IncompleteConfigurationException;
use TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration;
use TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository;
use TYPO3\CMS\FrontendLogin\Event\SendRecoveryEmailEvent;
/**
* @internal this is a concrete TYPO3 implementation and solely used for EXT:felogin and not part of TYPO3's Core API.
*/
class RecoveryService implements RecoveryServiceInterface
{
/**
* @var RecoveryConfiguration
*/
protected $recoveryConfiguration;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var Mailer
*/
protected $mailer;
/**
* @var array
*/
protected $settings;
/**
* @var UriBuilder
*/
protected $uriBuilder;
/**
* @var FrontendUserRepository
*/
protected $userRepository;
/**
* @param Mailer $mailer
* @param EventDispatcherInterface $eventDispatcher
* @param ConfigurationManager $configurationManager
* @param RecoveryConfiguration $recoveryConfiguration
* @param UriBuilder $uriBuilder
* @param FrontendUserRepository $userRepository
*
* @throws InvalidConfigurationTypeException
*/
public function __construct(
Mailer $mailer,
EventDispatcherInterface $eventDispatcher,
ConfigurationManager $configurationManager,
RecoveryConfiguration $recoveryConfiguration,
UriBuilder $uriBuilder,
FrontendUserRepository $userRepository
) {
$this->mailer = $mailer;
$this->eventDispatcher = $eventDispatcher;
$this->settings = $configurationManager->getConfiguration(ConfigurationManager::CONFIGURATION_TYPE_SETTINGS);
$this->recoveryConfiguration = $recoveryConfiguration;
$this->uriBuilder = $uriBuilder;
$this->userRepository = $userRepository;
}
/**
* Sends an email with an absolute link including a forgot hash to the passed email address
* with instructions to recover the account.
*
* @param string $emailAddress Receiver's email address.
*
* @throws TransportExceptionInterface
* @throws IncompleteConfigurationException
*/
public function sendRecoveryEmail(string $emailAddress): void
{
$hash = $this->recoveryConfiguration->getForgotHash();
$this->userRepository->updateForgotHashForUserByEmail($emailAddress, GeneralUtility::hmac($hash));
$userInformation = $this->userRepository->fetchUserInformationByEmail($emailAddress);
$receiver = new Address($emailAddress, $this->getReceiverName($userInformation));
$email = $this->prepareMail($receiver, $hash);
$event = new SendRecoveryEmailEvent($email, $userInformation);
$this->eventDispatcher->dispatch($event);
$this->mailer->send($event->getEmail());
}
/**
* Get display name from values. Fallback to username if none of the "_name" fields is set.
*
* @param array $userInformation
*
* @return string
*/
protected function getReceiverName(array $userInformation): string
{
$displayName = trim(
sprintf(
'%s%s%s',
$userInformation['first_name'],
$userInformation['middle_name'] ? " {$userInformation['middle_name']}" : '',
$userInformation['last_name'] ? " {$userInformation['last_name']}" : ''
)
);
return $displayName ?: $userInformation['username'];
}
/**
* Create email object from configuration.
*
* @param Address $receiver
* @param string $hash
*
* @return Email
* @throws IncompleteConfigurationException
*/
protected function prepareMail(Address $receiver, string $hash): Email
{
$url = $this->uriBuilder->setCreateAbsoluteUri(true)
->uriFor(
'showChangePassword',
['hash' => $hash],
'PasswordRecovery',
'felogin',
'Login'
);
$variables = [
'receiverName' => $receiver->getName(),
'url' => $url,
'validUntil' => date($this->settings['dateFormat'], $this->recoveryConfiguration->getLifeTimeTimestamp()),
];
$mailTemplatePaths = $this->recoveryConfiguration->getMailTemplatePaths();
$mail = GeneralUtility::makeInstance(FluidEmail::class, $mailTemplatePaths);
$mail->subject($this->getEmailSubject())
->from($this->recoveryConfiguration->getSender())
->to($receiver)
->assignMultiple($variables)
->setTemplate($this->recoveryConfiguration->getMailTemplateName());
$replyTo = $this->recoveryConfiguration->getReplyTo();
if ($replyTo) {
$mail->addReplyTo($replyTo);
}
if (($GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
$mail->setRequest($GLOBALS['TYPO3_REQUEST']);
}
return $mail;
}
protected function getEmailSubject(): string
{
return LocalizationUtility::translate('password_recovery_mail_header', 'felogin');
}
}