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.

323 lines
8.2 KiB

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