1
0

NavigationManager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. public function __construct(IAppManager $appManager,
  65. IURLGenerator $urlGenerator,
  66. IFactory $l10nFac,
  67. IUserSession $userSession,
  68. IGroupManager $groupManager,
  69. IConfig $config) {
  70. $this->appManager = $appManager;
  71. $this->urlGenerator = $urlGenerator;
  72. $this->l10nFac = $l10nFac;
  73. $this->userSession = $userSession;
  74. $this->groupManager = $groupManager;
  75. $this->config = $config;
  76. }
  77. /**
  78. * @inheritDoc
  79. */
  80. public function add($entry) {
  81. if ($entry instanceof \Closure) {
  82. $this->closureEntries[] = $entry;
  83. return;
  84. }
  85. $entry['active'] = false;
  86. if (!isset($entry['icon'])) {
  87. $entry['icon'] = '';
  88. }
  89. if (!isset($entry['classes'])) {
  90. $entry['classes'] = '';
  91. }
  92. if (!isset($entry['type'])) {
  93. $entry['type'] = 'link';
  94. }
  95. $id = $entry['id'];
  96. $entry['unread'] = $this->unreadCounters[$id] ?? 0;
  97. $this->entries[$id] = $entry;
  98. }
  99. /**
  100. * @inheritDoc
  101. */
  102. public function getAll(string $type = 'link'): array {
  103. $this->init();
  104. foreach ($this->closureEntries as $c) {
  105. $this->add($c());
  106. }
  107. $this->closureEntries = [];
  108. $result = $this->entries;
  109. if ($type !== 'all') {
  110. $result = array_filter($this->entries, function ($entry) use ($type) {
  111. return $entry['type'] === $type;
  112. });
  113. }
  114. return $this->proceedNavigation($result, $type);
  115. }
  116. /**
  117. * Sort navigation entries default app is always sorted first, then by order, name and set active flag
  118. *
  119. * @param array $list
  120. * @return array
  121. */
  122. private function proceedNavigation(array $list, string $type): array {
  123. uasort($list, function ($a, $b) {
  124. if (($a['default'] ?? false) xor ($b['default'] ?? false)) {
  125. // Always sort the default app first
  126. return ($a['default'] ?? false) ? -1 : 1;
  127. } elseif (isset($a['order']) && isset($b['order'])) {
  128. // Sort by order
  129. return ($a['order'] < $b['order']) ? -1 : 1;
  130. } elseif (isset($a['order']) || isset($b['order'])) {
  131. // Sort the one that has an order property first
  132. return isset($a['order']) ? -1 : 1;
  133. } else {
  134. // Sort by name otherwise
  135. return ($a['name'] < $b['name']) ? -1 : 1;
  136. }
  137. });
  138. if ($type === 'all' || $type === 'link') {
  139. // There might be the case that no default app was set, in this case the first app is the default app.
  140. // Otherwise the default app is already the ordered first, so setting the default prop will make no difference.
  141. foreach ($list as $index => &$navEntry) {
  142. if ($navEntry['type'] === 'link') {
  143. $navEntry['default'] = true;
  144. break;
  145. }
  146. }
  147. unset($navEntry);
  148. }
  149. $activeApp = $this->getActiveEntry();
  150. if ($activeApp !== null) {
  151. foreach ($list as $index => &$navEntry) {
  152. if ($navEntry['id'] == $activeApp) {
  153. $navEntry['active'] = true;
  154. } else {
  155. $navEntry['active'] = false;
  156. }
  157. }
  158. unset($navEntry);
  159. }
  160. return $list;
  161. }
  162. /**
  163. * removes all the entries
  164. */
  165. public function clear($loadDefaultLinks = true) {
  166. $this->entries = [];
  167. $this->closureEntries = [];
  168. $this->init = !$loadDefaultLinks;
  169. }
  170. /**
  171. * @inheritDoc
  172. */
  173. public function setActiveEntry($appId) {
  174. $this->activeEntry = $appId;
  175. }
  176. /**
  177. * @inheritDoc
  178. */
  179. public function getActiveEntry() {
  180. return $this->activeEntry;
  181. }
  182. private function init() {
  183. if ($this->init) {
  184. return;
  185. }
  186. $this->init = true;
  187. $l = $this->l10nFac->get('lib');
  188. if ($this->config->getSystemValueBool('knowledgebaseenabled', true)) {
  189. $this->add([
  190. 'type' => 'settings',
  191. 'id' => 'help',
  192. 'order' => 99998,
  193. 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
  194. 'name' => $l->t('Help'),
  195. 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
  196. ]);
  197. }
  198. if ($this->userSession->isLoggedIn()) {
  199. // Accessibility settings
  200. if ($this->appManager->isEnabledForUser('theming', $this->userSession->getUser())) {
  201. $this->add([
  202. 'type' => 'settings',
  203. 'id' => 'accessibility_settings',
  204. 'order' => 2,
  205. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'theming']),
  206. 'name' => $l->t('Appearance and accessibility'),
  207. 'icon' => $this->urlGenerator->imagePath('theming', 'accessibility-dark.svg'),
  208. ]);
  209. }
  210. if ($this->isAdmin()) {
  211. // App management
  212. $this->add([
  213. 'type' => 'settings',
  214. 'id' => 'core_apps',
  215. 'order' => 5,
  216. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  217. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  218. 'name' => $l->t('Apps'),
  219. ]);
  220. // Personal settings
  221. $this->add([
  222. 'type' => 'settings',
  223. 'id' => 'settings',
  224. 'order' => 3,
  225. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  226. 'name' => $l->t('Personal settings'),
  227. 'icon' => $this->urlGenerator->imagePath('settings', 'personal.svg'),
  228. ]);
  229. // Admin settings
  230. $this->add([
  231. 'type' => 'settings',
  232. 'id' => 'admin_settings',
  233. 'order' => 4,
  234. 'href' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview']),
  235. 'name' => $l->t('Administration settings'),
  236. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  237. ]);
  238. } else {
  239. // Personal settings
  240. $this->add([
  241. 'type' => 'settings',
  242. 'id' => 'settings',
  243. 'order' => 3,
  244. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  245. 'name' => $l->t('Settings'),
  246. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  247. ]);
  248. }
  249. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  250. if ($logoutUrl !== '') {
  251. // Logout
  252. $this->add([
  253. 'type' => 'settings',
  254. 'id' => 'logout',
  255. 'order' => 99999,
  256. 'href' => $logoutUrl,
  257. 'name' => $l->t('Log out'),
  258. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  259. ]);
  260. }
  261. if ($this->isSubadmin()) {
  262. // User management
  263. $this->add([
  264. 'type' => 'settings',
  265. 'id' => 'core_users',
  266. 'order' => 6,
  267. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  268. 'name' => $l->t('Users'),
  269. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  270. ]);
  271. }
  272. }
  273. if ($this->appManager === 'null') {
  274. return;
  275. }
  276. if ($this->userSession->isLoggedIn()) {
  277. $user = $this->userSession->getUser();
  278. $apps = $this->appManager->getEnabledAppsForUser($user);
  279. $customOrders = json_decode($this->config->getUserValue($user->getUID(), 'core', 'apporder', '[]'), true, flags:JSON_THROW_ON_ERROR);
  280. } else {
  281. $apps = $this->appManager->getInstalledApps();
  282. $customOrders = [];
  283. }
  284. // The default app of the current user without fallbacks
  285. $defaultApp = $this->appManager->getDefaultAppForUser($this->userSession->getUser(), false);
  286. foreach ($apps as $app) {
  287. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  288. continue;
  289. }
  290. // load plugins and collections from info.xml
  291. $info = $this->appManager->getAppInfo($app);
  292. if (!isset($info['navigations']['navigation'])) {
  293. continue;
  294. }
  295. foreach ($info['navigations']['navigation'] as $key => $nav) {
  296. $nav['type'] = $nav['type'] ?? 'link';
  297. if (!isset($nav['name'])) {
  298. continue;
  299. }
  300. // Allow settings navigation items with no route entry, all other types require one
  301. if (!isset($nav['route']) && $nav['type'] !== 'settings') {
  302. continue;
  303. }
  304. $role = $nav['@attributes']['role'] ?? 'all';
  305. if ($role === 'admin' && !$this->isAdmin()) {
  306. continue;
  307. }
  308. $l = $this->l10nFac->get($app);
  309. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  310. $order = $customOrders[$app][$key] ?? $nav['order'] ?? 100;
  311. $type = $nav['type'];
  312. $route = !empty($nav['route']) ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  313. $icon = $nav['icon'] ?? 'app.svg';
  314. foreach ([$icon, "$app.svg"] as $i) {
  315. try {
  316. $icon = $this->urlGenerator->imagePath($app, $i);
  317. break;
  318. } catch (\RuntimeException $ex) {
  319. // no icon? - ignore it then
  320. }
  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. // This is the default app that will always be shown first
  340. 'default' => $defaultApp === $id,
  341. // App that registered this navigation entry (not necessarly the same as the id)
  342. 'app' => $app,
  343. // The key used to identify this entry in the navigations entries
  344. 'key' => $key,
  345. ] : []
  346. ));
  347. }
  348. }
  349. }
  350. private function isAdmin() {
  351. $user = $this->userSession->getUser();
  352. if ($user !== null) {
  353. return $this->groupManager->isAdmin($user->getUID());
  354. }
  355. return false;
  356. }
  357. private function isSubadmin() {
  358. $user = $this->userSession->getUser();
  359. if ($user !== null) {
  360. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  361. }
  362. return false;
  363. }
  364. public function setUnreadCounter(string $id, int $unreadCounter): void {
  365. $this->unreadCounters[$id] = $unreadCounter;
  366. }
  367. }