NavigationManager.php 12 KB

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