Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use OCA\AppAPI\Listener\DeclarativeSettings\SetValueListener;
use OCA\AppAPI\Listener\FileEventsListener;
use OCA\AppAPI\Listener\LoadFilesPluginListener;
use OCA\AppAPI\Listener\LoadMenuEntriesListener;
use OCA\AppAPI\Listener\SabrePluginAuthInitListener;
use OCA\AppAPI\Middleware\AppAPIAuthMiddleware;
use OCA\AppAPI\Middleware\ExAppUIL10NMiddleware;
Expand All @@ -26,7 +27,6 @@
use OCA\AppAPI\Service\ProvidersAI\TaskProcessingService;
use OCA\AppAPI\Service\ProvidersAI\TextProcessingService;
use OCA\AppAPI\Service\ProvidersAI\TranslationService;
use OCA\AppAPI\Service\UI\TopMenuService;
use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCP\AppFramework\App;
Expand All @@ -41,13 +41,13 @@
use OCP\Files\Events\Node\NodeTouchedEvent;
use OCP\Files\Events\Node\NodeWrittenEvent;
use OCP\IConfig;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use OCP\SabrePluginEvent;
use OCP\Settings\Events\DeclarativeSettingsGetValueEvent;
use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent;
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Throwable;

class Application extends App implements IBootstrap {
public const APP_ID = 'app_api';
Expand All @@ -64,6 +64,7 @@ public function __construct(array $urlParams = []) {
* @psalm-suppress UndefinedClass
*/
public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalEntriesEvent::class, LoadMenuEntriesListener::class);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadFilesPluginListener::class);
$context->registerCapability(Capabilities::class);
$context->registerCapability(PublicCapabilities::class);
Expand Down Expand Up @@ -109,10 +110,6 @@ public function register(IRegistrationContext $context): void {
}

public function boot(IBootContext $context): void {
try {
$context->injectFn($this->registerExAppsMenuEntries(...));
} catch (NotFoundExceptionInterface|ContainerExceptionInterface|Throwable) {
}
}

public function registerDavAuth(): void {
Expand All @@ -123,10 +120,4 @@ public function registerDavAuth(): void {
$event->getServer()->addPlugin($container->query(DavPlugin::class));
});
}

private function registerExAppsMenuEntries(): void {
$container = $this->getContainer();
$menuEntryService = $container->get(TopMenuService::class);
$menuEntryService->registerMenuEntries($container);
}
}
80 changes: 80 additions & 0 deletions lib/Listener/LoadMenuEntriesListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\AppAPI\Listener;

use OCA\AppAPI\AppInfo\Application;
use OCA\AppAPI\Service\UI\TopMenuService;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUserSession;

use OCP\L10N\IFactory;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use OCP\Server;

/**
* @template-extends IEventListener<LoadMenuEntriesListener>
*/
class LoadMenuEntriesListener implements IEventListener {

public function __construct(
private readonly TopMenuService $topMenuService,
) {
}

public function handle(Event $event): void {
if (!$event instanceof LoadAdditionalEntriesEvent) {
return;
}

$menuEntries = $this->topMenuService->getExAppMenuEntries();
if (empty($menuEntries)) {
return;
}

$user = Server::get(IUserSession::class)->getUser();
if (!$user) {
return;
}
$isUserAdmin = Server::get(IGroupManager::class)->isAdmin($user->getUID());

/** @var INavigationManager $navigationManager */
$navigationManager = Server::get(INavigationManager::class);

foreach ($menuEntries as $menuEntry) {
if ($menuEntry->getAdminRequired() === 1 && !$isUserAdmin) {
continue; // Skip this entry if the user is not an admin and the entry requires admin privileges
}
$navigationManager->add(static function () use ($menuEntry) {
$appId = $menuEntry->getAppid();
$entryName = $menuEntry->getName();
$icon = $menuEntry->getIcon();
$urlGenerator = Server::get(IURLGenerator::class);
return [
'id' => Application::APP_ID . '_' . $appId . '_' . $entryName,
'type' => 'link',
'app' => Application::APP_ID,
'href' => $urlGenerator->linkToRoute(
'app_api.TopMenu.viewExAppPage', ['appId' => $appId, 'name' => $entryName]
),
'icon' => $icon === '' ?
$urlGenerator->imagePath('app_api', 'app.svg') :
$urlGenerator->linkToRoute(
'app_api.ExAppProxy.ExAppGet', ['appId' => $appId, 'other' => $icon]
),
'name' => Server::get(IFactory::class)->get($appId)->t($menuEntry->getDisplayName()),
];
});
}
}
}
50 changes: 0 additions & 50 deletions lib/Service/UI/TopMenuService.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,6 @@
use OCP\DB\Exception;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Log\LoggerInterface;

class TopMenuService {
Expand All @@ -44,47 +35,6 @@ public function __construct(
}
}

/**
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws Exception
*/
public function registerMenuEntries(ContainerInterface $container): void {
/** @var TopMenu $menuEntry */
foreach ($this->getExAppMenuEntries() as $menuEntry) {
$userSession = $container->get(IUserSession::class);
/** @var IGroupManager $groupManager */
$groupManager = $container->get(IGroupManager::class);
/** @var IUser $user */
$user = $userSession->getUser();
if ($menuEntry->getAdminRequired() === 1 && !$groupManager->isAdmin($user->getUID())) {
continue; // Skip this entry if user is not admin and entry requires admin privileges
}
$container->get(INavigationManager::class)->add(function () use ($container, $menuEntry) {
$urlGenerator = $container->get(IURLGenerator::class);
/** @var IFactory $l10nFactory */
$l10nFactory = $container->get(IFactory::class);
$appId = $menuEntry->getAppid();
$entryName = $menuEntry->getName();
$icon = $menuEntry->getIcon();
return [
'id' => Application::APP_ID . '_' . $appId . '_' . $entryName,
'type' => 'link',
'app' => Application::APP_ID,
'href' => $urlGenerator->linkToRoute(
'app_api.TopMenu.viewExAppPage', ['appId' => $appId, 'name' => $entryName]
),
'icon' => $icon === '' ?
$urlGenerator->imagePath('app_api', 'app.svg') :
$urlGenerator->linkToRoute(
'app_api.ExAppProxy.ExAppGet', ['appId' => $appId, 'other' => $icon]
),
'name' => $l10nFactory->get($appId)->t($menuEntry->getDisplayName()),
];
});
}
}

public function registerExAppMenuEntry(string $appId, string $name, string $displayName,
string $icon, int $adminRequired): ?TopMenu {
$menuEntry = $this->getExAppMenuEntry($appId, $name);
Expand Down
18 changes: 18 additions & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<file src="lib/AppInfo/Application.php">
<InvalidArgument>
<code><![CDATA[LoadFilesPluginListener::class]]></code>
<code><![CDATA[LoadMenuEntriesListener::class]]></code>
<code><![CDATA[SabrePluginAuthInitListener::class]]></code>
</InvalidArgument>
<MissingDependency>
Expand Down Expand Up @@ -97,6 +98,23 @@
<code><![CDATA[LoadAdditionalScriptsEvent]]></code>
</UndefinedClass>
</file>
<file src="lib/Listener/LoadMenuEntriesListener.php">
<ImplementedParamTypeMismatch>
<code><![CDATA[$event]]></code>
</ImplementedParamTypeMismatch>
<InvalidDocblock>
<code><![CDATA[class LoadMenuEntriesListener implements IEventListener {]]></code>
</InvalidDocblock>
<InvalidTemplateParam>
<code><![CDATA[IEventListener]]></code>
</InvalidTemplateParam>
<MissingTemplateParam>
<code><![CDATA[IEventListener]]></code>
</MissingTemplateParam>
<UndefinedClass>
<code><![CDATA[LoadAdditionalEntriesEvent]]></code>
</UndefinedClass>
</file>
<file src="lib/Listener/SabrePluginAuthInitListener.php">
<ImplementedParamTypeMismatch>
<code><![CDATA[$event]]></code>
Expand Down
Loading