[BUGFIX] Add possibility to use session data in TS conditions 08/55408/12
authorWolfgang Klinger <wolfgang@wazum.com>
Tue, 9 Jan 2018 19:17:56 +0000 (20:17 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Sun, 4 Feb 2018 21:48:43 +0000 (22:48 +0100)
Bring back the possibility to access FE session data in
TypoScript conditions:
[globalVar = TSFE:fe_user|sesData|some_key > 10]

Resolves: #83506
Releases: master, 8.7
Change-Id: I77f6bc73a6d9160b7074d39a948e94a59b51e521
Reviewed-on: https://review.typo3.org/55408
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Timo Pfeffer <pfeffer.timo+typo3@gmail.com>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/frontend/Classes/Configuration/TypoScript/ConditionMatching/ConditionMatcher.php
typo3/sysext/frontend/Tests/Unit/Configuration/TypoScript/ConditionMatching/ConditionMatcherTest.php

index b536d0f..439e064 100644 (file)
@@ -90,8 +90,13 @@ class ConditionMatcher extends AbstractConditionMatcher
     /**
      * Returns GP / ENV / TSFE vars
      *
+     * @example GP:L
+     * @example TSFE:fe_user|sesData|foo|bar
+     * @example TSFE:id
+     * @example ENV:HTTP_HOST
+     *
      * @param string $var Identifier
-     * @return mixed The value of the variable pointed to or NULL if variable did not exist
+     * @return mixed|null The value of the variable pointed to or NULL if variable did not exist
      */
     protected function getVariable($var)
     {
@@ -103,7 +108,11 @@ class ConditionMatcher extends AbstractConditionMatcher
             if ($k) {
                 switch ((string)trim($vars[0])) {
                     case 'TSFE':
-                        $val = $this->getGlobal('TSFE|' . $vars[1]);
+                        if (strpos($vars[1], 'fe_user|sesData|') === 0) {
+                            $val = $this->getSessionVariable(substr($vars[1], 16));
+                        } else {
+                            $val = $this->getGlobal('TSFE|' . $vars[1]);
+                        }
                         break;
                     default:
                 }
@@ -113,6 +122,33 @@ class ConditionMatcher extends AbstractConditionMatcher
     }
 
     /**
+     * Return variable from current frontend user session
+     *
+     * @param string $var Session key
+     * @return mixed|null The value of the variable pointed to or NULL if variable did not exist
+     */
+    protected function getSessionVariable(string $var)
+    {
+        $retVal = null;
+        $keyParts = explode('|', $var);
+        $sessionKey = array_shift($keyParts);
+        $tsfe = $this->getTypoScriptFrontendController();
+        if ($tsfe && is_object($tsfe->fe_user)) {
+            $retVal = $tsfe->fe_user->getSessionData($sessionKey);
+            foreach ($keyParts as $keyPart) {
+                if (is_object($retVal)) {
+                    $retVal = $retVal->{$keyPart};
+                } elseif (is_array($retVal)) {
+                    $retVal = $retVal[$keyPart];
+                } else {
+                    break;
+                }
+            }
+        }
+        return $retVal;
+    }
+
+    /**
      * Get the usergroup list of the current user.
      *
      * @return string The usergroup list of the current user
index 28aa67c..db932da 100644 (file)
@@ -14,8 +14,10 @@ namespace TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatchi
  * The TYPO3 project - inspiring people to share!
  */
 
+use Prophecy\Argument;
 use TYPO3\CMS\Core\Configuration\TypoScript\Exception\InvalidTypoScriptConditionException;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
 use TYPO3\CMS\Frontend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
 use TYPO3\CMS\Frontend\Tests\Unit\Configuration\TypoScript\ConditionMatching\Fixtures\TestConditionException;
 
@@ -607,8 +609,14 @@ class ConditionMatcherTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCas
         $GLOBALS['TSFE']->id = 1234567;
         $GLOBALS['TSFE']->testSimpleObject = new \stdClass();
         $GLOBALS['TSFE']->testSimpleObject->testSimpleVariable = 'testValue';
+
+        $prophecy = $this->prophesize(FrontendUserAuthentication::class);
+        $prophecy->getSessionData(Argument::exact('foo'))->willReturn(['bar' => 1234567]);
+        $GLOBALS['TSFE']->fe_user = $prophecy->reveal();
+
         $this->assertTrue($this->matchCondition->match('[globalString = TSFE:id = 1234567]'));
         $this->assertTrue($this->matchCondition->match('[globalString = TSFE:testSimpleObject|testSimpleVariable = testValue]'));
+        $this->assertTrue($this->matchCondition->match('[globalString = TSFE:fe_user|sesData|foo|bar = 1234567]'));
     }
 
     /**