Add SessionHandler::deleteIfNew()
authorTim Düsterhus <duesterhus@woltlab.com>
Fri, 15 Mar 2019 13:12:50 +0000 (14:12 +0100)
committerTim Düsterhus <duesterhus@woltlab.com>
Fri, 15 Mar 2019 13:12:50 +0000 (14:12 +0100)
see #2851

wcfsetup/install/files/lib/action/BackgroundQueuePerformAction.class.php
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index f5f05c9db98f2a52fa36fc7df8ef9d17c7ae55a4..b711c5a2d1c3ca3483188821bb0e7d7be532b7bd 100644 (file)
@@ -33,9 +33,7 @@ class BackgroundQueuePerformAction extends AbstractAction {
                        }
                }
                echo BackgroundQueueHandler::getInstance()->getRunnableCount();
-               if (WCF::getSession()->isFirstVisit() && !WCF::getUser()->userID) {
-                       WCF::getSession()->delete();
-               }
+               WCF::getSession()->deleteIfNew();
                exit;
        }
 }
index 2f3dd2c8c403295c33851f4db44cd9dd73abe26f..f70c52f17d6b31d7a6b825a6fb54b283b294f962 100644 (file)
@@ -989,6 +989,25 @@ class SessionHandler extends SingletonFactory {
                $this->disableUpdate();
        }
        
+       /**
+        * Deletes this session if:
+        * - it is newly created in this request, and
+        * - it belongs to a guest.
+        * 
+        * This method is useful if you have controllers that are likely to be
+        * accessed by a user agent that is not going to re-use sessions (e.g.
+        * curl in a cronjob). It immediately remove the session that was created
+        * just for that request and that is not going to be used ever again.
+        * 
+        * @since 5.2
+        */
+       public function deleteIfNew() {
+               if (!$this->isFirstVisit()) return;
+               if ($this->getUser()->userID) return;
+               
+               $this->delete();
+       }
+       
        /**
         * Returns currently active language id.
         *