SessionMiddleware.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\AppFramework\Middleware;
  28. use OC\AppFramework\Utility\ControllerMethodReflector;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http\Attribute\UseSession;
  31. use OCP\AppFramework\Http\Response;
  32. use OCP\AppFramework\Middleware;
  33. use OCP\ISession;
  34. use ReflectionMethod;
  35. class SessionMiddleware extends Middleware {
  36. /** @var ControllerMethodReflector */
  37. private $reflector;
  38. /** @var ISession */
  39. private $session;
  40. public function __construct(ControllerMethodReflector $reflector,
  41. ISession $session) {
  42. $this->reflector = $reflector;
  43. $this->session = $session;
  44. }
  45. /**
  46. * @param Controller $controller
  47. * @param string $methodName
  48. */
  49. public function beforeController($controller, $methodName) {
  50. /**
  51. * Annotation deprecated with Nextcloud 26
  52. */
  53. $hasAnnotation = $this->reflector->hasAnnotation('UseSession');
  54. if ($hasAnnotation) {
  55. $this->session->reopen();
  56. return;
  57. }
  58. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  59. $hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
  60. if ($hasAttribute) {
  61. $this->session->reopen();
  62. }
  63. }
  64. /**
  65. * @param Controller $controller
  66. * @param string $methodName
  67. * @param Response $response
  68. * @return Response
  69. */
  70. public function afterController($controller, $methodName, Response $response) {
  71. /**
  72. * Annotation deprecated with Nextcloud 26
  73. */
  74. $hasAnnotation = $this->reflector->hasAnnotation('UseSession');
  75. if ($hasAnnotation) {
  76. $this->session->close();
  77. return $response;
  78. }
  79. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  80. $hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
  81. if ($hasAttribute) {
  82. $this->session->close();
  83. }
  84. return $response;
  85. }
  86. }