You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NavigationManager.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 IGroupManager|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'] = isset($this->unreadCounters[$id]) ? $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);
  115. }
  116. /**
  117. * Sort navigation entries by order, name and set active flag
  118. *
  119. * @param array $list
  120. * @return array
  121. */
  122. private function proceedNavigation(array $list): array {
  123. uasort($list, function ($a, $b) {
  124. if (isset($a['order']) && isset($b['order'])) {
  125. return ($a['order'] < $b['order']) ? -1 : 1;
  126. } elseif (isset($a['order']) || isset($b['order'])) {
  127. return isset($a['order']) ? -1 : 1;
  128. } else {
  129. return ($a['name'] < $b['name']) ? -1 : 1;
  130. }
  131. });
  132. $activeApp = $this->getActiveEntry();
  133. if ($activeApp !== null) {
  134. foreach ($list as $index => &$navEntry) {
  135. if ($navEntry['id'] == $activeApp) {
  136. $navEntry['active'] = true;
  137. } else {
  138. $navEntry['active'] = false;
  139. }
  140. }
  141. unset($navEntry);
  142. }
  143. return $list;
  144. }
  145. /**
  146. * removes all the entries
  147. */
  148. public function clear($loadDefaultLinks = true) {
  149. $this->entries = [];
  150. $this->closureEntries = [];
  151. $this->init = !$loadDefaultLinks;
  152. }
  153. /**
  154. * @inheritDoc
  155. */
  156. public function setActiveEntry($id) {
  157. $this->activeEntry = $id;
  158. }
  159. /**
  160. * @inheritDoc
  161. */
  162. public function getActiveEntry() {
  163. return $this->activeEntry;
  164. }
  165. private function init() {
  166. if ($this->init) {
  167. return;
  168. }
  169. $this->init = true;
  170. $l = $this->l10nFac->get('lib');
  171. if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
  172. $this->add([
  173. 'type' => 'settings',
  174. 'id' => 'help',
  175. 'order' => 99998,
  176. 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
  177. 'name' => $l->t('Help'),
  178. 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
  179. ]);
  180. }
  181. if ($this->userSession->isLoggedIn()) {
  182. // Accessibility settings
  183. if ($this->appManager->isEnabledForUser('theming', $this->userSession->getUser())) {
  184. $this->add([
  185. 'type' => 'settings',
  186. 'id' => 'accessibility_settings',
  187. 'order' => 2,
  188. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index', ['section' => 'theming']),
  189. 'name' => $l->t('Appearance and accessibility'),
  190. 'icon' => $this->urlGenerator->imagePath('theming', 'accessibility-dark.svg'),
  191. ]);
  192. }
  193. if ($this->isAdmin()) {
  194. // App management
  195. $this->add([
  196. 'type' => 'settings',
  197. 'id' => 'core_apps',
  198. 'order' => 5,
  199. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  200. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  201. 'name' => $l->t('Apps'),
  202. ]);
  203. // Personal settings
  204. $this->add([
  205. 'type' => 'settings',
  206. 'id' => 'settings',
  207. 'order' => 3,
  208. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  209. 'name' => $l->t('Personal settings'),
  210. 'icon' => $this->urlGenerator->imagePath('settings', 'personal.svg'),
  211. ]);
  212. // Admin settings
  213. $this->add([
  214. 'type' => 'settings',
  215. 'id' => 'admin_settings',
  216. 'order' => 4,
  217. 'href' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'overview']),
  218. 'name' => $l->t('Administration settings'),
  219. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  220. ]);
  221. } else {
  222. // Personal settings
  223. $this->add([
  224. 'type' => 'settings',
  225. 'id' => 'settings',
  226. 'order' => 3,
  227. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  228. 'name' => $l->t('Settings'),
  229. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  230. ]);
  231. }
  232. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  233. if ($logoutUrl !== '') {
  234. // Logout
  235. $this->add([
  236. 'type' => 'settings',
  237. 'id' => 'logout',
  238. 'order' => 99999,
  239. 'href' => $logoutUrl,
  240. 'name' => $l->t('Log out'),
  241. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  242. ]);
  243. }
  244. if ($this->isSubadmin()) {
  245. // User management
  246. $this->add([
  247. 'type' => 'settings',
  248. 'id' => 'core_users',
  249. 'order' => 6,
  250. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  251. 'name' => $l->t('Users'),
  252. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  253. ]);
  254. }
  255. }
  256. if ($this->appManager === 'null') {
  257. return;
  258. }
  259. if ($this->userSession->isLoggedIn()) {
  260. $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
  261. } else {
  262. $apps = $this->appManager->getInstalledApps();
  263. }
  264. foreach ($apps as $app) {
  265. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  266. continue;
  267. }
  268. // load plugins and collections from info.xml
  269. $info = $this->appManager->getAppInfo($app);
  270. if (!isset($info['navigations']['navigation'])) {
  271. continue;
  272. }
  273. foreach ($info['navigations']['navigation'] as $key => $nav) {
  274. $nav['type'] = $nav['type'] ?? 'link';
  275. if (!isset($nav['name'])) {
  276. continue;
  277. }
  278. // Allow settings navigation items with no route entry, all other types require one
  279. if (!isset($nav['route']) && $nav['type'] !== 'settings') {
  280. continue;
  281. }
  282. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  283. if ($role === 'admin' && !$this->isAdmin()) {
  284. continue;
  285. }
  286. $l = $this->l10nFac->get($app);
  287. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  288. $order = isset($nav['order']) ? $nav['order'] : 100;
  289. $type = $nav['type'];
  290. $route = !empty($nav['route']) ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  291. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  292. foreach ([$icon, "$app.svg"] as $i) {
  293. try {
  294. $icon = $this->urlGenerator->imagePath($app, $i);
  295. break;
  296. } catch (\RuntimeException $ex) {
  297. // no icon? - ignore it then
  298. }
  299. }
  300. if ($icon === null) {
  301. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  302. }
  303. $this->add([
  304. 'id' => $id,
  305. 'order' => $order,
  306. 'href' => $route,
  307. 'icon' => $icon,
  308. 'type' => $type,
  309. 'name' => $l->t($nav['name']),
  310. ]);
  311. }
  312. }
  313. }
  314. private function isAdmin() {
  315. $user = $this->userSession->getUser();
  316. if ($user !== null) {
  317. return $this->groupManager->isAdmin($user->getUID());
  318. }
  319. return false;
  320. }
  321. private function isSubadmin() {
  322. $user = $this->userSession->getUser();
  323. if ($user !== null) {
  324. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  325. }
  326. return false;
  327. }
  328. public function setUnreadCounter(string $id, int $unreadCounter): void {
  329. $this->unreadCounters[$id] = $unreadCounter;
  330. }
  331. }