From 3dac41fff91db3c5494f6b6b67d9c7fce7c50db8 Mon Sep 17 00:00:00 2001
From: Benni Mack <benni@typo3.org>
Date: Tue, 18 Feb 2020 19:59:52 +0100
Subject: [PATCH] [TASK] Allow subject to be handled in EmailLoginNotification
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The subject can also be defined in a templated email. This way,
it is possible to allow custom subjects and completely modify
the login notification email.

Resolves: #90423
Releases: master
Change-Id: Idc904c8f06a7ffc2e491a2eb283ae6ba8f9dc00b
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/63313
Tested-by: Susanne Moog <look@susi.dev>
Tested-by: Daniel Goerz <daniel.goerz@posteo.de>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
Reviewed-by: Susanne Moog <look@susi.dev>
Reviewed-by: Daniel Goerz <daniel.goerz@posteo.de>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
---
 .../backend/Classes/Security/EmailLoginNotification.php  | 9 ++++++---
 .../Templates/Email/Security/LoginNotification.html      | 1 +
 .../Templates/Email/Security/LoginNotification.txt       | 1 +
 .../Tests/Unit/Security/EmailLoginNotificationTest.php   | 4 ----
 .../master/Feature-90266-Fluid-basedTemplatedEmails.rst  | 2 ++
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/backend/Classes/Security/EmailLoginNotification.php b/typo3/sysext/backend/Classes/Security/EmailLoginNotification.php
index 8f3f77a3c9ae..440a1d573eac 100644
--- a/typo3/sysext/backend/Classes/Security/EmailLoginNotification.php
+++ b/typo3/sysext/backend/Classes/Security/EmailLoginNotification.php
@@ -93,15 +93,18 @@ class EmailLoginNotification
      */
     protected function sendEmail(string $recipient, AbstractUserAuthentication $user, string $subjectPrefix = null): void
     {
-        $subject = $subjectPrefix . ' New TYPO3 Login at "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '" from ' . GeneralUtility::getIndpEnv('REMOTE_ADDR');
         $headline = 'TYPO3 Backend Login notification';
         $recipients = explode(',', $recipient);
         $email = GeneralUtility::makeInstance(FluidEmail::class)
             ->to(...$recipients)
-            ->subject($subject)
             ->setRequest($this->request)
             ->setTemplate('Security/LoginNotification')
-            ->assignMultiple(['user' => $user->user, 'headline' => $headline]);
+            ->assignMultiple([
+                'user' => $user->user,
+                'prefix' => $subjectPrefix,
+                'language' => $user->uc['lang'] ?? 'default',
+                'headline' => $headline
+            ]);
         GeneralUtility::makeInstance(Mailer::class)->send($email);
     }
 }
diff --git a/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.html b/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.html
index 7e0db4d42a55..36e8c2d1eb99 100644
--- a/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.html
+++ b/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.html
@@ -1,4 +1,5 @@
 <f:layout name="SystemEmail" />
+<f:section name="Subject">{prefix} New Login at "{typo3.sitename}"</f:section>
 <f:section name="Title">{headline}</f:section>
 <f:section name="Main">
     <h4>{introduction}</h4>
diff --git a/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.txt b/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.txt
index 2f991f4e65b4..0041e8a1e3fd 100644
--- a/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.txt
+++ b/typo3/sysext/backend/Resources/Private/Templates/Email/Security/LoginNotification.txt
@@ -1,4 +1,5 @@
 <f:layout name="SystemEmail" />
+<f:section name="Subject">{prefix} New Login at "{typo3.sitename}"</f:section>
 <f:section name="Title">{headline}</f:section>
 <f:section name="Main">{introduction}
 The user "{user.username}" logged in from the IP address "{normalizedParams.remoteAddress}" at the site "{typo3.sitename}".
diff --git a/typo3/sysext/backend/Tests/Unit/Security/EmailLoginNotificationTest.php b/typo3/sysext/backend/Tests/Unit/Security/EmailLoginNotificationTest.php
index bf898e899b42..ba24b6e7a6b6 100644
--- a/typo3/sysext/backend/Tests/Unit/Security/EmailLoginNotificationTest.php
+++ b/typo3/sysext/backend/Tests/Unit/Security/EmailLoginNotificationTest.php
@@ -128,7 +128,6 @@ class EmailLoginNotificationTest extends UnitTestCase
         $subject->emailAtLogin(['user' => $userData], $backendUser);
 
         $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
-        $mailMessage->subject('[AdminLoginWarning] New TYPO3 Login at "My TYPO3 Inc." from 127.0.0.1')->shouldHaveBeenCalled();
     }
 
     /**
@@ -158,7 +157,6 @@ class EmailLoginNotificationTest extends UnitTestCase
         $subject = new EmailLoginNotification();
         $subject->emailAtLogin(['user' => $userData], $backendUser);
 
-        $mailMessage->subject('[AdminLoginWarning] New TYPO3 Login at "My TYPO3 Inc." from 127.0.0.1')->shouldHaveBeenCalled();
         $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
     }
 
@@ -190,7 +188,6 @@ class EmailLoginNotificationTest extends UnitTestCase
         $subject->emailAtLogin(['user' => $userData], $backendUser);
 
         $mailMessage->to('typo3-admin@acme.com')->shouldHaveBeenCalled();
-        $mailMessage->subject('[LoginWarning] New TYPO3 Login at "My TYPO3 Inc." from 127.0.0.1')->shouldHaveBeenCalled();
     }
 
     /**
@@ -224,7 +221,6 @@ class EmailLoginNotificationTest extends UnitTestCase
     protected function setUpMailMessageProphecy()
     {
         $mailMessage = $this->prophesize(FluidEmail::class);
-        $mailMessage->subject(Argument::any())->willReturn($mailMessage->reveal());
         $mailMessage->to(Argument::any())->willReturn($mailMessage->reveal());
         $mailMessage->setTemplate(Argument::any())->willReturn($mailMessage->reveal());
         $mailMessage->from(Argument::any())->willReturn($mailMessage->reveal());
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-90266-Fluid-basedTemplatedEmails.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-90266-Fluid-basedTemplatedEmails.rst
index 10fb163b9b9c..b0fb32ddd42d 100644
--- a/typo3/sysext/core/Documentation/Changelog/master/Feature-90266-Fluid-basedTemplatedEmails.rst
+++ b/typo3/sysext/core/Documentation/Changelog/master/Feature-90266-Fluid-basedTemplatedEmails.rst
@@ -31,6 +31,8 @@ set this in your LocalConfiguration.php / AdditionalConfiguration.php file:
 * :php:`$GLOBALS['TYPO3_CONF_VARS']['MAIL']['templateRootPaths'][700] = 'EXT:my_site_extension/Resources/Private/Templates/Email';`
 * :php:`$GLOBALS['TYPO3_CONF_VARS']['MAIL']['layoutRootPaths'][700] = 'EXT:my_site_extension/Resources/Private/Layouts';`
 
+In addition, it is possible to define a section within the Fluid email,
+which - if defined - takes precedence over the :php:`subject()` method.
 
 In addition, it is possible to define a section within the Fluid email,
 which - if set - takes precedence over the :php:`subject()` method.
-- 
GitLab