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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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' => 6,
  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. if ($this->isAdmin()) {
  183. // App management
  184. $this->add([
  185. 'type' => 'settings',
  186. 'id' => 'core_apps',
  187. 'order' => 4,
  188. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  189. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  190. 'name' => $l->t('Apps'),
  191. ]);
  192. }
  193. // Personal and (if applicable) admin settings
  194. $this->add([
  195. 'type' => 'settings',
  196. 'id' => 'settings',
  197. 'order' => 2,
  198. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  199. 'name' => $l->t('Settings'),
  200. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  201. ]);
  202. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  203. if ($logoutUrl !== '') {
  204. // Logout
  205. $this->add([
  206. 'type' => 'settings',
  207. 'id' => 'logout',
  208. 'order' => 99999,
  209. 'href' => $logoutUrl,
  210. 'name' => $l->t('Log out'),
  211. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  212. ]);
  213. }
  214. if ($this->isSubadmin()) {
  215. // User management
  216. $this->add([
  217. 'type' => 'settings',
  218. 'id' => 'core_users',
  219. 'order' => 5,
  220. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  221. 'name' => $l->t('Users'),
  222. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  223. ]);
  224. }
  225. }
  226. if ($this->appManager === 'null') {
  227. return;
  228. }
  229. if ($this->userSession->isLoggedIn()) {
  230. $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
  231. } else {
  232. $apps = $this->appManager->getInstalledApps();
  233. }
  234. foreach ($apps as $app) {
  235. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  236. continue;
  237. }
  238. // load plugins and collections from info.xml
  239. $info = $this->appManager->getAppInfo($app);
  240. if (!isset($info['navigations']['navigation'])) {
  241. continue;
  242. }
  243. foreach ($info['navigations']['navigation'] as $key => $nav) {
  244. if (!isset($nav['name'])) {
  245. continue;
  246. }
  247. if (!isset($nav['route'])) {
  248. continue;
  249. }
  250. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  251. if ($role === 'admin' && !$this->isAdmin()) {
  252. continue;
  253. }
  254. $l = $this->l10nFac->get($app);
  255. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  256. $order = isset($nav['order']) ? $nav['order'] : 100;
  257. $type = isset($nav['type']) ? $nav['type'] : 'link';
  258. $route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  259. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  260. foreach ([$icon, "$app.svg"] as $i) {
  261. try {
  262. $icon = $this->urlGenerator->imagePath($app, $i);
  263. break;
  264. } catch (\RuntimeException $ex) {
  265. // no icon? - ignore it then
  266. }
  267. }
  268. if ($icon === null) {
  269. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  270. }
  271. $this->add([
  272. 'id' => $id,
  273. 'order' => $order,
  274. 'href' => $route,
  275. 'icon' => $icon,
  276. 'type' => $type,
  277. 'name' => $l->t($nav['name']),
  278. ]);
  279. }
  280. }
  281. }
  282. private function isAdmin() {
  283. $user = $this->userSession->getUser();
  284. if ($user !== null) {
  285. return $this->groupManager->isAdmin($user->getUID());
  286. }
  287. return false;
  288. }
  289. private function isSubadmin() {
  290. $user = $this->userSession->getUser();
  291. if ($user !== null) {
  292. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  293. }
  294. return false;
  295. }
  296. public function setUnreadCounter(string $id, int $unreadCounter): void {
  297. $this->unreadCounters[$id] = $unreadCounter;
  298. }
  299. }