2013-09-20 19:34:33 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-12-08 19:43:46 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud GmbH
|
2016-07-21 18:07:57 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Daniel Kesselberg <mail@danielkesselberg.de>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2013-09-20 19:34:33 +04:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2013-09-20 19:34:33 +04:00
|
|
|
namespace OC;
|
|
|
|
|
2017-01-27 11:38:10 +03:00
|
|
|
use OC\App\AppManager;
|
2017-03-26 20:40:41 +03:00
|
|
|
use OC\Group\Manager;
|
2016-12-08 19:43:46 +03:00
|
|
|
use OCP\App\IAppManager;
|
2017-03-26 20:40:41 +03:00
|
|
|
use OCP\IConfig;
|
2016-12-08 19:43:46 +03:00
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\INavigationManager;
|
|
|
|
use OCP\IURLGenerator;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\L10N\IFactory;
|
|
|
|
|
2013-09-20 19:34:33 +04:00
|
|
|
/**
|
2013-09-20 23:45:27 +04:00
|
|
|
* Manages the ownCloud navigation
|
2013-09-20 19:34:33 +04:00
|
|
|
*/
|
2016-12-08 19:43:46 +03:00
|
|
|
|
|
|
|
class NavigationManager implements INavigationManager {
|
|
|
|
protected $entries = [];
|
|
|
|
protected $closureEntries = [];
|
2013-09-20 23:45:27 +04:00
|
|
|
protected $activeEntry;
|
2016-12-08 19:43:46 +03:00
|
|
|
/** @var bool */
|
|
|
|
protected $init = false;
|
2017-01-27 11:38:10 +03:00
|
|
|
/** @var IAppManager|AppManager */
|
2016-12-08 19:43:46 +03:00
|
|
|
protected $appManager;
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
/** @var IFactory */
|
|
|
|
private $l10nFac;
|
|
|
|
/** @var IUserSession */
|
|
|
|
private $userSession;
|
2017-03-26 20:40:41 +03:00
|
|
|
/** @var IGroupManager|Manager */
|
2016-12-08 19:43:46 +03:00
|
|
|
private $groupManager;
|
2017-03-26 20:40:41 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2016-12-08 19:43:46 +03:00
|
|
|
|
2017-03-26 20:40:41 +03:00
|
|
|
public function __construct(IAppManager $appManager,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IFactory $l10nFac,
|
|
|
|
IUserSession $userSession,
|
|
|
|
IGroupManager $groupManager,
|
|
|
|
IConfig $config) {
|
2016-12-08 19:43:46 +03:00
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->l10nFac = $l10nFac;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->groupManager = $groupManager;
|
2017-03-26 20:40:41 +03:00
|
|
|
$this->config = $config;
|
2016-12-08 19:43:46 +03:00
|
|
|
}
|
2013-09-20 19:34:33 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new navigation entry
|
2015-03-16 18:17:43 +03:00
|
|
|
*
|
|
|
|
* @param array|\Closure $entry Array containing: id, name, order, icon and href key
|
|
|
|
* The use of a closure is preferred, because it will avoid
|
|
|
|
* loading the routing of your app, unless required.
|
|
|
|
* @return void
|
2013-09-20 19:34:33 +04:00
|
|
|
*/
|
2015-03-16 18:17:43 +03:00
|
|
|
public function add($entry) {
|
|
|
|
if ($entry instanceof \Closure) {
|
|
|
|
$this->closureEntries[] = $entry;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-20 19:34:33 +04:00
|
|
|
$entry['active'] = false;
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($entry['icon'])) {
|
2013-09-20 19:34:33 +04:00
|
|
|
$entry['icon'] = '';
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($entry['classes'])) {
|
2017-09-20 19:09:27 +03:00
|
|
|
$entry['classes'] = '';
|
|
|
|
}
|
2020-04-10 15:19:56 +03:00
|
|
|
if (!isset($entry['type'])) {
|
2017-03-26 20:40:41 +03:00
|
|
|
$entry['type'] = 'link';
|
|
|
|
}
|
2018-07-13 10:33:57 +03:00
|
|
|
$this->entries[$entry['id']] = $entry;
|
2013-09-20 19:34:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-15 12:38:25 +03:00
|
|
|
* Get a list of navigation entries
|
|
|
|
*
|
|
|
|
* @param string $type type of the navigation entries
|
|
|
|
* @return array
|
2013-09-20 19:34:33 +04:00
|
|
|
*/
|
2018-01-29 14:17:47 +03:00
|
|
|
public function getAll(string $type = 'link'): array {
|
2016-12-08 19:43:46 +03:00
|
|
|
$this->init();
|
2015-03-16 18:17:43 +03:00
|
|
|
foreach ($this->closureEntries as $c) {
|
|
|
|
$this->add($c());
|
|
|
|
}
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->closureEntries = [];
|
2017-03-26 20:40:41 +03:00
|
|
|
|
2018-01-29 12:07:12 +03:00
|
|
|
$result = $this->entries;
|
|
|
|
if ($type !== 'all') {
|
2020-04-09 14:53:40 +03:00
|
|
|
$result = array_filter($this->entries, function ($entry) use ($type) {
|
2018-01-29 12:07:12 +03:00
|
|
|
return $entry['type'] === $type;
|
|
|
|
});
|
2017-03-26 20:40:41 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 12:07:12 +03:00
|
|
|
return $this->proceedNavigation($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort navigation entries by order, name and set active flag
|
|
|
|
*
|
2018-02-15 12:38:25 +03:00
|
|
|
* @param array $list
|
|
|
|
* @return array
|
2018-01-29 12:07:12 +03:00
|
|
|
*/
|
2018-02-15 12:38:25 +03:00
|
|
|
private function proceedNavigation(array $list): array {
|
2020-04-09 14:53:40 +03:00
|
|
|
uasort($list, function ($a, $b) {
|
2018-01-29 12:07:12 +03:00
|
|
|
if (isset($a['order']) && isset($b['order'])) {
|
|
|
|
return ($a['order'] < $b['order']) ? -1 : 1;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (isset($a['order']) || isset($b['order'])) {
|
2018-01-29 12:07:12 +03:00
|
|
|
return isset($a['order']) ? -1 : 1;
|
|
|
|
} else {
|
|
|
|
return ($a['name'] < $b['name']) ? -1 : 1;
|
|
|
|
}
|
2017-03-26 20:40:41 +03:00
|
|
|
});
|
2018-01-29 12:07:12 +03:00
|
|
|
|
|
|
|
$activeApp = $this->getActiveEntry();
|
|
|
|
if ($activeApp !== null) {
|
|
|
|
foreach ($list as $index => &$navEntry) {
|
|
|
|
if ($navEntry['id'] == $activeApp) {
|
|
|
|
$navEntry['active'] = true;
|
|
|
|
} else {
|
|
|
|
$navEntry['active'] = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($navEntry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
2017-03-26 20:40:41 +03:00
|
|
|
}
|
|
|
|
|
2018-01-29 12:07:12 +03:00
|
|
|
|
2013-09-20 19:34:33 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* removes all the entries
|
2013-09-20 19:34:33 +04:00
|
|
|
*/
|
2017-03-26 22:15:25 +03:00
|
|
|
public function clear($loadDefaultLinks = true) {
|
2016-12-08 19:43:46 +03:00
|
|
|
$this->entries = [];
|
|
|
|
$this->closureEntries = [];
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->init = !$loadDefaultLinks;
|
2013-09-20 19:34:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the current navigation entry of the currently running app
|
|
|
|
* @param string $id of the app entry to activate (from added $entry)
|
|
|
|
*/
|
|
|
|
public function setActiveEntry($id) {
|
|
|
|
$this->activeEntry = $id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* gets the active Menu entry
|
2013-09-20 19:34:33 +04:00
|
|
|
* @return string id or empty string
|
|
|
|
*
|
|
|
|
* This function returns the id of the active navigation entry (set by
|
|
|
|
* setActiveEntry
|
|
|
|
*/
|
|
|
|
public function getActiveEntry() {
|
|
|
|
return $this->activeEntry;
|
|
|
|
}
|
2016-12-08 19:43:46 +03:00
|
|
|
|
|
|
|
private function init() {
|
|
|
|
if ($this->init) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->init = true;
|
2017-03-26 20:40:41 +03:00
|
|
|
|
2017-03-26 21:26:05 +03:00
|
|
|
$l = $this->l10nFac->get('lib');
|
2017-03-26 20:40:41 +03:00
|
|
|
if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
|
|
|
|
$this->add([
|
|
|
|
'type' => 'settings',
|
|
|
|
'id' => 'help',
|
2017-03-26 23:32:12 +03:00
|
|
|
'order' => 5,
|
2019-10-22 18:56:55 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
|
2017-03-26 20:40:41 +03:00
|
|
|
'name' => $l->t('Help'),
|
|
|
|
'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->userSession->isLoggedIn()) {
|
|
|
|
if ($this->isAdmin()) {
|
|
|
|
// App management
|
|
|
|
$this->add([
|
2017-03-26 21:26:05 +03:00
|
|
|
'type' => 'settings',
|
2017-03-26 20:40:41 +03:00
|
|
|
'id' => 'core_apps',
|
2017-03-26 23:30:21 +03:00
|
|
|
'order' => 3,
|
2017-03-26 20:40:41 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
|
|
|
|
'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
|
|
|
|
'name' => $l->t('Apps'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2017-05-16 02:40:10 +03:00
|
|
|
// Personal and (if applicable) admin settings
|
2017-03-26 20:40:41 +03:00
|
|
|
$this->add([
|
|
|
|
'type' => 'settings',
|
2017-05-16 02:40:10 +03:00
|
|
|
'id' => 'settings',
|
2017-03-26 20:40:41 +03:00
|
|
|
'order' => 1,
|
2017-05-17 13:06:51 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
|
2017-05-16 02:40:10 +03:00
|
|
|
'name' => $l->t('Settings'),
|
|
|
|
'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
|
2017-03-26 20:40:41 +03:00
|
|
|
]);
|
|
|
|
|
2017-08-18 16:32:40 +03:00
|
|
|
$logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($logoutUrl !== '') {
|
2017-08-18 13:16:43 +03:00
|
|
|
// Logout
|
|
|
|
$this->add([
|
|
|
|
'type' => 'settings',
|
|
|
|
'id' => 'logout',
|
|
|
|
'order' => 99999,
|
|
|
|
'href' => $logoutUrl,
|
|
|
|
'name' => $l->t('Log out'),
|
|
|
|
'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
|
|
|
|
]);
|
|
|
|
}
|
2017-03-26 20:40:41 +03:00
|
|
|
|
|
|
|
if ($this->isSubadmin()) {
|
|
|
|
// User management
|
|
|
|
$this->add([
|
|
|
|
'type' => 'settings',
|
|
|
|
'id' => 'core_users',
|
2017-03-26 23:30:21 +03:00
|
|
|
'order' => 4,
|
2018-04-20 18:29:42 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
|
2017-03-26 20:40:41 +03:00
|
|
|
'name' => $l->t('Users'),
|
|
|
|
'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->appManager === 'null') {
|
2016-12-08 19:43:46 +03:00
|
|
|
return;
|
|
|
|
}
|
2017-07-05 14:52:51 +03:00
|
|
|
|
|
|
|
if ($this->userSession->isLoggedIn()) {
|
|
|
|
$apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
|
|
|
|
} else {
|
|
|
|
$apps = $this->appManager->getInstalledApps();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($apps as $app) {
|
|
|
|
if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-12-08 19:43:46 +03:00
|
|
|
// load plugins and collections from info.xml
|
|
|
|
$info = $this->appManager->getAppInfo($app);
|
2019-02-22 17:45:55 +03:00
|
|
|
if (!isset($info['navigations']['navigation'])) {
|
2016-12-08 19:43:46 +03:00
|
|
|
continue;
|
|
|
|
}
|
2019-02-25 16:49:51 +03:00
|
|
|
foreach ($info['navigations']['navigation'] as $key => $nav) {
|
2017-03-26 22:15:25 +03:00
|
|
|
if (!isset($nav['name'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!isset($nav['route'])) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
|
|
|
|
if ($role === 'admin' && !$this->isAdmin()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$l = $this->l10nFac->get($app);
|
2019-02-25 16:49:51 +03:00
|
|
|
$id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
|
2017-03-26 22:15:25 +03:00
|
|
|
$order = isset($nav['order']) ? $nav['order'] : 100;
|
|
|
|
$type = isset($nav['type']) ? $nav['type'] : 'link';
|
2018-05-24 17:27:56 +03:00
|
|
|
$route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : '';
|
2017-03-26 22:15:25 +03:00
|
|
|
$icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
|
|
|
|
foreach ([$icon, "$app.svg"] as $i) {
|
|
|
|
try {
|
|
|
|
$icon = $this->urlGenerator->imagePath($app, $i);
|
|
|
|
break;
|
|
|
|
} catch (\RuntimeException $ex) {
|
|
|
|
// no icon? - ignore it then
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($icon === null) {
|
|
|
|
$icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
|
2016-12-08 19:43:46 +03:00
|
|
|
}
|
|
|
|
|
2017-03-26 22:15:25 +03:00
|
|
|
$this->add([
|
2017-03-27 00:07:54 +03:00
|
|
|
'id' => $id,
|
2017-03-26 22:15:25 +03:00
|
|
|
'order' => $order,
|
|
|
|
'href' => $route,
|
|
|
|
'icon' => $icon,
|
|
|
|
'type' => $type,
|
|
|
|
'name' => $l->t($nav['name']),
|
|
|
|
]);
|
|
|
|
}
|
2016-12-08 19:43:46 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function isAdmin() {
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user !== null) {
|
|
|
|
return $this->groupManager->isAdmin($user->getUID());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-26 20:40:41 +03:00
|
|
|
private function isSubadmin() {
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user !== null) {
|
|
|
|
return $this->groupManager->getSubAdmin()->isSubAdmin($user);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-20 19:34:33 +04:00
|
|
|
}
|