1
0

NavigationManager.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. // Logout
  173. $this->add([
  174. 'type' => 'settings',
  175. 'id' => 'logout',
  176. 'order' => 99999,
  177. 'href' => $this->urlGenerator->linkToRouteAbsolute(
  178. 'core.login.logout',
  179. ['requesttoken' => \OCP\Util::callRegister()]
  180. ),
  181. 'name' => $l->t('Log out'),
  182. 'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
  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. foreach ($this->appManager->getInstalledApps() as $app) {
  211. // load plugins and collections from info.xml
  212. $info = $this->appManager->getAppInfo($app);
  213. if (empty($info['navigations'])) {
  214. continue;
  215. }
  216. foreach ($info['navigations'] as $nav) {
  217. if (!isset($nav['name'])) {
  218. continue;
  219. }
  220. if (!isset($nav['route'])) {
  221. continue;
  222. }
  223. $role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
  224. if ($role === 'admin' && !$this->isAdmin()) {
  225. continue;
  226. }
  227. $l = $this->l10nFac->get($app);
  228. $id = isset($nav['id']) ? $nav['id'] : $app;
  229. $order = isset($nav['order']) ? $nav['order'] : 100;
  230. $type = isset($nav['type']) ? $nav['type'] : 'link';
  231. $route = $this->urlGenerator->linkToRoute($nav['route']);
  232. $icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
  233. foreach ([$icon, "$app.svg"] as $i) {
  234. try {
  235. $icon = $this->urlGenerator->imagePath($app, $i);
  236. break;
  237. } catch (\RuntimeException $ex) {
  238. // no icon? - ignore it then
  239. }
  240. }
  241. if ($icon === null) {
  242. $icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
  243. }
  244. $this->add([
  245. 'id' => $id,
  246. 'order' => $order,
  247. 'href' => $route,
  248. 'icon' => $icon,
  249. 'type' => $type,
  250. 'name' => $l->t($nav['name']),
  251. ]);
  252. }
  253. }
  254. }
  255. private function isAdmin() {
  256. $user = $this->userSession->getUser();
  257. if ($user !== null) {
  258. return $this->groupManager->isAdmin($user->getUID());
  259. }
  260. return false;
  261. }
  262. private function isSubadmin() {
  263. $user = $this->userSession->getUser();
  264. if ($user !== null) {
  265. return $this->groupManager->getSubAdmin()->isSubAdmin($user);
  266. }
  267. return false;
  268. }
  269. }