1
0

NavigationManager.php 12 KB

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