SessionMiddleware.php 2.1 KB

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