[TASK] Move update functionality to BackendUserSessionRepository 87/28487/9
authorPascal Dürsteler <pascal.duersteler@gmail.com>
Tue, 18 Mar 2014 08:45:37 +0000 (09:45 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Sun, 23 Mar 2014 18:01:09 +0000 (19:01 +0100)
As annotated in SwitchBackUserHook.php, the update method is moved
into the BackendUserSessionRepository file.

Resolves: #57012
Releases: 6.2
Change-Id: I5e127b8e71585e64bd99ae3175655b058162da83
Reviewed-on: https://review.typo3.org/28487
Reviewed-by: Wouter Wolters
Reviewed-by: Philipp Gampe
Tested-by: Philipp Gampe
Reviewed-by: Pascal Dürsteler
Tested-by: Pascal Dürsteler
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
typo3/sysext/beuser/Classes/Domain/Repository/BackendUserSessionRepository.php
typo3/sysext/beuser/Classes/Hook/SwitchBackUserHook.php

index 03a3326..7c1a418 100644 (file)
@@ -27,9 +27,10 @@ namespace TYPO3\CMS\Beuser\Domain\Repository;
  ***************************************************************/
 
 /**
- * Repository for Tx_Beuser_Domain_Model_BackendUser
+ * Repository for \TYPO3\CMS\Extbase\Domain\Model\BackendUser
  *
  * @author Felix Kopp <felix-source@phorax.com>
+ * @author Pascal Dürsteler <pascal@notionlab.ch>
  */
 class BackendUserSessionRepository extends \TYPO3\CMS\Extbase\Persistence\Repository {
 
@@ -60,4 +61,23 @@ class BackendUserSessionRepository extends \TYPO3\CMS\Extbase\Persistence\Reposi
                return $sessions;
        }
 
+       /**
+        * Update current session to move back to the original user.
+        *
+        * @param \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication $authentication
+        * @return void
+        */
+       public function switchBackToOriginalUser(\TYPO3\CMS\Core\Authentication\AbstractUserAuthentication $authentication) {
+               $updateData = array(
+                       'ses_userid' => $authentication->user['ses_backuserid'],
+                       'ses_backuserid' => 0,
+               );
+               $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
+                       'be_sessions',
+                       'ses_id = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['BE_USER']->id, 'be_sessions') .
+                               ' AND ses_name = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::getCookieName(), 'be_sessions') .
+                               ' AND ses_userid=' . (int)$GLOBALS['BE_USER']->user['uid'], $updateData
+               );
+       }
+
 }
index e9e7057..9ab6b98 100644 (file)
@@ -24,6 +24,8 @@ namespace TYPO3\CMS\Beuser\Hook;
  *  This copyright notice MUST APPEAR in all copies of the script!
  ***************************************************************/
 
+use TYPO3\CMS\Core\Authentication\AbstractUserAuthentication;
+
 /**
  * Backend user switchback, for logoff_pre_processing hook within
  * \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication class
@@ -31,41 +33,47 @@ namespace TYPO3\CMS\Beuser\Hook;
  * @author Kasper Skårhøj (kasperYYYY@typo3.com)
  * @author Sebastian Kurfürst <sebastian@garbage-group.de>
  * @author Felix Kopp <felix-source@phorax.com>
+ * @author Pascal Dürsteler <pascal@notionlab.ch>
  */
 class SwitchBackUserHook {
 
        /**
-        * Switch backend user session
+        * Switch backend user session.
         *
         * @param array $params
-        * @param \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication $that
-        * @see \TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
+        * @param AbstractUserAuthentication $authentication
+        * @see AbstractUserAuthentication
+        * @return void
+        */
+       public function switchBack($params, AbstractUserAuthentication $authentication) {
+               if ($this->isAHandledBackendSession($authentication)) {
+                       $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
+                       $backendUserSessionRepository = $objectManager->get('TYPO3\\CMS\\Beuser\\Domain\\Repository\\BackendUserSessionRepository');
+                       $backendUserSessionRepository->switchBackToOriginalUser($authentication);
+
+                       $redirectUrl = $GLOBALS['BACK_PATH'] . 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
+                       \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
+               }
+       }
+
+       /**
+        * Check if the given authentication object is a backend session and
+        * contains all necessary information to allow switching.
+        *
+        * @param AbstractUserAuthentication $authentication
+        * @return bool
         */
-       public function switchBack($params, $that) {
-               // Is a backend session handled?
+       protected function isAHandledBackendSession(AbstractUserAuthentication $authentication) {
                if (
-                       $that->session_table !== 'be_sessions'
-                       || !is_array($that->user)
-                       || !$that->user['uid']
-                       || !$that->user['ses_backuserid']
+                       $authentication->session_table !== 'be_sessions'
+                       || !is_array($authentication->user)
+                       || !$authentication->user['uid']
+                       || !$authentication->user['ses_backuserid']
                ) {
-                       return;
+                       return FALSE;
+               } else {
+                       return TRUE;
                }
-
-               // @TODO: Move update functionality to Tx_Beuser_Domain_Repository_BackendUserSessionRepository
-               $updateData = array(
-                       'ses_userid' => $that->user['ses_backuserid'],
-                       'ses_backuserid' => 0
-               );
-               $GLOBALS['TYPO3_DB']->exec_UPDATEquery(
-                       'be_sessions',
-                       'ses_id = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($GLOBALS['BE_USER']->id, 'be_sessions') .
-                               ' AND ses_name = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr(\TYPO3\CMS\Core\Authentication\BackendUserAuthentication::getCookieName(), 'be_sessions') .
-                               ' AND ses_userid=' . (int)$GLOBALS['BE_USER']->user['uid'], $updateData
-               );
-
-               $redirectUrl = $GLOBALS['BACK_PATH'] . 'index.php' . ($GLOBALS['TYPO3_CONF_VARS']['BE']['interfaces'] ? '' : '?commandLI=1');
-               \TYPO3\CMS\Core\Utility\HttpUtility::redirect($redirectUrl);
        }
 
-}
+}
\ No newline at end of file