1
0

NavigationManager.php 7.8 KB

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