NavigationManager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OC\App\AppManager;
  9. use OC\Group\Manager;
  10. use OCP\App\IAppManager;
  11. use OCP\IConfig;
  12. use OCP\IGroupManager;
  13. use OCP\INavigationManager;
  14. use OCP\IURLGenerator;
  15. use OCP\IUserSession;
  16. use OCP\L10N\IFactory;
  17. /**
  18. * Manages the ownCloud navigation
  19. */
  20. class NavigationManager implements INavigationManager {
  21. protected $entries = [];
  22. protected $closureEntries = [];
  23. protected $activeEntry;
  24. protected $unreadCounters = [];
  25. /** @var bool */
  26. protected $init = false;
  27. /** @var IAppManager|AppManager */
  28. protected $appManager;
  29. /** @var IURLGenerator */
  30. private $urlGenerator;
  31. /** @var IFactory */
  32. private $l10nFac;
  33. /** @var IUserSession */
  34. private $userSession;
  35. /** @var Manager */
  36. private $groupManager;
  37. /** @var IConfig */
  38. private $config;
  39. /** The default app for the current user (cached for the `add` function) */
  40. private ?string $defaultApp;
  41. /** User defined app order (cached for the `add` function) */
  42. private array $customAppOrder;
  43. public function __construct(IAppManager $appManager,
  44. IURLGenerator $urlGenerator,
  45. IFactory $l10nFac,
  46. IUserSession $userSession,
  47. IGroupManager $groupManager,
  48. IConfig $config) {
  49. $this->appManager = $appManager;
  50. $this->urlGenerator = $urlGenerator;
  51. $this->l10nFac = $l10nFac;
  52. $this->userSession = $userSession;
  53. $this->groupManager = $groupManager;
  54. $this->config = $config;
  55. $this->defaultApp = null;
  56. }
  57. /**
  58. * @inheritDoc
  59. */
  60. public function add($entry) {
  61. if ($entry instanceof \Closure) {
  62. $this->closureEntries[] = $entry;
  63. return;
  64. }
  65. $this->init();
  66. $id = $entry['id'];
  67. $entry['active'] = false;
  68. $entry['unread'] = $this->unreadCounters[$id] ?? 0;
  69. if (!isset($entry['icon'])) {
  70. $entry['icon'] = '';
  71. }
  72. if (!isset($entry['classes'])) {
  73. $entry['classes'] = '';
  74. }
  75. if (!isset($entry['type'])) {
  76. $entry['type'] = 'link';
  77. }
  78. if ($entry['type'] === 'link') {
  79. // app might not be set when using closures, in this case try to fallback to ID
  80. if (!isset($entry['app']) && $this->appManager->isEnabledForUser($id)) {
  81. $entry['app'] = $id;
  82. }
  83. // This is the default app that will always be shown first
  84. $entry['default'] = ($entry['app'] ?? false) === $this->defaultApp;
  85. // Set order from user defined app order
  86. $entry['order'] = (int)($this->customAppOrder[$id]['order'] ?? $entry['order'] ?? 100);
  87. }
  88. $this->entries[$id] = $entry;
  89. }
  90. /**
  91. * @inheritDoc
  92. */
  93. public function getAll(string $type = 'link'): array {
  94. $this->init();
  95. foreach ($this->closureEntries as $c) {
  96. $this->add($c());
  97. }
  98. $this->closureEntries = [];
  99. $result = $this->entries;
  100. if ($type !== 'all') {
  101. $result = array_filter($this->entries, function ($entry) use ($type) {
  102. return $entry['type'] === $type;
  103. });
  104. }
  105. return $this->proceedNavigation($result, $type);
  106. }
  107. /**
  108. * Sort navigation entries default app is always sorted first, then by order, name and set active flag
  109. *
  110. * @param array $list
  111. * @return array
  112. */
  113. private function proceedNavigation(array $list, string $type): array {
  114. uasort($list, function ($a, $b) {
  115. if (($a['default'] ?? false) xor ($b['default'] ?? false)) {
  116. // Always sort the default app first
  117. return ($a['default'] ?? false) ? -1 : 1;
  118. } elseif (isset($a['order']) && isset($b['order'])) {
  119. // Sort by order
  120. return ($a['order'] < $b['order']) ? -1 : 1;
  121. } elseif (isset($a['order']) || isset($b['order'])) {
  122. // Sort the one that has an order property first
  123. return isset($a['order']) ? -1 : 1;
  124. } else {
  125. // Sort by name otherwise
  126. return ($a['name'] < $b['name']) ? -1 : 1;
  127. }
  128. });
  129. if ($type === 'all' || $type === 'link') {
  130. // There might be the case that no default app was set, in this case the first app is the default app.
  131. // Otherwise the default app is already the ordered first, so setting the default prop will make no difference.
  132. foreach ($list as $index => &$navEntry) {
  133. if ($navEntry['type'] === 'link') {
  134. $navEntry['default'] = true;
  135. break;
  136. }
  137. }
  138. unset($navEntry);
  139. }
  140. $activeApp = $this->getActiveEntry();
  141. if ($activeApp !== null) {
  142. foreach ($list as $index => &$navEntry) {
  143. if ($navEntry['id'] == $activeApp) {
  144. $navEntry['active'] = true;
  145. } else {
  146. $navEntry['active'] = false;
  147. }
  148. }
  149. unset($navEntry);
  150. }
  151. return $list;
  152. }
  153. /**
  154. * removes all the entries
  155. */
  156. public function clear($loadDefaultLinks = true) {
  157. $this->entries = [];
  158. $this->closureEntries = [];
  159. $this->init = !$loadDefaultLinks;
  160. }
  161. /**
  162. * @inheritDoc
  163. */
  164. public function setActiveEntry($appId) {
  165. $this->activeEntry = $appId;
  166. }
  167. /**
  168. * @inheritDoc
  169. */
  170. public function getActiveEntry() {
  171. return $this->activeEntry;
  172. }
  173. private function init() {
  174. if ($this->init) {
  175. return;
  176. }
  177. $this->init = true;
  178. $l = $this->l10nFac->get('lib');
  179. if ($this->config->getSystemValueBool('knowledgebaseenabled', true)) {
  180. $this->add([
  181. 'type' => 'settings',
  182. 'id' => 'help',
  183. 'order' => 99998,
  184. 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
  185. 'name' => $l->t('Help & privacy'),
  186. 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
  187. ]);
  188. }
  189. $this->defaultApp = $this->appManager->getDefaultAppForUser($this->userSession->getUser(), false);
  190. if ($this->userSession->isLoggedIn()) {
  191. // Profile
  192. $this->add([
  193. 'type' => 'settings',
  194. 'id' => 'profile',
  195. 'order' => 1,
  196. 'href' => $this->urlGenerator->linkToRoute(
  197. 'core.ProfilePage.index',
  198. ['targetUserId' => $this->userSession->getUser()->getUID()],
  199. ),
  200. 'name' => $l->t('View profile'),
  201. ]);
  202. // Accessibility settings
  203. if ($this->appManager->isEnabledForUser('theming', $this->userSession->getUser())) {
  204. $this->add([
  205. 'type' => 'settings',
  206. 'id' => 'accessibility_settings',
  207. 'order' => 2,
  208. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'theming']),
  209. 'name' => $l->t('Appearance and accessibility'),
  210. 'icon' => $this->urlGenerator->imagePath('theming', 'accessibility-dark.svg'),
  211. ]);
  212. }
  213. if ($this->isAdmin()) {
  214. // App management
  215. $this->add([
  216. 'type' => 'settings',
  217. 'id' => 'core_apps',
  218. 'order' => 5,
  219. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  220. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  221. 'name' => $l->t('Apps'),
  222. ]);
  223. // Personal settings
  224. $this->add([
  225. 'type' => 'settings',
  226. 'id' => 'settings',
  227. 'order' => 3,
  228. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  229. 'name' => $l->t('Personal settings'),
  230. 'icon' => $this->urlGenerator->imagePath('settings', 'personal.svg'),
  231. ]);
  232. // Admin settings
  233. $this->add([
  234. 'type' => 'settings',
  235. 'id' => 'admin_settings',
  236. 'order' => 4,
  237. 'href' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview']),
  238. 'name' => $l->t('Administration settings'),
  239. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  240. ]);
  241. } else {
  242. // Personal settings
  243. $this->add([
  244. 'type' => 'settings',
  245. 'id' => 'settings',
  246. 'order' => 3,
  247. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  248. 'name' => $l->t('Settings'),
  249. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  250. ]);
  251. }
  252. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  253. if ($logoutUrl !== '') {
  254. // Logout
  255. $this->add([
  256. 'type' => 'settings',
  257. 'id' => 'logout',
  258. 'order' => 99999,
  259. 'href' => $logoutUrl,
  260. 'name' => $l->t('Log out'),
  261. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  262. ]);
  263. }
  264. if ($this->isSubadmin()) {
  265. // User management
  266. $this->add([
  267. 'type' => 'settings',
  268. 'id' => 'core_users',
  269. 'order' => 6,
  270. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  271. 'name' => $l->t('Accounts'),
  272. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  273. ]);
  274. }
  275. }
  276. if ($this->userSession->isLoggedIn()) {
  277. $user = $this->userSession->getUser();
  278. $apps = $this->appManager->getEnabledAppsForUser($user);
  279. $this->customAppOrder = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR);
  280. } else {
  281. $apps = $this->appManager->getInstalledApps();
  282. $this->customAppOrder = [];
  283. }
  284. foreach ($apps as $app) {
  285. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  286. continue;
  287. }
  288. // load plugins and collections from info.xml
  289. $info = $this->appManager->getAppInfo($app);
  290. if (!isset($info['navigations']['navigation'])) {
  291. continue;
  292. }
  293. foreach ($info['navigations']['navigation'] as $key => $nav) {
  294. $nav['type'] = $nav['type'] ?? 'link';
  295. if (!isset($nav['name'])) {
  296. continue;
  297. }
  298. // Allow settings navigation items with no route entry, all other types require one
  299. if (!isset($nav['route']) && $nav['type'] !== 'settings') {
  300. continue;
  301. }
  302. $role = $nav['@attributes']['role'] ?? 'all';
  303. if ($role === 'admin' && !$this->isAdmin()) {
  304. continue;
  305. }
  306. $l = $this->l10nFac->get($app);
  307. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  308. $order = $nav['order'] ?? 100;
  309. $type = $nav['type'];
  310. $route = !empty($nav['route']) ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  311. $icon = $nav['icon'] ?? null;
  312. if ($icon !== null) {
  313. try {
  314. $icon = $this->urlGenerator->imagePath($app, $icon);
  315. } catch (\RuntimeException $ex) {
  316. // ignore
  317. }
  318. }
  319. if ($icon === null) {
  320. $icon = $this->appManager->getAppIcon($app);
  321. }
  322. if ($icon === null) {
  323. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  324. }
  325. $this->add(array_merge([
  326. // Navigation id
  327. 'id' => $id,
  328. // Order where this entry should be shown
  329. 'order' => $order,
  330. // Target of the navigation entry
  331. 'href' => $route,
  332. // The icon used for the naviation entry
  333. 'icon' => $icon,
  334. // Type of the navigation entry ('link' vs 'settings')
  335. 'type' => $type,
  336. // Localized name of the navigation entry
  337. 'name' => $l->t($nav['name']),
  338. ], $type === 'link' ? [
  339. // App that registered this navigation entry (not necessarly the same as the id)
  340. 'app' => $app,
  341. ] : []
  342. ));
  343. }
  344. }
  345. }
  346. private function isAdmin() {
  347. $user = $this->userSession->getUser();
  348. if ($user !== null) {
  349. return $this->groupManager->isAdmin($user->getUID());
  350. }
  351. return false;
  352. }
  353. private function isSubadmin() {
  354. $user = $this->userSession->getUser();
  355. if ($user !== null) {
  356. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  357. }
  358. return false;
  359. }
  360. public function setUnreadCounter(string $id, int $unreadCounter): void {
  361. $this->unreadCounters[$id] = $unreadCounter;
  362. }
  363. }