1<?php
2/**
3 * @author Morris Jobke <hey@morrisjobke.de>
4 * @author Roeland Jago Douma <rullzer@users.noreply.github.com>
5 * @author Thomas Müller <thomas.mueller@tmit.eu>
6 *
7 * @copyright Copyright (c) 2018, ownCloud GmbH
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
24namespace OC\AppFramework\Middleware;
25
26use OC\AppFramework\Utility\ControllerMethodReflector;
27use OCP\IRequest;
28use OCP\AppFramework\Http\Response;
29use OCP\AppFramework\Middleware;
30use OCP\ISession;
31
32class SessionMiddleware extends Middleware {
33
34	/** @var IRequest */
35	private $request;
36
37	/** @var ControllerMethodReflector */
38	private $reflector;
39
40	/** @var ISession */
41	private $session;
42
43	/**
44	 * @param IRequest $request
45	 * @param ControllerMethodReflector $reflector
46	 */
47	public function __construct(
48		IRequest $request,
49		ControllerMethodReflector $reflector,
50		ISession $session
51	) {
52		$this->request = $request;
53		$this->reflector = $reflector;
54		$this->session = $session;
55	}
56
57	/**
58	 * @param \OCP\AppFramework\Controller $controller
59	 * @param string $methodName
60	 */
61	public function beforeController($controller, $methodName) {
62		$useSession = $this->reflector->hasAnnotation('UseSession');
63		if (!$useSession) {
64			$this->session->close();
65		}
66	}
67
68	/**
69	 * @param \OCP\AppFramework\Controller $controller
70	 * @param string $methodName
71	 * @param Response $response
72	 * @return Response
73	 */
74	public function afterController($controller, $methodName, Response $response) {
75		$useSession = $this->reflector->hasAnnotation('UseSession');
76		if ($useSession) {
77			$this->session->close();
78		}
79		return $response;
80	}
81}
82