Files
Carl Schwan d43d4e7c08 perf: Remove MiddlewareUtils overhead
Avoid doing some reflection

Signed-off-by: Carl Schwan <carl@carlschwan.eu>
2026-07-29 11:23:57 +02:00

67 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Provisioning_API\Middleware;
use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting;
use OCP\AppFramework\Http\Attribute\NoSubAdminRequired;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\OCS\OCSException;
use OCP\AppFramework\Utility\IControllerMethodReflector;
class ProvisioningApiMiddleware extends Middleware {
/**
* ProvisioningApiMiddleware constructor.
*
* @param IControllerMethodReflector $reflector
* @param bool $isAdmin
* @param bool $isSubAdmin
*/
public function __construct(
private IControllerMethodReflector $reflector,
private bool $isAdmin,
private bool $isSubAdmin,
) {
}
/**
* @param Controller $controller
* @param string $methodName
*
* @throws NotSubAdminException
*/
#[\Override]
public function beforeController(Controller $controller, string $methodName): void {
// If AuthorizedAdminSetting, the check will be done in the SecurityMiddleware
if (!$this->isAdmin && !$this->reflector->hasAnnotationOrAttribute('NoSubAdminRequired', NoSubAdminRequired::class)
&& !$this->isSubAdmin && !$this->reflector->hasAnnotationOrAttribute('AuthorizedAdminSetting', AuthorizedAdminSetting::class)) {
throw new NotSubAdminException();
}
}
/**
* @param Controller $controller
* @param string $methodName
* @param \Exception $exception
* @throws \Exception
*/
#[\Override]
public function afterException(Controller $controller, string $methodName, \Exception $exception): never {
if ($exception instanceof NotSubAdminException) {
throw new OCSException($exception->getMessage(), Http::STATUS_FORBIDDEN);
}
throw $exception;
}
}