[TASK] Frontend: BE User Initialization should be encapsulated
authorBenjamin Mack <benni@typo3.org>
Wed, 6 Jul 2011 15:06:37 +0000 (17:06 +0200)
committerOliver Hader <oliver@typo3.org>
Thu, 7 Jul 2011 14:37:10 +0000 (16:37 +0200)
The handling of the Backend User initialization should be encapsulated
into TSFE.
This way, it's easier to extend it - and to exchange this code.

Change-Id: If28fcb7123ed0fc3bd34682e4a686eecb5178d3b
Resolves: #27917
Releases: 4.6
Reviewed-on: http://review.typo3.org/3051
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
typo3/sysext/cms/tslib/class.tslib_fe.php
typo3/sysext/cms/tslib/index_ts.php

index 6f0c968..c292462 100644 (file)
        }
 
        /**
-        * Looks up the value of $this->RDCT in the database and if it is found to be associated with a redirect URL then the redirection is carried out with a 'Location:' header
+        * Looks up the value of $this->RDCT in the database and if it is
+        * found to be associated with a redirect URL then the redirection
+        * is carried out with a 'Location:' header
         * May exit after sending a location-header.
         *
-        * @return      void
+        * @return void
         */
-       function sendRedirect() {
-               $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('params', 'cache_md5params', 'md5hash='.$GLOBALS['TYPO3_DB']->fullQuoteStr($this->RDCT, 'cache_md5params'));
-               if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
-                       $this->updateMD5paramsRecord($this->RDCT);
-                       header('Location: '.$row['params']);
-                       exit;
+       public function sendRedirect() {
+               if ($this->RDCT) {
+                       $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
+                               'params',
+                               'cache_md5params',
+                               'md5hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($this->RDCT, 'cache_md5params')
+                       );
+                       if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
+                               $this->updateMD5paramsRecord($this->RDCT);
+                               header('Location: ' . $row['params']);
+                               exit;
+                       }
                }
        }
 
                $this->fePreview = 0;
        }
 
+       /**
+        * Checks if a backend user is logged in
+        *
+        * @return boolean whether a backend user is logged in
+        */
+       public function isBackendUserLoggedIn() {
+               return ($this->beUserLogin ? TRUE : FALSE);
+       }
+
+
+       /**
+        * Creates the backend user object and returns it.
+        *
+        * @return t3lib_tsfeBeUserAuth the backend user object
+        */
+       public function initializeBackendUser() {
+               // ****************
+               // PRE BE_USER HOOK
+               // ****************
+               if (is_array($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preBeUser'])) {
+                       foreach($this->TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preBeUser'] as $_funcRef) {
+                               $_params = array();
+                               t3lib_div::callUserFunction($_funcRef, $_params, $_params);
+                       }
+               }
+
+               /** @var $BE_USER t3lib_tsfeBeUserAuth */
+               $BE_USER = NULL;
+
+                       // If the backend cookie is set,
+                       // we proceed and check if a backend user is logged in.
+               if ($_COOKIE[t3lib_beUserAuth::getCookieName()]) {
+                       $GLOBALS['TYPO3_MISC']['microtime_BE_USER_start'] = microtime(TRUE);
+                       $GLOBALS['TT']->push('Back End user initialized', '');
+
+                               // TODO: validate the comment below: is this necessary? if so,
+                               // formfield_status should be set to "" in t3lib_tsfeBeUserAuth
+                               // which is a subclass of t3lib_beUserAuth
+                               // ----
+                               // the value this->formfield_status is set to empty in order to
+                               // disable login-attempts to the backend account through this script
+
+                               // New backend user object
+                       $BE_USER = t3lib_div::makeInstance('t3lib_tsfeBeUserAuth');
+                       $BE_USER->OS = TYPO3_OS;
+                       $BE_USER->lockIP = $this->TYPO3_CONF_VARS['BE']['lockIP'];
+
+                               // Object is initialized
+                       $BE_USER->start();
+                       $BE_USER->unpack_uc('');
+                       if ($BE_USER->user['uid']) {
+                               $BE_USER->fetchGroupData();
+                               $this->beUserLogin = 1;
+                       }
+                               // Unset the user initialization.
+                       if (!$BE_USER->checkLockToIP() || !$BE_USER->checkBackendAccessSettingsFromInitPhp() || !$BE_USER->user['uid']) {
+                               $BE_USER = NULL;
+                               $this->beUserLogin = 0;
+                               $_SESSION['TYPO3-TT-start'] = FALSE;
+                       }
+                       $GLOBALS['TT']->pull();
+                       $GLOBALS['TYPO3_MISC']['microtime_BE_USER_end'] = microtime(TRUE);
+
+               } elseif ($this->ADMCMD_preview_BEUSER_uid) {
+                               // TODO: validate the comment below: is this necessary? if so,
+                               // formfield_status should be set to "" in t3lib_tsfeBeUserAuth
+                               // which is a subclass of t3lib_beUserAuth
+                               // ----
+                               // the value this->formfield_status is set to empty in order to
+                               // disable login-attempts to the backend account through this script
+
+                               // New backend user object
+                       $BE_USER = t3lib_div::makeInstance('t3lib_tsfeBeUserAuth');
+                       $BE_USER->userTS_dontGetCached = 1;
+                       $BE_USER->OS = TYPO3_OS;
+                       $BE_USER->setBeUserByUid($this->ADMCMD_preview_BEUSER_uid);
+                       $BE_USER->unpack_uc('');
+                       if ($BE_USER->user['uid']) {
+                               $BE_USER->fetchGroupData();
+                               $this->beUserLogin = 1;
+                       } else {
+                               $BE_USER = NULL;
+                               $this->beUserLogin = 0;
+                               $_SESSION['TYPO3-TT-start'] = FALSE;
+                       }
+               }
+
+               return $BE_USER;
+       }
+
        /**
         * Determines the id and evaluates any preview settings
         * Basically this function is about determining whether a backend user is logged in, if he has read access to the page and if he's previewing the page. That all determines which id to show and how to initialize the id.
 
 
 
-
        /********************************************
         *
         * Template and caching related functions.
index 642fc24..98ca906 100644 (file)
@@ -241,7 +241,8 @@ if ($temp_previewConfig = $TSFE->ADMCMD_preview())  {
        $TSFE->ADMCMD_preview_postInit($temp_previewConfig);
 }
 
-if ($TSFE->RDCT)       {$TSFE->sendRedirect();}
+
+$TSFE->sendRedirect();
 
 
 // *******************
@@ -265,61 +266,13 @@ $TT->push('Front End user initialized','');
        $TSFE->initFEuser();
 $TT->pull();
 
-// ****************
-// PRE BE_USER HOOK
-// ****************
-if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preBeUser'])) {
-       foreach($TYPO3_CONF_VARS['SC_OPTIONS']['tslib/index_ts.php']['preBeUser'] as $_funcRef) {
-               $_params = array();
-               t3lib_div::callUserFunction($_funcRef, $_params , $_params);
-       }
-}
-
 
 // *********
 // BE_USER
 // *********
-$BE_USER = NULL;
 /** @var $BE_USER t3lib_tsfeBeUserAuth */
-if ($_COOKIE[t3lib_beUserAuth::getCookieName()]) {             // If the backend cookie is set, we proceed and checks if a backend user is logged in.
-       $TYPO3_MISC['microtime_BE_USER_start'] = microtime(TRUE);
-       $TT->push('Back End user initialized','');
-
-                       // the value this->formfield_status is set to empty in order to disable login-attempts to the backend account through this script
-               $BE_USER = t3lib_div::makeInstance('t3lib_tsfeBeUserAuth');     // New backend user object
-               $BE_USER->OS = TYPO3_OS;
-               $BE_USER->lockIP = $TYPO3_CONF_VARS['BE']['lockIP'];
-               $BE_USER->start();                      // Object is initialized
-               $BE_USER->unpack_uc('');
-               if ($BE_USER->user['uid'])      {
-                       $BE_USER->fetchGroupData();
-                       $TSFE->beUserLogin = 1;
-               }
-                       // Unset the user initialization.
-               if (!$BE_USER->checkLockToIP() || !$BE_USER->checkBackendAccessSettingsFromInitPhp() || !$BE_USER->user['uid']) {
-                       $BE_USER = NULL;
-                       $TSFE->beUserLogin=0;
-                       $_SESSION['TYPO3-TT-start'] = FALSE;
-               }
-       $TT->pull();
-       $TYPO3_MISC['microtime_BE_USER_end'] = microtime(TRUE);
-} elseif ($TSFE->ADMCMD_preview_BEUSER_uid)    {
-
-               // the value this->formfield_status is set to empty in order to disable login-attempts to the backend account through this script
-       $BE_USER = t3lib_div::makeInstance('t3lib_tsfeBeUserAuth');     // New backend user object
-       $BE_USER->userTS_dontGetCached = 1;
-       $BE_USER->OS = TYPO3_OS;
-       $BE_USER->setBeUserByUid($TSFE->ADMCMD_preview_BEUSER_uid);
-       $BE_USER->unpack_uc('');
-       if ($BE_USER->user['uid'])      {
-               $BE_USER->fetchGroupData();
-               $TSFE->beUserLogin = 1;
-       } else {
-               $BE_USER = NULL;
-               $TSFE->beUserLogin = 0;
-               $_SESSION['TYPO3-TT-start'] = FALSE;
-       }
-}
+$BE_USER = $TSFE->initializeBackendUser();
+
 
 // ********************
 // Workspace preview:
@@ -333,7 +286,7 @@ $TSFE->workspacePreviewInit();
 // *****************************************
 $TT->push('Process ID','');
                // Initialize admin panel since simulation settings are required here:
-       if ($TSFE->beUserLogin) {
+       if ($TSFE->isBackendUserLoggedIn()) {
                $BE_USER->initializeAdminPanel();
        }
 
@@ -342,7 +295,7 @@ $TT->push('Process ID','');
        $TSFE->determineId();
 
                // Now, if there is a backend user logged in and he has NO access to this page, then re-evaluate the id shown!
-       if ($TSFE->beUserLogin && (!$BE_USER->extPageReadAccess($TSFE->page) || t3lib_div::_GP('ADMCMD_noBeUser')))     {       // t3lib_div::_GP('ADMCMD_noBeUser') is placed here because workspacePreviewInit() might need to know if a backend user is logged in!
+       if ($TSFE->isBackendUserLoggedIn() && (!$BE_USER->extPageReadAccess($TSFE->page) || t3lib_div::_GP('ADMCMD_noBeUser'))) {       // t3lib_div::_GP('ADMCMD_noBeUser') is placed here because workspacePreviewInit() might need to know if a backend user is logged in!
 
                        // Remove user
                unset($BE_USER);
@@ -359,7 +312,7 @@ $TT->pull();
 // *****************************************
 // Admin Panel & Frontend editing
 // *****************************************
-if ($TSFE->beUserLogin) {
+if ($TSFE->isBackendUserLoggedIn()) {
                // if a BE User is present load, the sprite manager for frontend-editing
        $spriteManager = t3lib_div::makeInstance('t3lib_SpriteManager', FALSE);
        $spriteManager->loadCacheFile();
@@ -582,7 +535,7 @@ echo $TSFE->beLoginLinkIPList();
 // *************
 // Admin panel
 // *************
-if (is_object($BE_USER) && $BE_USER->isAdminPanelVisible() && $TSFE->beUserLogin) {
+if (is_object($BE_USER) && $BE_USER->isAdminPanelVisible() && $TSFE->isBackendUserLoggedIn()) {
        $TSFE->content = str_ireplace('</head>',  $BE_USER->adminPanel->getAdminPanelHeaderData() . '</head>', $TSFE->content);
        $TSFE->content = str_ireplace('</body>',  $BE_USER->displayAdminPanel() . '</body>', $TSFE->content);
 }