‪TYPO3CMS  10.4
RecoveryService.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\EventDispatcher\EventDispatcherInterface;
21 use Psr\Http\Message\ServerRequestInterface;
22 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
23 use Symfony\Component\Mime\Address;
24 use Symfony\Component\Mime\Email;
36 
41 {
45  protected ‪$recoveryConfiguration;
46 
50  protected ‪$eventDispatcher;
51 
55  protected ‪$mailer;
56 
60  protected ‪$settings;
61 
65  protected ‪$uriBuilder;
66 
70  protected ‪$userRepository;
71 
82  public function ‪__construct(
84  EventDispatcherInterface ‪$eventDispatcher,
85  ‪ConfigurationManager $configurationManager,
89  ) {
90  $this->mailer = ‪$mailer;
91  $this->eventDispatcher = ‪$eventDispatcher;
92  $this->settings = $configurationManager->‪getConfiguration(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS);
93  $this->recoveryConfiguration = ‪$recoveryConfiguration;
94  $this->uriBuilder = ‪$uriBuilder;
95  $this->userRepository = ‪$userRepository;
96  }
97 
107  public function ‪sendRecoveryEmail(string $emailAddress): void
108  {
109  $hash = $this->recoveryConfiguration->getForgotHash();
110  $this->userRepository->updateForgotHashForUserByEmail($emailAddress, GeneralUtility::hmac($hash));
111  $userInformation = $this->userRepository->fetchUserInformationByEmail($emailAddress);
112  $receiver = new Address($emailAddress, $this->‪getReceiverName($userInformation));
113  $email = $this->‪prepareMail($receiver, $hash);
114 
115  $event = new ‪SendRecoveryEmailEvent($email, $userInformation);
116  $this->eventDispatcher->dispatch($event);
117  $this->mailer->send($event->getEmail());
118  }
119 
127  protected function ‪getReceiverName(array $userInformation): string
128  {
129  $displayName = trim(
130  sprintf(
131  '%s%s%s',
132  $userInformation['first_name'],
133  $userInformation['middle_name'] ? " {$userInformation['middle_name']}" : '',
134  $userInformation['last_name'] ? " {$userInformation['last_name']}" : ''
135  )
136  );
137 
138  return $displayName ?: $userInformation['username'];
139  }
140 
150  protected function ‪prepareMail(Address $receiver, string $hash): Email
151  {
152  $url = $this->uriBuilder->setCreateAbsoluteUri(true)
153  ->uriFor(
154  'showChangePassword',
155  ['hash' => $hash],
156  'PasswordRecovery',
157  'felogin',
158  'Login'
159  );
160 
161  $variables = [
162  'receiverName' => $receiver->getName(),
163  'url' => $url,
164  'validUntil' => date($this->settings['dateFormat'], $this->recoveryConfiguration->getLifeTimeTimestamp()),
165  ];
166 
167  $mailTemplatePaths = $this->recoveryConfiguration->getMailTemplatePaths();
168 
169  $mail = GeneralUtility::makeInstance(FluidEmail::class, $mailTemplatePaths);
170  $mail->subject($this->‪getEmailSubject())
171  ->from($this->recoveryConfiguration->getSender())
172  ->to($receiver)
173  ->assignMultiple($variables)
174  ->setTemplate($this->recoveryConfiguration->getMailTemplateName());
175 
176  $replyTo = $this->recoveryConfiguration->getReplyTo();
177  if ($replyTo) {
178  $mail->addReplyTo($replyTo);
179  }
180 
181  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
182  $mail->setRequest(‪$GLOBALS['TYPO3_REQUEST']);
183  }
184 
185  return $mail;
186  }
187 
188  protected function ‪getEmailSubject(): string
189  {
190  return ‪LocalizationUtility::translate('password_recovery_mail_header', 'felogin');
191  }
192 }
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:31
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:39
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager\getConfiguration
‪array getConfiguration(string $configurationType, string $extensionName=null, string $pluginName=null)
Definition: ConfigurationManager.php:113
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\prepareMail
‪Email prepareMail(Address $receiver, string $hash)
Definition: RecoveryService.php:144
‪TYPO3\CMS\FrontendLogin\Service
Definition: RecoveryService.php:18
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService
Definition: RecoveryService.php:41
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\__construct
‪__construct(Mailer $mailer, EventDispatcherInterface $eventDispatcher, ConfigurationManager $configurationManager, RecoveryConfiguration $recoveryConfiguration, UriBuilder $uriBuilder, FrontendUserRepository $userRepository)
Definition: RecoveryService.php:76
‪TYPO3\CMS\FrontendLogin\Configuration\IncompleteConfigurationException
Definition: IncompleteConfigurationException.php:24
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$mailer
‪Mailer $mailer
Definition: RecoveryService.php:52
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\sendRecoveryEmail
‪sendRecoveryEmail(string $emailAddress)
Definition: RecoveryService.php:101
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$settings
‪array $settings
Definition: RecoveryService.php:56
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\FrontendLogin\Configuration\RecoveryConfiguration
Definition: RecoveryConfiguration.php:34
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$recoveryConfiguration
‪RecoveryConfiguration $recoveryConfiguration
Definition: RecoveryService.php:44
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
Definition: InvalidConfigurationTypeException.php:26
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface\CONFIGURATION_TYPE_SETTINGS
‪const CONFIGURATION_TYPE_SETTINGS
Definition: ConfigurationManagerInterface.php:30
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\getEmailSubject
‪getEmailSubject()
Definition: RecoveryService.php:182
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$userRepository
‪FrontendUserRepository $userRepository
Definition: RecoveryService.php:64
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\getReceiverName
‪string getReceiverName(array $userInformation)
Definition: RecoveryService.php:121
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$uriBuilder
‪UriBuilder $uriBuilder
Definition: RecoveryService.php:60
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: RecoveryService.php:48
‪TYPO3\CMS\FrontendLogin\Event\SendRecoveryEmailEvent
Definition: SendRecoveryEmailEvent.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\FrontendLogin\Service\RecoveryServiceInterface
Definition: RecoveryServiceInterface.php:23