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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * Creates a new navigation entry
  77. *
  78. * @param array|\Closure $entry Array containing: id, name, order, icon and href key
  79. * The use of a closure is preferred, because it will avoid
  80. * loading the routing of your app, unless required.
  81. * @return void
  82. */
  83. public function add($entry) {
  84. if ($entry instanceof \Closure) {
  85. $this->closureEntries[] = $entry;
  86. return;
  87. }
  88. $entry['active'] = false;
  89. if (!isset($entry['icon'])) {
  90. $entry['icon'] = '';
  91. }
  92. if (!isset($entry['classes'])) {
  93. $entry['classes'] = '';
  94. }
  95. if (!isset($entry['type'])) {
  96. $entry['type'] = 'link';
  97. }
  98. $this->entries[$entry['id']] = $entry;
  99. }
  100. /**
  101. * Get a list of navigation entries
  102. *
  103. * @param string $type type of the navigation entries
  104. * @return array
  105. */
  106. public function getAll(string $type = 'link'): array {
  107. $this->init();
  108. foreach ($this->closureEntries as $c) {
  109. $this->add($c());
  110. }
  111. $this->closureEntries = [];
  112. $result = $this->entries;
  113. if ($type !== 'all') {
  114. $result = array_filter($this->entries, function ($entry) use ($type) {
  115. return $entry['type'] === $type;
  116. });
  117. }
  118. return $this->proceedNavigation($result);
  119. }
  120. /**
  121. * Sort navigation entries by order, name and set active flag
  122. *
  123. * @param array $list
  124. * @return array
  125. */
  126. private function proceedNavigation(array $list): array {
  127. uasort($list, function ($a, $b) {
  128. if (isset($a['order']) && isset($b['order'])) {
  129. return ($a['order'] < $b['order']) ? -1 : 1;
  130. } elseif (isset($a['order']) || isset($b['order'])) {
  131. return isset($a['order']) ? -1 : 1;
  132. } else {
  133. return ($a['name'] < $b['name']) ? -1 : 1;
  134. }
  135. });
  136. $activeApp = $this->getActiveEntry();
  137. if ($activeApp !== null) {
  138. foreach ($list as $index => &$navEntry) {
  139. if ($navEntry['id'] == $activeApp) {
  140. $navEntry['active'] = true;
  141. } else {
  142. $navEntry['active'] = false;
  143. }
  144. }
  145. unset($navEntry);
  146. }
  147. return $list;
  148. }
  149. /**
  150. * removes all the entries
  151. */
  152. public function clear($loadDefaultLinks = true) {
  153. $this->entries = [];
  154. $this->closureEntries = [];
  155. $this->init = !$loadDefaultLinks;
  156. }
  157. /**
  158. * Sets the current navigation entry of the currently running app
  159. * @param string $id of the app entry to activate (from added $entry)
  160. */
  161. public function setActiveEntry($id) {
  162. $this->activeEntry = $id;
  163. }
  164. /**
  165. * gets the active Menu entry
  166. * @return string id or empty string
  167. *
  168. * This function returns the id of the active navigation entry (set by
  169. * setActiveEntry
  170. */
  171. public function getActiveEntry() {
  172. return $this->activeEntry;
  173. }
  174. private function init() {
  175. if ($this->init) {
  176. return;
  177. }
  178. $this->init = true;
  179. $l = $this->l10nFac->get('lib');
  180. if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
  181. $this->add([
  182. 'type' => 'settings',
  183. 'id' => 'help',
  184. 'order' => 6,
  185. 'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
  186. 'name' => $l->t('Help'),
  187. 'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
  188. ]);
  189. }
  190. if ($this->userSession->isLoggedIn()) {
  191. if ($this->isAdmin()) {
  192. // App management
  193. $this->add([
  194. 'type' => 'settings',
  195. 'id' => 'core_apps',
  196. 'order' => 4,
  197. 'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
  198. 'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
  199. 'name' => $l->t('Apps'),
  200. ]);
  201. }
  202. // Personal and (if applicable) admin settings
  203. $this->add([
  204. 'type' => 'settings',
  205. 'id' => 'settings',
  206. 'order' => 2,
  207. 'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
  208. 'name' => $l->t('Settings'),
  209. 'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
  210. ]);
  211. $logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
  212. if ($logoutUrl !== '') {
  213. // Logout
  214. $this->add([
  215. 'type' => 'settings',
  216. 'id' => 'logout',
  217. 'order' => 99999,
  218. 'href' => $logoutUrl,
  219. 'name' => $l->t('Log out'),
  220. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  221. ]);
  222. }
  223. if ($this->isSubadmin()) {
  224. // User management
  225. $this->add([
  226. 'type' => 'settings',
  227. 'id' => 'core_users',
  228. 'order' => 5,
  229. 'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
  230. 'name' => $l->t('Users'),
  231. 'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
  232. ]);
  233. }
  234. }
  235. if ($this->appManager === 'null') {
  236. return;
  237. }
  238. if ($this->userSession->isLoggedIn()) {
  239. $apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
  240. } else {
  241. $apps = $this->appManager->getInstalledApps();
  242. }
  243. foreach ($apps as $app) {
  244. if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
  245. continue;
  246. }
  247. // load plugins and collections from info.xml
  248. $info = $this->appManager->getAppInfo($app);
  249. if (!isset($info['navigations']['navigation'])) {
  250. continue;
  251. }
  252. foreach ($info['navigations']['navigation'] as $key => $nav) {
  253. if (!isset($nav['name'])) {
  254. continue;
  255. }
  256. if (!isset($nav['route'])) {
  257. continue;
  258. }
  259. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  260. if ($role === 'admin' && !$this->isAdmin()) {
  261. continue;
  262. }
  263. $l = $this->l10nFac->get($app);
  264. $id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
  265. $order = isset($nav['order']) ? $nav['order'] : 100;
  266. $type = isset($nav['type']) ? $nav['type'] : 'link';
  267. $route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : '';
  268. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  269. foreach ([$icon, "$app.svg"] as $i) {
  270. try {
  271. $icon = $this->urlGenerator->imagePath($app, $i);
  272. break;
  273. } catch (\RuntimeException $ex) {
  274. // no icon? - ignore it then
  275. }
  276. }
  277. if ($icon === null) {
  278. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  279. }
  280. $this->add([
  281. 'id' => $id,
  282. 'order' => $order,
  283. 'href' => $route,
  284. 'icon' => $icon,
  285. 'type' => $type,
  286. 'name' => $l->t($nav['name']),
  287. ]);
  288. }
  289. }
  290. }
  291. private function isAdmin() {
  292. $user = $this->userSession->getUser();
  293. if ($user !== null) {
  294. return $this->groupManager->isAdmin($user->getUID());
  295. }
  296. return false;
  297. }
  298. private function isSubadmin() {
  299. $user = $this->userSession->getUser();
  300. if ($user !== null) {
  301. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  302. }
  303. return false;
  304. }
  305. }