NavigationManager.php 12 KB

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