From 775c29d08fb34612b50e011567b42010fea426d7 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Mon, 23 Jan 2023 17:11:39 +0100 Subject: [PATCH] feat(app-framework): Add support for global middlewares This allows apps to register middlewares that always register, not just for the app's own requests Signed-off-by: Christoph Wurst --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../Bootstrap/MiddlewareRegistration.php | 45 +++++++++++++++++++ .../Bootstrap/RegistrationContext.php | 13 +++--- .../DependencyInjection/DIContainer.php | 4 +- .../Bootstrap/IRegistrationContext.php | 3 +- 6 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index a92b208ba7a5f..853d97f4a2e3e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -664,6 +664,7 @@ 'OC\\AppFramework\\Bootstrap\\Coordinator' => $baseDir . '/lib/private/AppFramework/Bootstrap/Coordinator.php', 'OC\\AppFramework\\Bootstrap\\EventListenerRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/EventListenerRegistration.php', 'OC\\AppFramework\\Bootstrap\\FunctionInjector' => $baseDir . '/lib/private/AppFramework/Bootstrap/FunctionInjector.php', + 'OC\\AppFramework\\Bootstrap\\MiddlewareRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php', 'OC\\AppFramework\\Bootstrap\\ParameterRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ParameterRegistration.php', 'OC\\AppFramework\\Bootstrap\\PreviewProviderRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php', 'OC\\AppFramework\\Bootstrap\\RegistrationContext' => $baseDir . '/lib/private/AppFramework/Bootstrap/RegistrationContext.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index c0ff8e6d9a4b3..455eccc45fd64 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -697,6 +697,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\AppFramework\\Bootstrap\\Coordinator' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/Coordinator.php', 'OC\\AppFramework\\Bootstrap\\EventListenerRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/EventListenerRegistration.php', 'OC\\AppFramework\\Bootstrap\\FunctionInjector' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/FunctionInjector.php', + 'OC\\AppFramework\\Bootstrap\\MiddlewareRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php', 'OC\\AppFramework\\Bootstrap\\ParameterRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ParameterRegistration.php', 'OC\\AppFramework\\Bootstrap\\PreviewProviderRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/PreviewProviderRegistration.php', 'OC\\AppFramework\\Bootstrap\\RegistrationContext' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/RegistrationContext.php', diff --git a/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php b/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php new file mode 100644 index 0000000000000..da226b311e073 --- /dev/null +++ b/lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php @@ -0,0 +1,45 @@ + + * + * @author 2023 Christoph Wurst + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +namespace OC\AppFramework\Bootstrap; + +use OCP\AppFramework\Middleware; + +/** + * @psalm-immutable + * @template-extends ServiceRegistration + */ +class MiddlewareRegistration extends ServiceRegistration { + private bool $global; + + public function __construct(string $appId, string $service, bool $global) { + parent::__construct($appId, $service); + $this->global = $global; + } + + public function isGlobal(): bool { + return $this->global; + } +} diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index a777fd7528846..737ceb8cd97a6 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -94,7 +94,7 @@ class RegistrationContext { /** @var EventListenerRegistration[] */ private $eventListeners = []; - /** @var ServiceRegistration[] */ + /** @var ServiceRegistration[] */ private $middlewares = []; /** @var ServiceRegistration[] */ @@ -205,10 +205,11 @@ public function registerEventListener(string $event, string $listener, int $prio ); } - public function registerMiddleware(string $class): void { + public function registerMiddleware(string $class, bool $global = false): void { $this->context->registerMiddleware( $this->appId, - $class + $class, + $global, ); } @@ -368,8 +369,8 @@ public function registerEventListener(string $appId, string $event, string $list /** * @psalm-param class-string $class */ - public function registerMiddleware(string $appId, string $class): void { - $this->middlewares[] = new ServiceRegistration($appId, $class); + public function registerMiddleware(string $appId, string $class, bool $global): void { + $this->middlewares[] = new MiddlewareRegistration($appId, $class, $global); } public function registerSearchProvider(string $appId, string $class) { @@ -617,7 +618,7 @@ public function delegateContainerRegistrations(array $apps): void { } /** - * @return ServiceRegistration[] + * @return MiddlewareRegistration[] */ public function getMiddlewareRegistrations(): array { return $this->middlewares; diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 4b102526dbe4a..90fab1f5a2989 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -319,9 +319,9 @@ public function __construct(string $appName, array $urlParams = [], ServerContai $coordinator = $c->get(OC\AppFramework\Bootstrap\Coordinator::class); $registrationContext = $coordinator->getRegistrationContext(); if ($registrationContext !== null) { - $appId = $this->getAppName(); foreach ($registrationContext->getMiddlewareRegistrations() as $middlewareRegistration) { - if ($middlewareRegistration->getAppId() === $appId) { + if ($middlewareRegistration->getAppId() === $this->getAppName() + || $middlewareRegistration->isGlobal()) { $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); } } diff --git a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php index 7ff2bdca52e19..ae6cebe374c9b 100644 --- a/lib/public/AppFramework/Bootstrap/IRegistrationContext.php +++ b/lib/public/AppFramework/Bootstrap/IRegistrationContext.php @@ -138,6 +138,7 @@ public function registerEventListener(string $event, string $listener, int $prio /** * @param string $class + * @param bool $global load this middleware also for requests of other apps? Added in Nextcloud 26 * @psalm-param class-string<\OCP\AppFramework\Middleware> $class * * @return void @@ -145,7 +146,7 @@ public function registerEventListener(string $event, string $listener, int $prio * * @since 20.0.0 */ - public function registerMiddleware(string $class): void; + public function registerMiddleware(string $class, bool $global = false): void; /** * Register a search provider for the unified search