NavigationManager.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC;
  32. use OC\App\AppManager;
  33. use OC\Group\Manager;
  34. use OCP\App\IAppManager;
  35. use OCP\IConfig;
  36. use OCP\IGroupManager;
  37. use OCP\INavigationManager;
  38. use OCP\IURLGenerator;
  39. use OCP\IUserSession;
  40. use OCP\L10N\IFactory;
  41. /**
  42. * Manages the ownCloud navigation
  43. */
  44. class NavigationManager implements INavigationManager {
  45. protected $entries = [];
  46. protected $closureEntries = [];
  47. protected $activeEntry;
  48. /** @var bool */
  49. protected $init = false;
  50. /** @var IAppManager|AppManager */
  51. protected $appManager;
  52. /** @var IURLGenerator */
  53. private $urlGenerator;
  54. /** @var IFactory */
  55. private $l10nFac;
  56. /** @var IUserSession */
  57. private $userSession;
  58. /** @var IGroupManager|Manager */
  59. private $groupManager;
  60. /** @var IConfig */
  61. private $config;
  62. public function __construct(IAppManager $appManager,
  63. IURLGenerator $urlGenerator,
  64. IFactory $l10nFac,
  65. IUserSession $userSession,
  66. IGroupManager $groupManager,
  67. IConfig $config) {
  68. $this->appManager = $appManager;
  69. $this->urlGenerator = $urlGenerator;
  70. $this->l10nFac = $l10nFac;
  71. $this->userSession = $userSession;
  72. $this->groupManager = $groupManager;
  73. $this->config = $config;
  74. }
  75. /**
  76. * @inheritDoc
  77. */
  78. public function add($entry) {
  79. if ($entry instanceof \Closure) {
  80. $this->closureEntries[] = $entry;
  81. return;
  82. }
  83. $entry['active'] = false;
  84. if (!isset($entry['icon'])) {
  85. $entry['icon'] = '';
  86. }
  87. if (!isset($entry['classes'])) {
  88. $entry['classes'] = '';
  89. }
  90. if (!isset($entry['type'])) {
  91. $entry['type'] = 'link';
  92. }
  93. $this->entries[$entry['id']] = $entry;
  94. }
  95. /**
  96. * @inheritDoc
  97. */
  98. public function getAll(string $type = 'link'): array {
  99. $this->init();
  100. foreach ($this->closureEntries as $c) {
  101. $this->add($c());
  102. }
  103. $this->closureEntries = [];
  104. $result = $this->entries;
  105. if ($type !== 'all') {
  106. $result = array_filter($this->entries, function ($entry) use ($type) {
  107. return $entry['type'] === $type;
  108. });
  109. }
  110. return $this->proceedNavigation($result);
  111. }
  112. /**
  113. * Sort navigation entries by order, name and set active flag
  114. *
  115. * @param array $list
  116. * @return array
  117. */
  118. private function proceedNavigation(array $list): array {
  119. uasort($list, function ($a, $b) {
  120. if (isset($a['order']) && isset($b['order'])) {
  121. return ($a['order'] < $b['order']) ? -1 : 1;
  122. } elseif (isset($a['order']) || isset($b['order'])) {
  123. return isset($a['order']) ? -1 : 1;
  124. } else {
  125. return ($a['name'] < $b['name']) ? -1 : 1;
  126. }
  127. });
  128. $activeApp = $this->getActiveEntry();
  129. if ($activeApp !== null) {
  130. foreach ($list as $index => &$navEntry) {
  131. if ($navEntry['id'] == $activeApp) {
  132. $navEntry['active'] = true;
  133. } else {
  134. $navEntry['active'] = false;
  135. }
  136. }
  137. unset($navEntry);
  138. }
  139. return $list;
  140. }
  141. /**
  142. * removes all the entries
  143. */
  144. public function clear($loadDefaultLinks = true) {
  145. $this->entries = [];
  146. $this->closureEntries = [];
  147. $this->init = !$loadDefaultLinks;
  148. }
  149. /**
  150. * @inheritDoc
  151. */
  152. public function setActiveEntry($id) {
  153. $this->activeEntry = $id;
  154. }
  155. /**
  156. * @inheritDoc
  157. */
  158. public function getActiveEntry() {
  159. return $this->activeEntry;
  160. }
  161. private function init() {
  162. if ($this->init) {
  163. return;
  164. }
  165. $this->init = true;
  166. $l = $this->l10nFac->get('lib');
  167. if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
  168. $this->add([
  169. 'type' => 'settings',
  170. 'id' => 'help',
  171. 'order' => 6,
  172. 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
  173. 'name' => $l->t('Help'),
  174. 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
  175. ]);
  176. }
  177. if ($this->userSession->isLoggedIn()) {
  178. if ($this->isAdmin()) {
  179. // App management
  180. $this->add([
  181. 'type' => 'settings',
  182. 'id' => 'core_apps',
  183. 'order' => 4,
  184. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  185. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  186. 'name' => $l->t('Apps'),
  187. ]);
  188. }
  189. // Personal and (if applicable) admin settings
  190. $this->add([
  191. 'type' => 'settings',
  192. 'id' => 'settings',
  193. 'order' => 2,
  194. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  195. 'name' => $l->t('Settings'),
  196. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  197. ]);
  198. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  199. if ($logoutUrl !== '') {
  200. // Logout
  201. $this->add([
  202. 'type' => 'settings',
  203. 'id' => 'logout',
  204. 'order' => 99999,
  205. 'href' => $logoutUrl,
  206. 'name' => $l->t('Log out'),
  207. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  208. ]);
  209. }
  210. if ($this->isSubadmin()) {
  211. // User management
  212. $this->add([
  213. 'type' => 'settings',
  214. 'id' => 'core_users',
  215. 'order' => 5,
  216. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  217. 'name' => $l->t('Users'),
  218. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  219. ]);
  220. }
  221. }
  222. if ($this->appManager === 'null') {
  223. return;
  224. }
  225. if ($this->userSession->isLoggedIn()) {
  226. $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
  227. } else {
  228. $apps = $this->appManager->getInstalledApps();
  229. }
  230. foreach ($apps as $app) {
  231. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  232. continue;
  233. }
  234. // load plugins and collections from info.xml
  235. $info = $this->appManager->getAppInfo($app);
  236. if (!isset($info['navigations']['navigation'])) {
  237. continue;
  238. }
  239. foreach ($info['navigations']['navigation'] as $key => $nav) {
  240. if (!isset($nav['name'])) {
  241. continue;
  242. }
  243. if (!isset($nav['route'])) {
  244. continue;
  245. }
  246. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  247. if ($role === 'admin' && !$this->isAdmin()) {
  248. continue;
  249. }
  250. $l = $this->l10nFac->get($app);
  251. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  252. $order = isset($nav['order']) ? $nav['order'] : 100;
  253. $type = isset($nav['type']) ? $nav['type'] : 'link';
  254. $route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  255. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  256. foreach ([$icon, "$app.svg"] as $i) {
  257. try {
  258. $icon = $this->urlGenerator->imagePath($app, $i);
  259. break;
  260. } catch (\RuntimeException $ex) {
  261. // no icon? - ignore it then
  262. }
  263. }
  264. if ($icon === null) {
  265. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  266. }
  267. $this->add([
  268. 'id' => $id,
  269. 'order' => $order,
  270. 'href' => $route,
  271. 'icon' => $icon,
  272. 'type' => $type,
  273. 'name' => $l->t($nav['name']),
  274. ]);
  275. }
  276. }
  277. }
  278. private function isAdmin() {
  279. $user = $this->userSession->getUser();
  280. if ($user !== null) {
  281. return $this->groupManager->isAdmin($user->getUID());
  282. }
  283. return false;
  284. }
  285. private function isSubadmin() {
  286. $user = $this->userSession->getUser();
  287. if ($user !== null) {
  288. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  289. }
  290. return false;
  291. }
  292. }