[FEATURE] Execute hooks on backend user login 19/55319/7
authorMichael Schams <typo3@2018.schams.net>
Fri, 19 Jan 2018 05:27:57 +0000 (16:27 +1100)
committerChristian Kuhn <lolli@schwarzbu.ch>
Mon, 22 Jan 2018 20:59:49 +0000 (21:59 +0100)
When a user successfully logs in to the backend of TYPO3, registered
hooks are executed. This allows for the development of further actions
such as notification services.

Resolves: #83529
Releases: master
Change-Id: I26b060463ae76075a4d40f25c8e695534fe69875
Reviewed-on: https://review.typo3.org/55319
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Michael Schams <typo3@2018.schams.net>
Tested-by: Michael Schams <typo3@2018.schams.net>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Tested-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Henning Liebe <h.liebe@neusta.de>
Tested-by: Henning Liebe <h.liebe@neusta.de>
Reviewed-by: Reiner Teubner <reiner.teubner@me.com>
Tested-by: Reiner Teubner <reiner.teubner@me.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/core/Classes/Authentication/BackendUserAuthentication.php
typo3/sysext/core/Documentation/Changelog/master/Feature-83529-ExecuteHooksOnBackendUserLogin.rst [new file with mode: 0644]

index af7c86a..638abb8 100644 (file)
@@ -2516,8 +2516,16 @@ This is a dump of the failures:
                 if ($this->isUserAllowedToLogin()) {
                     // Setting the UC array. It's needed with fetchGroupData first, due to default/overriding of values.
                     $this->backendSetUC();
-                    // Email at login - if option set.
-                    $this->emailAtLogin();
+                    if ($this->loginSessionStarted) {
+                        // Process hooks
+                        $hooks = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['backendUserLogin'];
+                        foreach ($hooks ?? [] as $_funcRef) {
+                            $_params = ['user' => $this->user];
+                            GeneralUtility::callUserFunction($_funcRef, $_params, $this);
+                        }
+                        // Email at login, if feature is enabled in configuration
+                        $this->emailAtLogin();
+                    }
                 } else {
                     throw new \RuntimeException('Login Error: TYPO3 is in maintenance mode at the moment. Only administrators are allowed access.', 1294585860);
                 }
@@ -2600,55 +2608,52 @@ This is a dump of the failures:
     }
 
     /**
-     * Will send an email notification to warning_email_address/the login users email address when a login session is just started.
-     * Depends on various parameters whether mails are send and to whom.
+     * Sends an email notification to warning_email_address and/or the logged-in user's email address.
      *
      * @access private
      */
     private function emailAtLogin()
     {
-        if ($this->loginSessionStarted) {
-            // Send notify-mail
-            $subject = 'At "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '"' . ' from '
-                . GeneralUtility::getIndpEnv('REMOTE_ADDR')
-                . (GeneralUtility::getIndpEnv('REMOTE_HOST') ? ' (' . GeneralUtility::getIndpEnv('REMOTE_HOST') . ')' : '');
-            $msg = sprintf(
-                'User "%s" logged in from %s (%s) at "%s" (%s)',
-                $this->user['username'],
-                GeneralUtility::getIndpEnv('REMOTE_ADDR'),
-                GeneralUtility::getIndpEnv('REMOTE_HOST'),
-                $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
-                GeneralUtility::getIndpEnv('HTTP_HOST')
-            );
-            // Warning email address
-            if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
-                $warn = 0;
-                $prefix = '';
-                if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 1) {
-                    // first bit: All logins
-                    $warn = 1;
-                    $prefix = $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
-                }
-                if ($this->isAdmin() && (int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 2) {
-                    // second bit: Only admin-logins
-                    $warn = 1;
-                    $prefix = '[AdminLoginWarning]';
-                }
-                if ($warn) {
-                    /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
-                    $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
-                    $mail->setTo($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'])->setSubject($prefix . ' ' . $subject)->setBody($msg);
-                    $mail->send();
-                }
+        // Send notify-mail
+        $subject = 'At "' . $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '"' . ' from '
+            . GeneralUtility::getIndpEnv('REMOTE_ADDR')
+            . (GeneralUtility::getIndpEnv('REMOTE_HOST') ? ' (' . GeneralUtility::getIndpEnv('REMOTE_HOST') . ')' : '');
+        $msg = sprintf(
+            'User "%s" logged in from %s (%s) at "%s" (%s)',
+            $this->user['username'],
+            GeneralUtility::getIndpEnv('REMOTE_ADDR'),
+            GeneralUtility::getIndpEnv('REMOTE_HOST'),
+            $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'],
+            GeneralUtility::getIndpEnv('HTTP_HOST')
+        );
+        // Warning email address
+        if ($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr']) {
+            $warn = 0;
+            $prefix = '';
+            if ((int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 1) {
+                // first bit: All logins
+                $warn = 1;
+                $prefix = $this->isAdmin() ? '[AdminLoginWarning]' : '[LoginWarning]';
             }
-            // If An email should be sent to the current user, do that:
-            if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) {
+            if ($this->isAdmin() && (int)$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_mode'] & 2) {
+                // second bit: Only admin-logins
+                $warn = 1;
+                $prefix = '[AdminLoginWarning]';
+            }
+            if ($warn) {
                 /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
                 $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
-                $mail->setTo($this->user['email'])->setSubject($subject)->setBody($msg);
+                $mail->setTo($GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'])->setSubject($prefix . ' ' . $subject)->setBody($msg);
                 $mail->send();
             }
         }
+        // Trigger an email to the current BE user, if this has been enabled in the user configuration
+        if ($this->uc['emailMeAtLogin'] && strstr($this->user['email'], '@')) {
+            /** @var $mail \TYPO3\CMS\Core\Mail\MailMessage */
+            $mail = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class);
+            $mail->setTo($this->user['email'])->setSubject($subject)->setBody($msg);
+            $mail->send();
+        }
     }
 
     /**
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-83529-ExecuteHooksOnBackendUserLogin.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-83529-ExecuteHooksOnBackendUserLogin.rst
new file mode 100644 (file)
index 0000000..1077c1e
--- /dev/null
@@ -0,0 +1,43 @@
+.. include:: ../../Includes.txt
+
+=====================================================
+Feature: #83529 - Execute hooks on backend user login
+=====================================================
+
+See :issue:`83529`
+
+Description
+===========
+
+When a user successfully logs in to the backend of TYPO3, registered hooks are executed.
+Developers can register their hooks as shown below.
+
+.. code-block:: php
+
+   // Register hook on successful BE user login
+   $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauthgroup.php']['backendUserLogin'][] =
+       \Vendor\MyExtension\Hooks\BackendUserLogin::class . '->dispatch';
+
+On user login, method :php:`dispatch()` of class :php:`\Vendor\MyExtension\Hooks\BackendUserLogin`
+is executed and the backend user array is passed as a parameter:
+
+.. code-block:: php
+
+    public function dispatch($backendUser)
+    {
+      if (isset($backendUser['user']['username'])) {
+        $username = $backendUser['user']['username'];
+        $email = $backendUser['user']['email'];
+        // do something...
+      }
+    }
+
+
+Impact
+======
+
+TYPO3 core developers as well as extension developers can develop functions which will be executed
+when a backend user successfully logs in to the backend of TYPO3.
+A typical use case would be any type of notification service.
+
+.. index:: NotScanned