1
0

SessionMiddleware.php 2.0 KB

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