NavigationManager.php 8.6 KB

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