AdditionalScriptsMiddleware.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\AppFramework\Middleware;
  8. use OC\Core\Controller\LoginController;
  9. use OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent;
  10. use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent;
  11. use OCP\AppFramework\Http\Response;
  12. use OCP\AppFramework\Http\StandaloneTemplateResponse;
  13. use OCP\AppFramework\Http\TemplateResponse;
  14. use OCP\AppFramework\Middleware;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\IUserSession;
  17. class AdditionalScriptsMiddleware extends Middleware {
  18. public function __construct(
  19. private IUserSession $userSession,
  20. private IEventDispatcher $dispatcher,
  21. ) {
  22. }
  23. public function afterController($controller, $methodName, Response $response): Response {
  24. if ($response instanceof TemplateResponse) {
  25. if ($controller instanceof LoginController) {
  26. $this->dispatcher->dispatchTyped(new BeforeLoginTemplateRenderedEvent($response));
  27. } else {
  28. $isLoggedIn = !($response instanceof StandaloneTemplateResponse) && $this->userSession->isLoggedIn();
  29. $this->dispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($isLoggedIn, $response));
  30. }
  31. }
  32. return $response;
  33. }
  34. }