ReloadExecutionMiddleware.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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\Security;
  8. use OC\AppFramework\Middleware\Security\Exceptions\ReloadExecutionException;
  9. use OCP\AppFramework\Http\RedirectResponse;
  10. use OCP\AppFramework\Middleware;
  11. use OCP\ISession;
  12. use OCP\IURLGenerator;
  13. /**
  14. * Simple middleware to handle the clearing of the execution context. This will trigger
  15. * a reload but if the session variable is set we properly redirect to the login page.
  16. */
  17. class ReloadExecutionMiddleware extends Middleware {
  18. /** @var ISession */
  19. private $session;
  20. /** @var IURLGenerator */
  21. private $urlGenerator;
  22. public function __construct(ISession $session, IURLGenerator $urlGenerator) {
  23. $this->session = $session;
  24. $this->urlGenerator = $urlGenerator;
  25. }
  26. public function beforeController($controller, $methodName) {
  27. if ($this->session->exists('clearingExecutionContexts')) {
  28. throw new ReloadExecutionException();
  29. }
  30. }
  31. public function afterException($controller, $methodName, \Exception $exception) {
  32. if ($exception instanceof ReloadExecutionException) {
  33. $this->session->remove('clearingExecutionContexts');
  34. return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute(
  35. 'core.login.showLoginForm',
  36. ['clear' => true] // this param the code in login.js may be removed when the "Clear-Site-Data" is working in the browsers
  37. ));
  38. }
  39. return parent::afterException($controller, $methodName, $exception);
  40. }
  41. }