Skip to content
Open
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
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@
'OCP\\Migration\\IRepairStepExpensive' => $baseDir . '/lib/public/Migration/IRepairStepExpensive.php',
'OCP\\Migration\\SimpleMigrationStep' => $baseDir . '/lib/public/Migration/SimpleMigrationStep.php',
'OCP\\Navigation\\Events\\LoadAdditionalEntriesEvent' => $baseDir . '/lib/public/Navigation/Events/LoadAdditionalEntriesEvent.php',
'OCP\\Navigation\\Events\\NavigationEntriesFilterEvent' => $baseDir . '/lib/public/Navigation/Events/NavigationEntriesFilterEvent.php',
'OCP\\Notification\\AlreadyProcessedException' => $baseDir . '/lib/public/Notification/AlreadyProcessedException.php',
'OCP\\Notification\\IAction' => $baseDir . '/lib/public/Notification/IAction.php',
'OCP\\Notification\\IApp' => $baseDir . '/lib/public/Notification/IApp.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 @@ -846,6 +846,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Migration\\IRepairStepExpensive' => __DIR__ . '/../../..' . '/lib/public/Migration/IRepairStepExpensive.php',
'OCP\\Migration\\SimpleMigrationStep' => __DIR__ . '/../../..' . '/lib/public/Migration/SimpleMigrationStep.php',
'OCP\\Navigation\\Events\\LoadAdditionalEntriesEvent' => __DIR__ . '/../../..' . '/lib/public/Navigation/Events/LoadAdditionalEntriesEvent.php',
'OCP\\Navigation\\Events\\NavigationEntriesFilterEvent' => __DIR__ . '/../../..' . '/lib/public/Navigation/Events/NavigationEntriesFilterEvent.php',
'OCP\\Notification\\AlreadyProcessedException' => __DIR__ . '/../../..' . '/lib/public/Notification/AlreadyProcessedException.php',
'OCP\\Notification\\IAction' => __DIR__ . '/../../..' . '/lib/public/Notification/IAction.php',
'OCP\\Notification\\IApp' => __DIR__ . '/../../..' . '/lib/public/Notification/IApp.php',
Expand Down
8 changes: 8 additions & 0 deletions lib/private/NavigationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use OCP\Navigation\Events\NavigationEntriesFilterEvent;
use Override;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -154,6 +155,13 @@ public function getAll(string $type = 'link'): array {
});
}

if ($this->eventDispatcher->hasListeners(NavigationEntriesFilterEvent::class)) {
$event = new NavigationEntriesFilterEvent($result, $type);
$this->eventDispatcher->dispatchTyped($event);
// a listener removes entries; adding one is LoadAdditionalEntriesEvent's job
$result = array_intersect_key($result, $event->getEntries());
}

return $this->proceedNavigation($result, $type);
}

Expand Down
61 changes: 61 additions & 0 deletions lib/public/Navigation/Events/NavigationEntriesFilterEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

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

namespace OCP\Navigation\Events;

use OCP\EventDispatcher\Event;

/**
* Dispatched when the navigation entries are read, before they are sorted,
* so an app can remove entries the current user should not be offered.
*
* Entries can only be removed: an entry added or changed here is ignored.
* Use LoadAdditionalEntriesEvent to add one. Only dispatched when an app
* listens for it.
*
* @since 36.0.0
*/
class NavigationEntriesFilterEvent extends Event {
/**
* @param array<string, array> $entries the entries, keyed by their id
* @param string $type the type that was asked for, see INavigationManager::TYPE_*
* @since 36.0.0
*/
public function __construct(
private array $entries,
private string $type,
) {
parent::__construct();
}

/**
* @return array<string, array> the entries, keyed by their id
* @since 36.0.0
*/
public function getEntries(): array {
return $this->entries;
}

/**
* @param array<string, array> $entries the entries to keep, keyed by their id
* @since 36.0.0
*/
public function setEntries(array $entries): void {
$this->entries = $entries;
}

/**
* The type of entries that was asked for, see INavigationManager::TYPE_*
*
* @since 36.0.0
*/
public function getType(): string {
return $this->type;
}
}
46 changes: 46 additions & 0 deletions tests/lib/NavigationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use OCP\Navigation\Events\NavigationEntriesFilterEvent;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

Expand Down Expand Up @@ -244,6 +245,51 @@ public function testGetAllFiltersActions(): void {
$this->assertEquals(['files', 'logout'], array_keys($this->navigationManager->getAll(INavigationManager::TYPE_ALL)));
}

public function testGetAllLetsListenersRemoveEntries(): void {
$this->navigationManager->add(['id' => 'files', 'name' => 'Files', 'order' => 1, 'href' => 'url']);
$this->navigationManager->add(['id' => 'social', 'name' => 'Social', 'order' => 2, 'href' => 'url']);
$this->navigationManager->add(['id' => 'logout', 'name' => 'Log out', 'order' => 3, 'href' => 'url', 'type' => INavigationManager::TYPE_ACTION]);

$types = [];
$this->dispatcher->method('hasListeners')->with(NavigationEntriesFilterEvent::class)->willReturn(true);
$this->dispatcher->method('dispatchTyped')->willReturnCallback(function ($event) use (&$types): void {
$this->assertInstanceOf(NavigationEntriesFilterEvent::class, $event);
$types[] = $event->getType();
$entries = $event->getEntries();
unset($entries['files']);
$event->setEntries($entries);
});

$this->assertEquals(['social'], array_keys($this->navigationManager->getAll()));
$this->assertEquals(['social', 'logout'], array_keys($this->navigationManager->getAll(INavigationManager::TYPE_ALL)));
$this->assertEquals([INavigationManager::TYPE_APPS, INavigationManager::TYPE_ALL], $types);
}

public function testGetAllIgnoresEntriesAListenerAddsOrChanges(): void {
$this->navigationManager->add(['id' => 'files', 'name' => 'Files', 'order' => 1, 'href' => 'url']);

$this->dispatcher->method('hasListeners')->willReturn(true);
$this->dispatcher->method('dispatchTyped')->willReturnCallback(function (NavigationEntriesFilterEvent $event): void {
$entries = $event->getEntries();
$entries['files']['href'] = 'https://evil.example';
$entries['extra'] = ['id' => 'extra', 'name' => 'Extra', 'order' => 0, 'href' => 'url', 'type' => 'link', 'active' => false];
$event->setEntries($entries);
});

$all = $this->navigationManager->getAll();
$this->assertEquals(['files'], array_keys($all));
$this->assertEquals('url', $all['files']['href']);
}

public function testGetAllDispatchesNoFilterEventWithoutListeners(): void {
$this->navigationManager->add(['id' => 'files', 'name' => 'Files', 'order' => 1, 'href' => 'url']);

$this->dispatcher->method('hasListeners')->willReturn(false);
$this->dispatcher->expects($this->never())->method('dispatchTyped');

$this->assertEquals(['files'], array_keys($this->navigationManager->getAll()));
}

public function testAddArrayClearGetAll(): void {
$entry = [
'id' => 'entry id',
Expand Down
Loading