]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add real events to load additionalscripts 21822/head
authorRoeland Jago Douma <roeland@famdouma.nl>
Mon, 13 Jul 2020 20:29:14 +0000 (22:29 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Wed, 15 Jul 2020 12:07:18 +0000 (14:07 +0200)
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/AppFramework/Middleware/AdditionalScriptsMiddleware.php
lib/public/AppFramework/Http/Events/BeforeTemplateRenderedEvent.php [new file with mode: 0644]
lib/public/AppFramework/Http/TemplateResponse.php
tests/lib/AppFramework/Middleware/AdditionalScriptsMiddlewareTest.php

index 5e80e620d72c4247baeceb1019235b4859c86c41..6a53f4e04cf634774edbdaad3b4b0915f7da1124 100644 (file)
@@ -41,6 +41,7 @@ return array(
     'OCP\\AppFramework\\Http\\DownloadResponse' => $baseDir . '/lib/public/AppFramework/Http/DownloadResponse.php',
     'OCP\\AppFramework\\Http\\EmptyContentSecurityPolicy' => $baseDir . '/lib/public/AppFramework/Http/EmptyContentSecurityPolicy.php',
     'OCP\\AppFramework\\Http\\EmptyFeaturePolicy' => $baseDir . '/lib/public/AppFramework/Http/EmptyFeaturePolicy.php',
+    'OCP\\AppFramework\\Http\\Events\\BeforeTemplateRenderedEvent' => $baseDir . '/lib/public/AppFramework/Http/Events/BeforeTemplateRenderedEvent.php',
     'OCP\\AppFramework\\Http\\FeaturePolicy' => $baseDir . '/lib/public/AppFramework/Http/FeaturePolicy.php',
     'OCP\\AppFramework\\Http\\FileDisplayResponse' => $baseDir . '/lib/public/AppFramework/Http/FileDisplayResponse.php',
     'OCP\\AppFramework\\Http\\ICallbackResponse' => $baseDir . '/lib/public/AppFramework/Http/ICallbackResponse.php',
index 4cffa4e4a6900fab123352395f644461490fb90d..edb51c1865f46daaf10155991063beb3514f902d 100644 (file)
@@ -70,6 +70,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
         'OCP\\AppFramework\\Http\\DownloadResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/DownloadResponse.php',
         'OCP\\AppFramework\\Http\\EmptyContentSecurityPolicy' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/EmptyContentSecurityPolicy.php',
         'OCP\\AppFramework\\Http\\EmptyFeaturePolicy' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/EmptyFeaturePolicy.php',
+        'OCP\\AppFramework\\Http\\Events\\BeforeTemplateRenderedEvent' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/Events/BeforeTemplateRenderedEvent.php',
         'OCP\\AppFramework\\Http\\FeaturePolicy' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/FeaturePolicy.php',
         'OCP\\AppFramework\\Http\\FileDisplayResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/FileDisplayResponse.php',
         'OCP\\AppFramework\\Http\\ICallbackResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/ICallbackResponse.php',
index b9f238eecb33e5f5b694d6abe2c33a71d53d73a3..619432be78fd8d5dfc579504f4cf6de3b43b3e04 100644 (file)
@@ -27,24 +27,29 @@ declare(strict_types=1);
 
 namespace OC\AppFramework\Middleware;
 
+use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
 use OCP\AppFramework\Http\Response;
 use OCP\AppFramework\Http\StandaloneTemplateResponse;
 use OCP\AppFramework\Http\TemplateResponse;
 use OCP\AppFramework\Middleware;
 use OCP\AppFramework\PublicShareController;
 use OCP\EventDispatcher\GenericEvent;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IUserSession;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 
 class AdditionalScriptsMiddleware extends Middleware {
        /** @var EventDispatcherInterface */
-       private $dispatcher;
+       private $legacyDispatcher;
        /** @var IUserSession */
        private $userSession;
+       /** @var IEventDispatcher */
+       private $dispatcher;
 
-       public function __construct(EventDispatcherInterface $dispatcher, IUserSession $userSession) {
-               $this->dispatcher = $dispatcher;
+       public function __construct(EventDispatcherInterface $legacyDispatcher, IUserSession $userSession, IEventDispatcher $dispatcher) {
+               $this->legacyDispatcher = $legacyDispatcher;
                $this->userSession = $userSession;
+               $this->dispatcher = $dispatcher;
        }
 
        public function afterController($controller, $methodName, Response $response): Response {
@@ -57,11 +62,16 @@ class AdditionalScriptsMiddleware extends Middleware {
                }
 
                if ($response instanceof TemplateResponse) {
-                       $this->dispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, new GenericEvent());
+                       $this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, new GenericEvent());
 
                        if (!($response instanceof StandaloneTemplateResponse) && $this->userSession->isLoggedIn()) {
-                               $this->dispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, new GenericEvent());
+                               $this->legacyDispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, new GenericEvent());
+                               $isLoggedIn = true;
+                       } else {
+                               $isLoggedIn = false;
                        }
+
+                       $this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn));
                }
 
                return $response;
diff --git a/lib/public/AppFramework/Http/Events/BeforeTemplateRenderedEvent.php b/lib/public/AppFramework/Http/Events/BeforeTemplateRenderedEvent.php
new file mode 100644 (file)
index 0000000..1af679f
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ * @author Morris Jobke <hey@morrisjobke.de>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCP\AppFramework\Http\Events;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * The event is triggered before the rendering step of each TemplateResponse.
+ *
+ * @package OCP\AppFramework\Http\Events
+ * @since 20.0.0
+ */
+class BeforeTemplateRenderedEvent extends Event {
+       /** @var bool */
+       private $loggedIn;
+
+       /**
+        * @since 20.0.0
+        */
+       public function __construct(bool $loggedIn) {
+               parent::__construct();
+
+               $this->loggedIn = $loggedIn;
+       }
+
+       /**
+        * @since 20.0.0
+        */
+       public function isLoggedIn(): bool {
+               return $this->loggedIn;
+       }
+}
index bc86c153b7592de606ed3f2ddd70b075bd15a2f5..48e6c43411f626eb713b348657bd920b80dd5a1f 100644 (file)
@@ -38,7 +38,13 @@ namespace OCP\AppFramework\Http;
  * @since 6.0.0
  */
 class TemplateResponse extends Response {
+       /**
+        * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
+        */
        public const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts';
+       /**
+        * @deprecated 20.0.0 use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent
+        */
        public const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn';
 
        /**
index 617ead473c0cee3f0415ebb96635e6a8e8fc3451..5127248215b6b277f5f74800dff6699b3147de0a 100644 (file)
@@ -27,10 +27,12 @@ namespace Test\AppFramework\Middleware;
 
 use OC\AppFramework\Middleware\AdditionalScriptsMiddleware;
 use OCP\AppFramework\Controller;
+use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
 use OCP\AppFramework\Http\Response;
 use OCP\AppFramework\Http\StandaloneTemplateResponse;
 use OCP\AppFramework\Http\TemplateResponse;
 use OCP\AppFramework\PublicShareController;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IUserSession;
 use PHPUnit\Framework\MockObject\MockObject;
 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@@ -38,7 +40,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
 
        /** @var EventDispatcherInterface|MockObject */
-       private $dispatcher;
+       private $legacyDispatcher;
        /** @var IUserSession|MockObject */
        private $userSession;
 
@@ -47,40 +49,48 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
 
        /** @var AdditionalScriptsMiddleware */
        private $middleWare;
+       /** @var IEventDispatcher|MockObject */
+       private $dispatcher;
 
        protected function setUp(): void {
                parent::setUp();
 
-               $this->dispatcher = $this->createMock(EventDispatcherInterface::class);
+               $this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
                $this->userSession = $this->createMock(IUserSession::class);
+               $this->dispatcher = $this->createMock(IEventDispatcher::class);
                $this->middleWare = new AdditionalScriptsMiddleware(
-                       $this->dispatcher,
-                       $this->userSession
+                       $this->legacyDispatcher,
+                       $this->userSession,
+                       $this->dispatcher
                );
 
                $this->controller = $this->createMock(Controller::class);
        }
 
        public function testNoTemplateResponse() {
-               $this->dispatcher->expects($this->never())
+               $this->legacyDispatcher->expects($this->never())
                        ->method($this->anything());
                $this->userSession->expects($this->never())
                        ->method($this->anything());
+               $this->dispatcher->expects($this->never())
+                       ->method($this->anything());
 
                $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
        }
 
        public function testPublicShareController() {
-               $this->dispatcher->expects($this->never())
+               $this->legacyDispatcher->expects($this->never())
                        ->method($this->anything());
                $this->userSession->expects($this->never())
                        ->method($this->anything());
+               $this->dispatcher->expects($this->never())
+                       ->method($this->anything());
 
                $this->middleWare->afterController($this->createMock(PublicShareController::class), 'myMethod', $this->createMock(Response::class));
        }
 
        public function testStandaloneTemplateResponse() {
-               $this->dispatcher->expects($this->once())
+               $this->legacyDispatcher->expects($this->once())
                        ->method('dispatch')
                        ->willReturnCallback(function ($eventName) {
                                if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
@@ -91,12 +101,21 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
                        });
                $this->userSession->expects($this->never())
                        ->method($this->anything());
+               $this->dispatcher->expects($this->once())
+                       ->method('dispatchTyped')
+                       ->willReturnCallback(function ($event) {
+                               if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
+                                       return;
+                               }
+
+                               $this->fail('Wrong event dispatched');
+                       });
 
                $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(StandaloneTemplateResponse::class));
        }
 
        public function testTemplateResponseNotLoggedIn() {
-               $this->dispatcher->expects($this->once())
+               $this->legacyDispatcher->expects($this->once())
                        ->method('dispatch')
                        ->willReturnCallback(function ($eventName) {
                                if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
@@ -107,6 +126,15 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
                        });
                $this->userSession->method('isLoggedIn')
                        ->willReturn(false);
+               $this->dispatcher->expects($this->once())
+                       ->method('dispatchTyped')
+                       ->willReturnCallback(function ($event) {
+                               if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === false) {
+                                       return;
+                               }
+
+                               $this->fail('Wrong event dispatched');
+                       });
 
                $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
        }
@@ -114,7 +142,7 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
        public function testTemplateResponseLoggedIn() {
                $events = [];
 
-               $this->dispatcher->expects($this->exactly(2))
+               $this->legacyDispatcher->expects($this->exactly(2))
                        ->method('dispatch')
                        ->willReturnCallback(function ($eventName) use (&$events) {
                                if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS ||
@@ -127,6 +155,15 @@ class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
                        });
                $this->userSession->method('isLoggedIn')
                        ->willReturn(true);
+               $this->dispatcher->expects($this->once())
+                       ->method('dispatchTyped')
+                       ->willReturnCallback(function ($event) {
+                               if ($event instanceof BeforeTemplateRenderedEvent && $event->isLoggedIn() === true) {
+                                       return;
+                               }
+
+                               $this->fail('Wrong event dispatched');
+                       });
 
                $this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));