aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/NavigationManager.php
blob: 83d26c7ca6384192b119432e91bcfd71c8d09065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
='n233' href='#n233'>233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
<?php

/**
 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud GmbH
 * SPDX-License-Identifier: AGPL-3.0-only
 */
namespace OC;

use InvalidArgumentException;
use OC\App\AppManager;
use OC\Group\Manager;
use OCP\App\IAppManager;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\INavigationManager;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
use OCP\Navigation\Events\LoadAdditionalEntriesEvent;
use Psr\Log\LoggerInterface;

/**
 * Manages the ownCloud navigation
 */

class NavigationManager implements INavigationManager {
	protected $entries = [];
	protected $closureEntries = [];
	protected $activeEntry;
	protected $unreadCounters = [];

	/** @var bool */
	protected $init = false;
	/** @var IAppManager|AppManager */
	protected $appManager;
	/** @var IURLGenerator */
	private $urlGenerator;
	/** @var IFactory */
	private $l10nFac;
	/** @var IUserSession */
	private $userSession;
	/** @var Manager */
	private $groupManager;
	/** @var IConfig */
	private $config;
	/** User defined app order (cached for the `add` function) */
	private array $customAppOrder;
	private LoggerInterface $logger;

	public function __construct(
		IAppManager $appManager,
		IURLGenerator $urlGenerator,
		IFactory $l10nFac,
		IUserSession $userSession,
		IGroupManager $groupManager,
		IConfig $config,
		LoggerInterface $logger,
		protected IEventDispatcher $eventDispatcher,
	) {
		$this->appManager = $appManager;
		$this->urlGenerator = $urlGenerator;
		$this->l10nFac = $l10nFac;
		$this->userSession = $userSession;
		$this->groupManager = $groupManager;
		$this->config = $config;
		$this->logger = $logger;
	}

	/**
	 * @inheritDoc
	 */
	public function add($entry) {
		if ($entry instanceof \Closure) {
			$this->closureEntries[] = $entry;
			return;
		}
		$this->init();

		$id = $entry['id'];

		$entry['active'] = false;
		$entry['unread'] = $this->unreadCounters[$id] ?? 0;
		if (!isset($entry['icon'])) {
			$entry['icon'] = '';
		}
		if (!isset($entry['classes'])) {
			$entry['classes'] = '';
		}
		if (!isset($entry['type'])) {
			$entry['type'] = 'link';
		}

		if ($entry['type'] === 'link') {
			// app might not be set when using closures, in this case try to fallback to ID
			if (!isset($entry['app']) && $this->appManager->isEnabledForUser($id)) {
				$entry['app'] = $id;
			}

			// Set order from user defined app order
			$entry['order'] = (int)($this->customAppOrder[$id]['order'] ?? $entry['order'] ?? 100);
		}

		$this->entries[$id] = $entry;

		// Needs to be done after adding the new entry to account for the default entries containing this new entry.
		$this->updateDefaultEntries();
	}

	private function updateDefaultEntries() {
		foreach ($this->entries as $id => $entry) {
			if ($entry['type'] === 'link') {
				$this->entries[$id]['default'] = $id === $this->getDefaultEntryIdForUser($this->userSession->getUser(), false);
			}
		}
	}

	/**
	 * @inheritDoc
	 */
	public function getAll(string $type = 'link'): array {
		$this->init();
		foreach ($this->closureEntries as $c) {
			$this->add($c());
		}
		$this->closureEntries = [];

		$result = $this->entries;
		if ($type !== 'all') {
			$result = array_filter($this->entries, function ($entry) use ($type) {
				return $entry['type'] === $type;
			});
		}

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

	/**
	 * Sort navigation entries default app is always sorted first, then by order, name and set active flag
	 *
	 * @param array $list
	 * @return array
	 */
	private function proceedNavigation(array $list, string $type): array {
		uasort($list, function ($a, $b) {
			if (($a['default'] ?? false) xor ($b['default'] ?? false)) {
				// Always sort the default app first
				return ($a['default'] ?? false) ? -1 : 1;
			} elseif (isset($a['order']) && isset($b['order'])) {
				// Sort by order
				return ($a['order'] < $b['order']) ? -1 : 1;
			} elseif (isset($a['order']) || isset($b['order'])) {
				// Sort the one that has an order property first
				return isset($a['order']) ? -1 : 1;
			} else {
				// Sort by name otherwise
				return ($a['name'] < $b['name']) ? -1 : 1;
			}
		});

		if ($type === 'all' || $type === 'link') {
			// There might be the case that no default app was set, in this case the first app is the default app.
			// Otherwise the default app is already the ordered first, so setting the default prop will make no difference.
			foreach ($list as $index => &$navEntry) {
				if ($navEntry['type'] === 'link') {
					$navEntry['default'] = true;
					break;
				}
			}
			unset($navEntry);
		}

		$activeEntry = $this->getActiveEntry();
		if ($activeEntry !== null) {
			foreach ($list as $index => &$navEntry) {
				if ($navEntry['id'] == $activeEntry) {
					$navEntry['active'] = true;
				} else {
					$navEntry['active'] = false;
				}
			}
			unset($navEntry);
		}

		return $list;
	}


	/**
	 * removes all the entries
	 */
	public function clear($loadDefaultLinks = true) {
		$this->entries = [];
		$this->closureEntries = [];
		$this->init = !$loadDefaultLinks;
	}

	/**
	 * @inheritDoc
	 */
	public function setActiveEntry($appId) {
		$this->activeEntry = $appId;
	}

	/**
	 * @inheritDoc
	 */
	public function getActiveEntry() {
		return $this->activeEntry;
	}

	private function init() {
		if ($this->init) {
			return;
		}
		$this->init = true;

		$l = $this->l10nFac->get('lib');
		if ($this->config->getSystemValueBool('knowledgebaseenabled', true)) {
			$this->add([
				'type' => 'settings',
				'id' => 'help',
				'order' => 99998,
				'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
				'name' => $l->t('Help & privacy'),
				'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
			]);
		}

		if ($this->userSession->isLoggedIn()) {
			// Profile
			$this->add([
				'type' => 'settings',
				'id' => 'profile',
				'order' => 1,
				'href' => $this->urlGenerator->linkToRoute(
					'profile.ProfilePage.index',
					['targetUserId' => $this->userSession->getUser()->getUID()],
				),
				'name' => $l->t('View profile'),
			]);

			// Accessibility settings
			if ($this->appManager->isEnabledForUser('theming', $this->userSession->getUser())) {
				$this->add([
					'type' => 'settings',
					'id' => 'accessibility_settings',
					'order' => 2,
					'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'theming']),
					'name' => $l->t('Appearance and accessibility'),
					'icon' => $this->urlGenerator->imagePath('theming', 'accessibility-dark.svg'),
				]);
			}

			if ($this->isAdmin()) {
				// App management
				$this->add([
					'type' => 'settings',
					'id' => 'core_apps',
					'order' => 5,
					'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
					'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
					'name' => $l->t('Apps'),
				]);

				// Personal settings
				$this->add([
					'type' => 'settings',
					'id' => 'settings',
					'order' => 3,
					'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
					'name' => $l->t('Personal settings'),
					'icon' => $this->urlGenerator->imagePath('settings', 'personal.svg'),
				]);

				// Admin settings
				$this->add([
					'type' => 'settings',
					'id' => 'admin_settings',
					'order' => 4,
					'href' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview']),
					'name' => $l->t('Administration settings'),
					'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
				]);
			} else {
				// Personal settings
				$this->add([
					'type' => 'settings',
					'id' => 'settings',
					'order' => 3,
					'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
					'name' => $l->t('Settings'),
					'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
				]);
			}

			$logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
			if ($logoutUrl !== '') {
				// Logout
				$this->add([
					'type' => 'settings',
					'id' => 'logout',
					'order' => 99999,
					'href' => $logoutUrl,
					'name' => $l->t('Log out'),
					'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
				]);
			}

			if ($this->isSubadmin()) {
				// User management
				$this->add([
					'type' => 'settings',
					'id' => 'core_users',
					'order' => 6,
					'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
					'name' => $l->t('Accounts'),
					'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
				]);
			}
		}
		$this->eventDispatcher->dispatchTyped(new LoadAdditionalEntriesEvent());

		if ($this->userSession->isLoggedIn()) {
			$user = $this->userSession->getUser();
			$apps = $this->appManager->getEnabledAppsForUser($user);
			$this->customAppOrder = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR);
		} else {
			$apps = $this->appManager->getEnabledApps();
			$this->customAppOrder = [];
		}

		foreach ($apps as $app) {
			if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
				continue;
			}

			// load plugins and collections from info.xml
			$info = $this->appManager->getAppInfo($app);
			if (!isset($info['navigations']['navigation'])) {
				continue;
			}
			foreach ($info['navigations']['navigation'] as $key => $nav) {
				$nav['type'] = $nav['type'] ?? 'link';
				if (!isset($nav['name'])) {
					continue;
				}
				// Allow settings navigation items with no route entry, all other types require one
				if (!isset($nav['route']) && $nav['type'] !== 'settings') {
					continue;
				}
				$role = $nav['@attributes']['role'] ?? 'all';
				if ($role === 'admin' && !$this->isAdmin()) {
					continue;
				}
				$l = $this->l10nFac->get($app);
				$id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
				$order = $nav['order'] ?? 100;
				$type = $nav['type'];
				$route = !empty($nav['route']) ? $this->urlGenerator->linkToRoute($nav['route']) : '';
				$icon = $nav['icon'] ?? null;
				if ($icon !== null) {
					try {
						$icon = $this->urlGenerator->imagePath($app, $icon);
					} catch (\RuntimeException $ex) {
						// ignore
					}
				}
				if ($icon === null) {
					$icon = $this->appManager->getAppIcon($app);
				}
				if ($icon === null) {
					$icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
				}

				$this->add(array_merge([
					// Navigation id
					'id' => $id,
					// Order where this entry should be shown
					'order' => $order,
					// Target of the navigation entry
					'href' => $route,
					// The icon used for the naviation entry
					'icon' => $icon,
					// Type of the navigation entry ('link' vs 'settings')
					'type' => $type,
					// Localized name of the navigation entry
					'name' => $l->t($nav['name']),
				], $type === 'link' ? [
					// App that registered this navigation entry (not necessarly the same as the id)
					'app' => $app,
				] : []
				));
			}
		}
	}

	private function isAdmin() {
		$user = $this->userSession->getUser();
		if ($user !== null) {
			return $this->groupManager->isAdmin($user->getUID());
		}
		return false;
	}

	private function isSubadmin() {
		$user = $this->userSession->getUser();
		if ($user !== null) {
			return $this->groupManager->getSubAdmin()->isSubAdmin($user);
		}
		return false;
	}

	public function setUnreadCounter(string $id, int $unreadCounter): void {
		$this->unreadCounters[$id] = $unreadCounter;
	}

	public function get(string $id): ?array {
		$this->init();
		foreach ($this->closureEntries as $c) {
			$this->add($c());
		}
		$this->closureEntries = [];

		return $this->entries[$id];
	}

	public function getDefaultEntryIdForUser(?IUser $user = null, bool $withFallbacks = true): string {
		$this->init();
		// Disable fallbacks here, as we need to override them with the user defaults if none are configured.
		$defaultEntryIds = $this->getDefaultEntryIds(false);

		$user ??= $this->userSession->getUser();

		if ($user !== null) {
			$userDefaultEntryIds = explode(',', $this->config->getUserValue($user->getUID(), 'core', 'defaultapp'));
			$defaultEntryIds = array_filter(array_merge($userDefaultEntryIds, $defaultEntryIds));
			if (empty($defaultEntryIds) && $withFallbacks) {
				/* Fallback on user defined apporder */
				$customOrders = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags: JSON_THROW_ON_ERROR);
				if (!empty($customOrders)) {
					// filter only entries with app key (when added using closures or NavigationManager::add the app is not guaranteed to be set)
					$customOrders = array_filter($customOrders, static fn ($entry) => isset($entry['app']));
					// sort apps by order
					usort($customOrders, static fn ($a, $b) => $a['order'] - $b['order']);
					// set default apps to sorted apps
					$defaultEntryIds = array_map(static fn ($entry) => $entry['app'], $customOrders);
				}
			}
		}

		if (empty($defaultEntryIds) && $withFallbacks) {
			$defaultEntryIds = ['dashboard','files'];
		}

		$entryIds = array_keys($this->entries);

		// Find the first app that is enabled for the current user
		foreach ($defaultEntryIds as $defaultEntryId) {
			if (in_array($defaultEntryId, $entryIds, true)) {
				return $defaultEntryId;
			}
		}

		// Set fallback to always-enabled files app
		return $withFallbacks ? 'files' : '';
	}

	public function getDefaultEntryIds(bool $withFallbacks = true): array {
		$this->init();
		$storedIds = explode(',', $this->config->getSystemValueString('defaultapp', $withFallbacks ? 'dashboard,files' : ''));
		$ids = [];
		$entryIds = array_keys($this->entries);
		foreach ($storedIds as $id) {
			if (in_array($id, $entryIds, true)) {
				$ids[] = $id;
				break;
			}
		}
		return array_filter($ids);
	}

	public function setDefaultEntryIds(array $ids): void {
		$this->init();
		$entryIds = array_keys($this->entries);

		foreach ($ids as $id) {
			if (!in_array($id, $entryIds, true)) {
				$this->logger->debug('Cannot set unavailable entry as default entry', ['missing_entry' => $id]);
				throw new InvalidArgumentException('Entry not available');
			}
		}

		$this->config->setSystemValue('defaultapp', join(',', $ids));
	}
}