2014-10-21 08:53:10 -04:00
|
|
|
<?php
|
2023-01-25 13:26:21 -05:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-10-21 08:53:10 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2014-10-21 08:53:10 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\AppFramework\Middleware;
|
|
|
|
|
|
|
|
|
|
use OC\AppFramework\Utility\ControllerMethodReflector;
|
2017-07-26 03:03:04 -04:00
|
|
|
use OCP\AppFramework\Controller;
|
2023-01-25 13:26:21 -05:00
|
|
|
use OCP\AppFramework\Http\Attribute\UseSession;
|
2014-10-21 08:53:10 -04:00
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
|
use OCP\AppFramework\Middleware;
|
|
|
|
|
use OCP\ISession;
|
2023-01-25 13:26:21 -05:00
|
|
|
use ReflectionMethod;
|
2014-10-21 08:53:10 -04:00
|
|
|
|
|
|
|
|
class SessionMiddleware extends Middleware {
|
2019-08-28 07:02:29 -04:00
|
|
|
/** @var ControllerMethodReflector */
|
2014-10-21 08:53:10 -04:00
|
|
|
private $reflector;
|
|
|
|
|
|
2019-08-28 07:02:29 -04:00
|
|
|
/** @var ISession */
|
|
|
|
|
private $session;
|
|
|
|
|
|
|
|
|
|
public function __construct(ControllerMethodReflector $reflector,
|
2023-11-23 04:22:34 -05:00
|
|
|
ISession $session) {
|
2014-10-21 08:53:10 -04:00
|
|
|
$this->reflector = $reflector;
|
|
|
|
|
$this->session = $session;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2014-10-21 08:53:10 -04:00
|
|
|
* @param string $methodName
|
|
|
|
|
*/
|
2017-08-01 11:32:03 -04:00
|
|
|
public function beforeController($controller, $methodName) {
|
2023-01-25 13:26:21 -05:00
|
|
|
/**
|
|
|
|
|
* Annotation deprecated with Nextcloud 26
|
|
|
|
|
*/
|
|
|
|
|
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
|
|
|
|
|
if ($hasAnnotation) {
|
|
|
|
|
$this->session->reopen();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
|
|
|
|
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
|
|
|
|
|
if ($hasAttribute) {
|
2022-04-26 06:57:58 -04:00
|
|
|
$this->session->reopen();
|
2014-10-21 08:53:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-07-26 03:03:04 -04:00
|
|
|
* @param Controller $controller
|
2014-10-21 08:53:10 -04:00
|
|
|
* @param string $methodName
|
|
|
|
|
* @param Response $response
|
|
|
|
|
* @return Response
|
|
|
|
|
*/
|
2020-04-09 07:53:40 -04:00
|
|
|
public function afterController($controller, $methodName, Response $response) {
|
2023-01-25 13:26:21 -05:00
|
|
|
/**
|
|
|
|
|
* Annotation deprecated with Nextcloud 26
|
|
|
|
|
*/
|
|
|
|
|
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
|
|
|
|
|
if ($hasAnnotation) {
|
2014-10-21 08:53:10 -04:00
|
|
|
$this->session->close();
|
2023-01-25 13:26:21 -05:00
|
|
|
return $response;
|
2014-10-21 08:53:10 -04:00
|
|
|
}
|
2023-01-25 13:26:21 -05:00
|
|
|
|
|
|
|
|
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
|
|
|
|
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
|
|
|
|
|
if ($hasAttribute) {
|
|
|
|
|
$this->session->close();
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-21 08:53:10 -04:00
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
}
|