‪TYPO3CMS  11.5
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  $genericLoginWarning = $this->warningMode > 0 && !empty($this->warningEmailRecipient);
77  $userLoginNotification = ($currentUser->uc['emailMeAtLogin'] ?? null) && GeneralUtility::validEmail($user['email']);
78  if (!$genericLoginWarning && !$userLoginNotification) {
79  return;
80  }
81  $this->request = $parameters['request'] ?? ‪$GLOBALS['TYPO3_REQUEST'] ?? ‪ServerRequestFactory::fromGlobals();
82 
83  if ($genericLoginWarning) {
84  $prefix = $currentUser->‪isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
85  if ($this->warningMode & 1) {
86  // First bit: Send warning email on any login
87  $this->‪sendEmail($this->warningEmailRecipient, $currentUser, $prefix);
88  } elseif ($currentUser->‪isAdmin() && $this->warningMode & 2) {
89  // Second bit: Only send warning email when an admin logs in
90  $this->‪sendEmail($this->warningEmailRecipient, $currentUser, $prefix);
91  }
92  }
93  // Trigger an email to the current BE user, if this has been enabled in the user configuration
94  if ($userLoginNotification) {
95  $this->‪sendEmail($user['email'], $currentUser);
96  }
97  }
98 
106  protected function ‪sendEmail(string $recipient, ‪AbstractUserAuthentication $user, ?string $subjectPrefix = null): void
107  {
108  $headline = 'TYPO3 Backend Login notification';
109  $recipients = explode(',', $recipient);
110  $email = GeneralUtility::makeInstance(FluidEmail::class)
111  ->to(...$recipients)
112  ->setRequest($this->request)
113  ->setTemplate('Security/LoginNotification')
114  ->assignMultiple([
115  'user' => $user->user,
116  'prefix' => $subjectPrefix,
117  'language' => ($user->user['lang'] ?? '') ?: 'default',
118  'headline' => $headline,
119  ]);
120  try {
121  GeneralUtility::makeInstance(Mailer::class)->send($email);
122  } catch (TransportException $e) {
123  $this->logger->warning('Could not send notification email to "{recipient}" due to mailer settings error', [
124  'recipient' => $recipient,
125  'userId' => $user->user['uid'] ?? 0,
126  'recipientList' => $recipients,
127  'exception' => $e,
128  ]);
129  } catch (RfcComplianceException $e) {
130  $this->logger->warning('Could not send notification email to "{recipient}" due to invalid email address', [
131  'recipient' => $recipient,
132  'userId' => $user->user['uid'] ?? 0,
133  'recipientList' => $recipients,
134  'exception' => $e,
135  ]);
136  }
137  }
138 }
‪TYPO3\CMS\Backend\Security\EmailLoginNotification\$request
‪ServerRequestInterface $request
Definition: EmailLoginNotification.php:56
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication\isAdmin
‪bool isAdmin()
Definition: BackendUserAuthentication.php:245
‪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:103
‪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:25
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56
‪TYPO3\CMS\Core\Http\ServerRequestFactory\fromGlobals
‪static ServerRequest fromGlobals()
Definition: ServerRequestFactory.php:59