Revert "Prevent saving `styleID` in sessions for user"
authorTim Düsterhus <duesterhus@woltlab.com>
Mon, 30 May 2022 08:47:36 +0000 (10:47 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Mon, 30 May 2022 08:47:36 +0000 (10:47 +0200)
The intention behind that change still is valid. However using the
`StyleAction::changeStyle()` method which internally uses
`UserAction::update()` internally is problematic, due to events firing. At the
point where `initStyle()` runs, the applications are not yet initialized and
thus the event listener classes of applications will not be found by the
autoloader.

With bb2430b495a4bfe7e8f205b97749f49ce4f59229 the handling of the `styleID`
parameter is already removed, thus ultimately solving the same problem, but
without the issues.

This reverts commit cc5207457ef1157b44ecad54db32ab7438a1158e.

wcfsetup/install/files/lib/data/style/StyleAction.class.php
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index 1e6b306e659e7e24af7b1c4166517f6c5b998201..b0a53c826ddfdadb5f0b156640796b9656ef1b04 100644 (file)
@@ -685,6 +685,8 @@ BROWSERCONFIG;
     {
         StyleHandler::getInstance()->changeStyle($this->style->styleID);
         if (StyleHandler::getInstance()->getStyle()->styleID == $this->style->styleID) {
+            WCF::getSession()->setStyleID($this->style->styleID);
+
             if (WCF::getUser()->userID) {
                 // set this as the permanent style
                 $userAction = new UserAction([WCF::getUser()], 'update', [
@@ -693,8 +695,6 @@ BROWSERCONFIG;
                     ],
                 ]);
                 $userAction->executeAction();
-            } else {
-                WCF::getSession()->register('styleID', $this->style->styleID);
             }
         }
     }
index cc98ee0cb1bd2819f3502a539c6a649266887a7a..3fb6b51bc9fae55a8c1bb84bf42d5ba155ba1205 100644 (file)
@@ -9,7 +9,6 @@ use wcf\data\package\PackageCache;
 use wcf\data\package\PackageEditor;
 use wcf\data\page\Page;
 use wcf\data\page\PageCache;
-use wcf\data\style\StyleAction;
 use wcf\page\CmsPage;
 use wcf\system\application\ApplicationHandler;
 use wcf\system\application\IApplication;
@@ -24,9 +23,7 @@ use wcf\system\exception\ErrorException;
 use wcf\system\exception\IPrintableException;
 use wcf\system\exception\NamedUserException;
 use wcf\system\exception\ParentClassException;
-use wcf\system\exception\PermissionDeniedException;
 use wcf\system\exception\SystemException;
-use wcf\system\exception\UserInputException;
 use wcf\system\language\LanguageFactory;
 use wcf\system\package\PackageInstallationDispatcher;
 use wcf\system\registry\RegistryHandler;
@@ -549,31 +546,13 @@ class WCF
      */
     protected function initStyle()
     {
-        $styleID = 0;
-
         /** @deprecated The 'styleID' parameter is deprecated. */
         if (isset($_REQUEST['styleID'])) {
-            $styleID = \intval($_REQUEST['styleID']);
-
-            try {
-                $action = new StyleAction([$styleID], 'changeStyle');
-                $action->validateAction();
-                $action->executeAction();
-            } catch (PermissionDeniedException | UserInputException $e) {
-                $styleID = 0;
-            }
-        }
-
-        if ($styleID === 0) {
-            if (self::getSession()->getUser()->userID) {
-                $styleID = self::getSession()->getUser()->styleID ?: 0;
-            } else {
-                $styleID = self::getSession()->getVar('styleID') ?: 0;
-            }
+            self::getSession()->setStyleID(\intval($_REQUEST['styleID']));
         }
 
         $styleHandler = StyleHandler::getInstance();
-        $styleHandler->changeStyle($styleID);
+        $styleHandler->changeStyle(self::getSession()->getStyleID());
     }
 
     /**
index b71e9fdf80a86cbb9da808cf5e5c84bc9335245b..05cbeafa1fffaaa5d0c9b0ff00111bcf26f2788f 100644 (file)
@@ -5,7 +5,6 @@ namespace wcf\system\session;
 use ParagonIE\ConstantTime\Hex;
 use wcf\data\session\Session as LegacySession;
 use wcf\data\session\SessionEditor;
-use wcf\data\style\StyleAction;
 use wcf\data\user\User;
 use wcf\data\user\UserEditor;
 use wcf\system\application\ApplicationHandler;
@@ -16,11 +15,9 @@ use wcf\system\database\exception\DatabaseQueryExecutionException;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\event\EventHandler;
 use wcf\system\exception\PermissionDeniedException;
-use wcf\system\exception\UserInputException;
 use wcf\system\page\PageLocationManager;
 use wcf\system\request\RouteHandler;
 use wcf\system\SingletonFactory;
-use wcf\system\style\StyleHandler;
 use wcf\system\user\storage\UserStorageHandler;
 use wcf\system\WCF;
 use wcf\system\WCFACP;
@@ -92,6 +89,12 @@ final class SessionHandler extends SingletonFactory
      */
     protected $legacySession;
 
+    /**
+     * style id
+     * @var int
+     */
+    protected $styleID;
+
     /**
      * user object
      * @var User
@@ -412,8 +415,9 @@ final class SessionHandler extends SingletonFactory
     {
         $this->defineConstants();
 
-        // assign language
+        // assign language and style id
         $this->languageID = $this->getVar('languageID') ?: $this->user->languageID;
+        $this->styleID = $this->getVar('styleID') ?: $this->user->styleID;
 
         // https://github.com/WoltLab/WCF/issues/2568
         if ($this->getVar('__wcfIsFirstVisit') === true) {
@@ -1026,6 +1030,7 @@ final class SessionHandler extends SingletonFactory
         $this->groupData = null;
         $this->languageIDs = null;
         $this->languageID = $this->user->languageID;
+        $this->styleID = $this->user->styleID;
 
         // change language
         WCF::setLanguage($this->languageID ?: 0);
@@ -1170,8 +1175,8 @@ final class SessionHandler extends SingletonFactory
         // If we reach this point we determined that a new authentication is not necessary.
         \assert(
             ($lastAuthentication >= TIME_NOW - $softLimit)
-                || ($lastAuthentication >= TIME_NOW - self::REAUTHENTICATION_HARD_LIMIT
-                    && $lastCheck >= TIME_NOW - self::REAUTHENTICATION_GRACE_PERIOD)
+            || ($lastAuthentication >= TIME_NOW - self::REAUTHENTICATION_HARD_LIMIT
+                && $lastCheck >= TIME_NOW - self::REAUTHENTICATION_GRACE_PERIOD)
         );
 
         // Update the lastCheck timestamp to make sure that the grace period works properly.
@@ -1370,24 +1375,24 @@ final class SessionHandler extends SingletonFactory
     }
 
     /**
-     * @deprecated 5.5 - Use `StyleHandler::getInstance()->getStyle()->styleID` instead.
+     * Returns currently active style id.
+     *
+     * @return  int
      */
     public function getStyleID()
     {
-        return StyleHandler::getInstance()->getStyle()->styleID;
+        return $this->styleID;
     }
 
     /**
-     * @deprecated 5.5 - Set the style directly with the `StyleAction::changeStyle()`.
+     * Sets the currently active style id.
+     *
+     * @param int $styleID
      */
     public function setStyleID($styleID)
     {
-        try {
-            $action = new StyleAction([$styleID], 'changeStyle');
-            $action->validateAction();
-            $action->executeAction();
-        } catch (PermissionDeniedException | UserInputException $e) {
-        }
+        $this->styleID = $styleID;
+        $this->register('styleID', $this->styleID);
     }
 
     /**