Skip to content

Commit

Permalink
feat(app-framework): Add support for global middlewares
Browse files Browse the repository at this point in the history
This allows apps to register middlewares that always register, not just
for the app's own requests

Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Jan 23, 2023
1 parent cdcb1dc commit 775c29d
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
45 changes: 45 additions & 0 deletions lib/private/AppFramework/Bootstrap/MiddlewareRegistration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* @copyright 2023 Christoph Wurst <[email protected]>
*
* @author 2023 Christoph Wurst <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*/

namespace OC\AppFramework\Bootstrap;

use OCP\AppFramework\Middleware;

/**
* @psalm-immutable
* @template-extends ServiceRegistration<Middleware>
*/
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;
}
}
13 changes: 7 additions & 6 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class RegistrationContext {
/** @var EventListenerRegistration[] */
private $eventListeners = [];

/** @var ServiceRegistration<Middleware>[] */
/** @var ServiceRegistration<MiddlewareRegistration>[] */
private $middlewares = [];

/** @var ServiceRegistration<IProvider>[] */
Expand Down Expand Up @@ -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,
);
}

Expand Down Expand Up @@ -368,8 +369,8 @@ public function registerEventListener(string $appId, string $event, string $list
/**
* @psalm-param class-string<Middleware> $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) {
Expand Down Expand Up @@ -617,7 +618,7 @@ public function delegateContainerRegistrations(array $apps): void {
}

/**
* @return ServiceRegistration[]
* @return MiddlewareRegistration[]
*/
public function getMiddlewareRegistrations(): array {
return $this->middlewares;
Expand Down
4 changes: 2 additions & 2 deletions lib/private/AppFramework/DependencyInjection/DIContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/public/AppFramework/Bootstrap/IRegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ 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
* @see IAppContainer::registerMiddleWare()
*
* @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
Expand Down

0 comments on commit 775c29d

Please sign in to comment.