‪TYPO3CMS  11.5
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;
35 
40 {
44  protected ‪$recoveryConfiguration;
45 
49  protected ‪$eventDispatcher;
50 
54  protected ‪$mailer;
55 
59  protected ‪$settings;
60 
64  protected ‪$uriBuilder;
65 
69  protected ‪$userRepository;
70 
81  public function ‪__construct(
83  EventDispatcherInterface ‪$eventDispatcher,
84  ‪ConfigurationManager $configurationManager,
88  ) {
89  $this->mailer = ‪$mailer;
90  $this->eventDispatcher = ‪$eventDispatcher;
91  $this->settings = $configurationManager->‪getConfiguration(‪ConfigurationManager::CONFIGURATION_TYPE_SETTINGS);
92  $this->recoveryConfiguration = ‪$recoveryConfiguration;
93  $this->uriBuilder = ‪$uriBuilder;
94  $this->userRepository = ‪$userRepository;
95  }
96 
103  public function ‪sendRecoveryEmail(string $emailAddress): void
104  {
105  $hash = $this->recoveryConfiguration->getForgotHash();
106  // @todo: This repository method call should be moved to PasswordRecoveryController, since its
107  // @todo: unexpected that it happens here. Would also drop the dependency to FrontendUserRepository
108  // @todo: in this sendRecoveryEmail() method and the class.
109  $this->userRepository->updateForgotHashForUserByEmail($emailAddress, GeneralUtility::hmac($hash));
110  $userInformation = $this->userRepository->fetchUserInformationByEmail($emailAddress);
111  $receiver = new Address($emailAddress, $this->‪getReceiverName($userInformation));
112  $email = $this->‪prepareMail($receiver, $hash);
113 
114  $event = new ‪SendRecoveryEmailEvent($email, $userInformation);
115  $this->eventDispatcher->dispatch($event);
116  $this->mailer->send($event->getEmail());
117  }
118 
122  protected function ‪getReceiverName(array $userInformation): string
123  {
124  $displayName = trim(
125  sprintf(
126  '%s%s%s',
127  $userInformation['first_name'],
128  $userInformation['middle_name'] ? " {$userInformation['middle_name']}" : '',
129  $userInformation['last_name'] ? " {$userInformation['last_name']}" : ''
130  )
131  );
132 
133  return $displayName ? $displayName . ' (' . $userInformation['username'] . ')' : $userInformation['username'];
134  }
135 
139  protected function ‪prepareMail(Address $receiver, string $hash): Email
140  {
141  $url = $this->uriBuilder
142  ->reset()
143  ->setCreateAbsoluteUri(true)
144  ->uriFor(
145  'showChangePassword',
146  ['hash' => $hash],
147  'PasswordRecovery',
148  'felogin',
149  'Login'
150  );
151 
152  $variables = [
153  'receiverName' => $receiver->getName(),
154  'url' => $url,
155  'validUntil' => date($this->settings['dateFormat'], $this->recoveryConfiguration->getLifeTimeTimestamp()),
156  ];
157 
158  $mailTemplatePaths = $this->recoveryConfiguration->getMailTemplatePaths();
159 
160  $mail = GeneralUtility::makeInstance(FluidEmail::class, $mailTemplatePaths);
161  $mail->subject($this->‪getEmailSubject())
162  ->from($this->recoveryConfiguration->getSender())
163  ->to($receiver)
164  ->assignMultiple($variables)
165  ->setTemplate($this->recoveryConfiguration->getMailTemplateName());
166 
167  $replyTo = $this->recoveryConfiguration->getReplyTo();
168  if ($replyTo) {
169  $mail->addReplyTo($replyTo);
170  }
171 
172  if ((‪$GLOBALS['TYPO3_REQUEST'] ?? null) instanceof ServerRequestInterface) {
173  $mail->setRequest(‪$GLOBALS['TYPO3_REQUEST']);
174  }
175 
176  return $mail;
177  }
178 
179  protected function ‪getEmailSubject(): string
180  {
181  return ‪LocalizationUtility::translate('password_recovery_mail_header', 'felogin');
182  }
183 }
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\prepareMail
‪prepareMail(Address $receiver, string $hash)
Definition: RecoveryService.php:133
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:30
‪TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
Definition: UriBuilder.php:41
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager\getConfiguration
‪array getConfiguration(string $configurationType, string $extensionName=null, string $pluginName=null)
Definition: ConfigurationManager.php:101
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\getReceiverName
‪getReceiverName(array $userInformation)
Definition: RecoveryService.php:116
‪TYPO3\CMS\FrontendLogin\Service
Definition: RecoveryService.php:18
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService
Definition: RecoveryService.php:40
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\__construct
‪__construct(Mailer $mailer, EventDispatcherInterface $eventDispatcher, ConfigurationManager $configurationManager, RecoveryConfiguration $recoveryConfiguration, UriBuilder $uriBuilder, FrontendUserRepository $userRepository)
Definition: RecoveryService.php:75
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$mailer
‪Mailer $mailer
Definition: RecoveryService.php:51
‪TYPO3\CMS\Extbase\Configuration\ConfigurationManager
Definition: ConfigurationManager.php:33
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\sendRecoveryEmail
‪sendRecoveryEmail(string $emailAddress)
Definition: RecoveryService.php:97
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$settings
‪array $settings
Definition: RecoveryService.php:55
‪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:43
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
Definition: InvalidConfigurationTypeException.php:25
‪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:173
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$userRepository
‪FrontendUserRepository $userRepository
Definition: RecoveryService.php:63
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$uriBuilder
‪UriBuilder $uriBuilder
Definition: RecoveryService.php:59
‪TYPO3\CMS\FrontendLogin\Service\RecoveryService\$eventDispatcher
‪EventDispatcherInterface $eventDispatcher
Definition: RecoveryService.php:47
‪TYPO3\CMS\FrontendLogin\Event\SendRecoveryEmailEvent
Definition: SendRecoveryEmailEvent.php:29
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\FrontendLogin\Service\RecoveryServiceInterface
Definition: RecoveryServiceInterface.php:21