diff options
5 files changed, 61 insertions, 41 deletions
diff --git a/lib/private/AppFramework/Bootstrap/Coordinator.php b/lib/private/AppFramework/Bootstrap/Coordinator.php index f5f50b1b775..ff04196fef6 100644 --- a/lib/private/AppFramework/Bootstrap/Coordinator.php +++ b/lib/private/AppFramework/Bootstrap/Coordinator.php @@ -153,7 +153,6 @@ class Coordinator { $this->registrationContext->delegateDashboardPanelRegistrations($this->dashboardManager); $this->registrationContext->delegateEventListenerRegistrations($this->eventDispatcher); $this->registrationContext->delegateContainerRegistrations($apps); - $this->registrationContext->delegateMiddlewareRegistrations($apps); } public function getRegistrationContext(): ?RegistrationContext { diff --git a/lib/private/AppFramework/Bootstrap/RegistrationContext.php b/lib/private/AppFramework/Bootstrap/RegistrationContext.php index 04faf330dc3..ac3f42ff2af 100644 --- a/lib/private/AppFramework/Bootstrap/RegistrationContext.php +++ b/lib/private/AppFramework/Bootstrap/RegistrationContext.php @@ -617,29 +617,10 @@ class RegistrationContext { } /** - * @param App[] $apps + * @return ServiceRegistration<Middleware>[] */ - public function delegateMiddlewareRegistrations(array $apps): void { - while (($middleware = array_shift($this->middlewares)) !== null) { - $appId = $middleware->getAppId(); - if (!isset($apps[$appId])) { - // If we land here something really isn't right. But at least we caught the - // notice that is otherwise emitted for the undefined index - $this->logger->error("App $appId not loaded for the container middleware registration"); - - continue; - } - - try { - $apps[$appId] - ->getContainer() - ->registerMiddleWare($middleware->getService()); - } catch (Throwable $e) { - $this->logger->error("Error during capability registration of $appId: " . $e->getMessage(), [ - 'exception' => $e, - ]); - } - } + public function getMiddlewareRegistrations(): array { + return $this->middlewares; } /** diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 6e0e452bccd..6e7f054a80b 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -315,6 +315,17 @@ class DIContainer extends SimpleContainer implements IAppContainer { $c->get(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) ); + /** @var \OC\AppFramework\Bootstrap\Coordinator $coordinator */ + $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) { + $dispatcher->registerMiddleware($c->get($middlewareRegistration->getService())); + } + } + } foreach ($this->middleWares as $middleWare) { $dispatcher->registerMiddleware($c->get($middleWare)); } diff --git a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php index 22111f5b68e..cee30b665fb 100644 --- a/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php +++ b/tests/lib/AppFramework/Bootstrap/RegistrationContextTest.php @@ -27,6 +27,7 @@ namespace lib\AppFramework\Bootstrap; use OC\AppFramework\Bootstrap\RegistrationContext; use OC\AppFramework\Bootstrap\ServiceRegistration; +use OC\Core\Middleware\TwoFactorMiddleware; use OCP\AppFramework\App; use OCP\AppFramework\IAppContainer; use OCP\EventDispatcher\IEventDispatcher; @@ -145,24 +146,6 @@ class RegistrationContextTest extends TestCase { ]); } - public function testRegisterMiddleware(): void { - $app = $this->createMock(App::class); - $name = 'abc'; - $container = $this->createMock(IAppContainer::class); - $app->method('getContainer') - ->willReturn($container); - $container->expects($this->once()) - ->method('registerMiddleware') - ->with($name); - $this->logger->expects($this->never()) - ->method('error'); - - $this->context->for('myapp')->registerMiddleware($name); - $this->context->delegateMiddlewareRegistrations([ - 'myapp' => $app, - ]); - } - public function testRegisterUserMigrator(): void { $appIdA = 'myapp'; $migratorClassA = 'OCA\App\UserMigration\AppMigrator'; @@ -195,4 +178,14 @@ class RegistrationContextTest extends TestCase { [false] ]; } + + public function testGetMiddlewareRegistrations(): void { + $this->context->registerMiddleware('core', TwoFactorMiddleware::class); + + $registrations = $this->context->getMiddlewareRegistrations(); + + self::assertNotEmpty($registrations); + self::assertSame('core', $registrations[0]->getAppId()); + self::assertSame(TwoFactorMiddleware::class, $registrations[0]->getService()); + } } diff --git a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php index 69367ad6ce2..bcf4e0a2771 100644 --- a/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php +++ b/tests/lib/AppFramework/DependencyInjection/DIContainerTest.php @@ -25,9 +25,13 @@ namespace Test\AppFramework\DependencyInjection; +use OC\AppFramework\Bootstrap\Coordinator; +use OC\AppFramework\Bootstrap\RegistrationContext; +use OC\AppFramework\Bootstrap\ServiceRegistration; use OC\AppFramework\DependencyInjection\DIContainer; use OC\AppFramework\Http\Request; use OC\AppFramework\Middleware\Security\SecurityMiddleware; +use OCP\AppFramework\Middleware; use OCP\AppFramework\QueryException; use OCP\IConfig; use OCP\IRequestId; @@ -84,6 +88,38 @@ class DIContainerTest extends \Test\TestCase { $this->assertTrue($found); } + public function testMiddlewareDispatcherIncludesBootstrapMiddlewares(): void { + $coordinator = $this->createMock(Coordinator::class); + $this->container[Coordinator::class] = $coordinator; + $this->container['Request'] = $this->createMock(Request::class); + $registrationContext = $this->createMock(RegistrationContext::class); + $registrationContext->method('getMiddlewareRegistrations') + ->willReturn([ + new ServiceRegistration($this->container['appName'], 'foo'), + new ServiceRegistration('otherapp', 'bar'), + ]); + $this->container['foo'] = new class extends Middleware { + }; + $this->container['bar'] = new class extends Middleware { + }; + $coordinator->method('getRegistrationContext')->willReturn($registrationContext); + + $dispatcher = $this->container['MiddlewareDispatcher']; + + $middlewares = $dispatcher->getMiddlewares(); + self::assertNotEmpty($middlewares); + foreach ($middlewares as $middleware) { + if ($middleware === $this->container['bar']) { + $this->fail('Container must not register this middleware'); + } + if ($middleware === $this->container['foo']) { + // It is done + return; + } + } + $this->fail('Bootstrap registered middleware not found'); + } + public function testInvalidAppClass() { $this->expectException(QueryException::class); $this->container->query('\OCA\Name\Foo'); |