-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathMaintenanceModeMiddleware.php
More file actions
53 lines (44 loc) · 1.47 KB
/
Copy pathMaintenanceModeMiddleware.php
File metadata and controls
53 lines (44 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\AppAPI\Middleware;
use Exception;
use OCA\AppAPI\Attribute\MaintenanceModeAvailable;
use OCA\AppAPI\Exceptions\MaintenanceModeException;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Middleware;
use OCP\IConfig;
use ReflectionMethod;
class MaintenanceModeMiddleware extends Middleware {
public function __construct(
private IConfig $config,
) {
}
/**
* @throws MaintenanceModeException when the server is in maintenance mode and the route is not allowed during it
* @throws \ReflectionException
*/
public function beforeController($controller, $methodName) {
if (!$this->config->getSystemValueBool('maintenance', false)) {
return;
}
$reflectionMethod = new ReflectionMethod($controller, $methodName);
if (!empty($reflectionMethod->getAttributes(MaintenanceModeAvailable::class))) {
return;
}
throw new MaintenanceModeException();
}
public function afterException($controller, $methodName, Exception $exception): Response {
if ($exception instanceof MaintenanceModeException) {
$response = new JSONResponse(['message' => $exception->getMessage()], $exception->getCode());
$response->addHeader('X-Nextcloud-Maintenance-Mode', '1');
$response->addHeader('Retry-After', '120');
return $response;
}
throw $exception;
}
}