From 02c5933af8d185cbef4eacc7f732566767fb9b4c Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Tue, 21 Oct 2014 14:53:10 +0200 Subject: introduce SessionMiddleWare to control session handling via an annotation --- .../appframework/middleware/sessionmiddleware.php | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/private/appframework/middleware/sessionmiddleware.php (limited to 'lib/private/appframework/middleware/sessionmiddleware.php') diff --git a/lib/private/appframework/middleware/sessionmiddleware.php b/lib/private/appframework/middleware/sessionmiddleware.php new file mode 100644 index 00000000000..d50880f3739 --- /dev/null +++ b/lib/private/appframework/middleware/sessionmiddleware.php @@ -0,0 +1,70 @@ + + * @copyright Thomas Müller 2014 + */ + +namespace OC\AppFramework\Middleware; + +use OC\AppFramework\Utility\ControllerMethodReflector; +use OCP\IRequest; +use OCP\AppFramework\Http\Response; +use OCP\AppFramework\Middleware; +use OCP\ISession; + +class SessionMiddleware extends Middleware { + + /** + * @var IRequest + */ + private $request; + + /** + * @var ControllerMethodReflector + */ + private $reflector; + + /** + * @param IRequest $request + * @param ControllerMethodReflector $reflector + */ + public function __construct(IRequest $request, + ControllerMethodReflector $reflector, + ISession $session +) { + $this->request = $request; + $this->reflector = $reflector; + $this->session = $session; + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + */ + public function beforeController($controller, $methodName) { + $useSession = $this->reflector->hasAnnotation('UseSession'); + if (!$useSession) { + $this->session->close(); + } + } + + /** + * @param \OCP\AppFramework\Controller $controller + * @param string $methodName + * @param Response $response + * @return Response + */ + public function afterController($controller, $methodName, Response $response){ + $useSession = $this->reflector->hasAnnotation('UseSession'); + if ($useSession) { + $this->session->close(); + } + return $response; + } + +} -- cgit v1.2.3