diff --git a/appinfo/info.xml b/appinfo/info.xml index 6e5846a1..a872b36c 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -14,13 +14,4 @@ - - - office - Office - office.page.index - app.svg - link - - diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 68a60099..501f131b 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -5,6 +5,7 @@ namespace OCA\Office\AppInfo; use OCA\Office\Listener\AppMenuActionListener; +use OCA\Office\Listener\LoadNavigationEntryListener; use OCP\AppFramework\App; use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; @@ -22,6 +23,7 @@ public function __construct() { #[\Override] public function register(IRegistrationContext $context): void { $context->registerEventListener(LoadAdditionalEntriesEvent::class, AppMenuActionListener::class); + $context->registerEventListener(LoadAdditionalEntriesEvent::class, LoadNavigationEntryListener::class); } #[\Override] diff --git a/lib/Listener/AppMenuActionListener.php b/lib/Listener/AppMenuActionListener.php index d4eeb32d..2a8b1598 100644 --- a/lib/Listener/AppMenuActionListener.php +++ b/lib/Listener/AppMenuActionListener.php @@ -6,6 +6,7 @@ use OCA\Office\AppInfo\Application; use OCA\Office\Service\CreatorCategoryService; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventListener; use OCP\INavigationManager; @@ -22,6 +23,7 @@ final class AppMenuActionListener implements IEventListener { /** @psalm-suppress PossiblyUnusedMethod Constructed by the DI container */ public function __construct( + private IAppManager $appManager, private CreatorCategoryService $categoryService, private INavigationManager $navigationManager, private IURLGenerator $urlGenerator, @@ -35,8 +37,9 @@ public function handle(Event $event): void { return; } - // Creators are registered per user, so there is nothing to offer a guest. - if (!$this->userSession->isLoggedIn()) { + // Creators are registered per user, so there is nothing to offer a guest, + // nor to a user outside the groups the app is limited to. + if (!$this->userSession->isLoggedIn() || !$this->appManager->isEnabledForUser(Application::APP_ID)) { return; } diff --git a/lib/Listener/LoadNavigationEntryListener.php b/lib/Listener/LoadNavigationEntryListener.php new file mode 100644 index 00000000..5cef6b4f --- /dev/null +++ b/lib/Listener/LoadNavigationEntryListener.php @@ -0,0 +1,53 @@ + + */ +final class LoadNavigationEntryListener implements IEventListener { + /** @psalm-suppress PossiblyUnusedMethod Constructed by the DI container */ + public function __construct( + private IAppManager $appManager, + private INavigationManager $navigationManager, + private IURLGenerator $urlGenerator, + ) { + } + + #[\Override] + public function handle(Event $event): void { + if (!$event instanceof LoadAdditionalEntriesEvent) { + return; + } + + /** @psalm-suppress InvalidArgument OCP types the closure as non-nullable, but NavigationManager skips null entries */ + $this->navigationManager->add(function (): ?array { + if (!$this->appManager->isEnabledForUser(Application::APP_ID)) { + return null; + } + + return [ + 'id' => Application::APP_ID, + 'app' => Application::APP_ID, + 'type' => INavigationManager::TYPE_APPS, + 'order' => 10, + 'href' => $this->urlGenerator->linkToRoute(Application::APP_ID . '.page.index'), + 'icon' => $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'), + 'name' => 'Office', + ]; + }); + } +} diff --git a/tests/unit/Listener/AppMenuActionListenerTest.php b/tests/unit/Listener/AppMenuActionListenerTest.php index 94ee20da..7ed84640 100644 --- a/tests/unit/Listener/AppMenuActionListenerTest.php +++ b/tests/unit/Listener/AppMenuActionListenerTest.php @@ -6,6 +6,7 @@ use OCA\Office\Listener\AppMenuActionListener; use OCA\Office\Service\CreatorCategoryService; +use OCP\App\IAppManager; use OCP\EventDispatcher\Event; use OCP\Files\Template\ITemplateManager; use OCP\Files\Template\TemplateFileCreator; @@ -20,6 +21,7 @@ use PHPUnit\Framework\TestCase; final class AppMenuActionListenerTest extends TestCase { + private bool $appEnabled = true; private ITemplateManager&MockObject $templateManager; private INavigationManager&MockObject $navigationManager; private IUserSession&MockObject $userSession; @@ -30,6 +32,8 @@ protected function setUp(): void { $l10n = $this->createMock(IL10N::class); $l10n->method('t')->willReturnArgument(0); + $appManager = $this->createMock(IAppManager::class); + $appManager->method('isEnabledForUser')->willReturnCallback(fn (string $app): bool => $app === 'office' && $this->appEnabled); $this->templateManager = $this->createMock(ITemplateManager::class); $this->navigationManager = $this->createMock(INavigationManager::class); $this->userSession = $this->createMock(IUserSession::class); @@ -43,6 +47,7 @@ protected function setUp(): void { ->willReturnCallback(static fn (string $app, string $image): string => "/apps/$app/img/$image"); $this->listener = new AppMenuActionListener( + $appManager, $this->categoryService($l10n, $this->userSession), $this->navigationManager, $urlGenerator, @@ -177,6 +182,7 @@ public function testIgnoresGuests(): void { $userSession->method('getUser')->willReturn(null); $l10n = $this->createMock(IL10N::class); $listener = new AppMenuActionListener( + $this->createMock(IAppManager::class), $this->categoryService($l10n, $userSession), $this->navigationManager, $this->createMock(IURLGenerator::class), @@ -188,6 +194,14 @@ public function testIgnoresGuests(): void { $listener->handle(new LoadAdditionalEntriesEvent()); } + public function testIgnoresUsersTheAppIsNotEnabledFor(): void { + $this->appEnabled = false; + $this->templateManager->expects($this->never())->method('listCreators'); + $this->navigationManager->expects($this->never())->method('add'); + + $this->listener->handle(new LoadAdditionalEntriesEvent()); + } + public function testIgnoresOtherEvents(): void { $this->navigationManager->expects($this->never())->method('add'); diff --git a/tests/unit/Listener/LoadNavigationEntryListenerTest.php b/tests/unit/Listener/LoadNavigationEntryListenerTest.php new file mode 100644 index 00000000..6b664004 --- /dev/null +++ b/tests/unit/Listener/LoadNavigationEntryListenerTest.php @@ -0,0 +1,69 @@ +appManager = $this->createMock(IAppManager::class); + $this->navigationManager = $this->createMock(INavigationManager::class); + $urlGenerator = $this->createMock(IURLGenerator::class); + $urlGenerator->method('linkToRoute')->with('office.page.index')->willReturn('/apps/office/'); + $urlGenerator->method('imagePath')->with('office', 'app.svg')->willReturn('/apps/office/img/app.svg'); + + $this->listener = new LoadNavigationEntryListener($this->appManager, $this->navigationManager, $urlGenerator); + } + + private function registeredEntry(): ?array { + $entry = null; + $this->navigationManager->expects($this->once()) + ->method('add') + ->willReturnCallback(static function (\Closure $closure) use (&$entry): void { + $entry = $closure(); + }); + + $this->listener->handle(new LoadAdditionalEntriesEvent()); + + return $entry; + } + + public function testRegistersTheUntranslatedAppEntry(): void { + $this->appManager->method('isEnabledForUser')->with('office')->willReturn(true); + + $entry = $this->registeredEntry(); + + $this->assertSame('office', $entry['id']); + $this->assertSame('office', $entry['app']); + $this->assertSame('Office', $entry['name']); + $this->assertSame('/apps/office/', $entry['href']); + $this->assertSame('/apps/office/img/app.svg', $entry['icon']); + } + + public function testSkipsEntryForUsersWithoutAccess(): void { + $this->appManager->method('isEnabledForUser')->with('office')->willReturn(false); + + $this->assertNull($this->registeredEntry()); + } + + public function testIgnoresOtherEvents(): void { + $this->navigationManager->expects($this->never())->method('add'); + + $this->listener->handle(new Event()); + } +}