‪TYPO3CMS  10.4
EmailLoginNotification.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\Http\Message\ServerRequestInterface;
21 use Psr\Log\LoggerAwareInterface;
22 use Psr\Log\LoggerAwareTrait;
23 use Symfony\Component\Mailer\Exception\TransportException;
24 use Symfony\Component\Mime\Exception\RfcComplianceException;
31 
42 class ‪EmailLoginNotification implements LoggerAwareInterface
43 {
44  use LoggerAwareTrait;
45 
49  private ‪$warningMode;
50 
55 
59  private ‪$request;
60 
61  public function ‪__construct()
62  {
63  $this->warningMode = (int)(‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] ?? 0);
64  $this->warningEmailRecipient = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'] ?? '';
65  }
66 
73  public function ‪emailAtLogin(array $parameters, ‪BackendUserAuthentication $currentUser): void
74  {
75  $user = $parameters['user'];
76  $this->request = $parameters['request'] ?? ‪$GLOBALS['TYPO3_REQUEST'] ?? ‪ServerRequestFactory::fromGlobals();
77 
78  if ($this->warningMode > 0 && !empty($this->warningEmailRecipient)) {
79  $prefix = $currentUser->‪isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
80  if ($this->warningMode & 1) {
81  // First bit: Send warning email on any login
82  $this->‪sendEmail($this->warningEmailRecipient, $currentUser, $prefix);
83  } elseif ($currentUser->‪isAdmin() && $this->warningMode & 2) {
84  // Second bit: Only send warning email when an admin logs in
85  $this->‪sendEmail($this->warningEmailRecipient, $currentUser, $prefix);
86  }
87  }
88  // Trigger an email to the current BE user, if this has been enabled in the user configuration
89  if (($currentUser->uc['emailMeAtLogin'] ?? null) && GeneralUtility::validEmail($user['email'])) {
90  $this->‪sendEmail($user['email'], $currentUser);
91  }
92  }
93 
101  protected function ‪sendEmail(string $recipient, ‪AbstractUserAuthentication $user, string $subjectPrefix = null): void
102  {
103  $headline = 'TYPO3 Backend Login notification';
104  $recipients = explode(',', $recipient);
105  $email = GeneralUtility::makeInstance(FluidEmail::class)
106  ->to(...$recipients)
107  ->setRequest($this->request)
108  ->setTemplate('Security/LoginNotification')
109  ->assignMultiple([
110  'user' => $user->user,
111  'prefix' => $subjectPrefix,
112  'language' => $user->uc['lang'] ?? 'default',
113  'headline' => $headline
114  ]);
115  try {
116  GeneralUtility::makeInstance(Mailer::class)->send($email);
117  } catch (TransportException $e) {
118  $this->logger->warning('Could not send notification email to "' . $recipient . '" due to mailer settings error', [
119  'userId' => $user->user['uid'] ?? 0,
120  'recipientList' => $recipients,
121  'exception' => $e
122  ]);
123  } catch (RfcComplianceException $e) {
124  $this->logger->warning('Could not send notification email to "' . $recipient . '" due to invalid email address', [
125  'userId' => $user->user['uid'] ?? 0,
126  'recipientList' => $recipients,
127  'exception' => $e
128  ]);
129  }
130  }
131 }
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\$request
‪ServerRequestInterface $request
Definition: EmailLoginNotification.php:56
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:292
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\__construct
‪__construct()
Definition: EmailLoginNotification.php:58
‪TYPO3\CMS\Backend\Security\EmailLoginNotification
Definition: EmailLoginNotification.php:43
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\sendEmail
‪sendEmail(string $recipient, AbstractUserAuthentication $user, string $subjectPrefix=null)
Definition: EmailLoginNotification.php:98
‪TYPO3\CMS\Core\Http\ServerRequestFactory
Definition: ServerRequestFactory.php:34
‪TYPO3\CMS\Backend\Security
Definition: CategoryPermissionsAspect.php:16
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\emailAtLogin
‪emailAtLogin(array $parameters, BackendUserAuthentication $currentUser)
Definition: EmailLoginNotification.php:70
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Core\Mail\Mailer
Definition: Mailer.php:38
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\$warningMode
‪int $warningMode
Definition: EmailLoginNotification.php:48
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\$warningEmailRecipient
‪string $warningEmailRecipient
Definition: EmailLoginNotification.php:52
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59