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
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-07-21 18:07:57 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @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,
|
|
|
|
* 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;
|
|
|
|
if(!isset($entry['icon'])) {
|
|
|
|
$entry['icon'] = '';
|
|
|
|
}
|
2017-09-20 19:09:27 +03:00
|
|
|
if(!isset($entry['classes'])) {
|
|
|
|
$entry['classes'] = '';
|
|
|
|
}
|
2017-03-26 20:40:41 +03:00
|
|
|
if(!isset($entry['type'])) {
|
|
|
|
$entry['type'] = 'link';
|
|
|
|
}
|
2013-09-20 19:34:33 +04:00
|
|
|
$this->entries[] = $entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* returns all the added Menu entries
|
2017-03-26 20:40:41 +03:00
|
|
|
* @param string $type
|
2014-05-11 21:13:51 +04:00
|
|
|
* @return array an array of the added entries
|
2013-09-20 19:34:33 +04:00
|
|
|
*/
|
2017-03-26 20:40:41 +03:00
|
|
|
public function getAll($type = 'link') {
|
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());
|
|
|
|
}
|
|
|
|
$this->closureEntries = array();
|
2017-03-26 20:40:41 +03:00
|
|
|
|
|
|
|
if ($type === 'all') {
|
|
|
|
return $this->entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_filter($this->entries, function($entry) use ($type) {
|
|
|
|
return $entry['type'] === $type;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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,
|
2017-03-26 20:40:41 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings_help'),
|
|
|
|
'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);
|
2017-08-18 13:16:43 +03:00
|
|
|
if($logoutUrl !== '') {
|
|
|
|
// 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,
|
2017-03-26 20:40:41 +03:00
|
|
|
'href' => $this->urlGenerator->linkToRoute('settings_users'),
|
|
|
|
'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);
|
2017-03-26 22:15:25 +03:00
|
|
|
if (empty($info['navigations'])) {
|
2016-12-08 19:43:46 +03:00
|
|
|
continue;
|
|
|
|
}
|
2017-03-26 22:15:25 +03:00
|
|
|
foreach ($info['navigations'] as $nav) {
|
|
|
|
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);
|
2017-03-27 00:07:54 +03:00
|
|
|
$id = isset($nav['id']) ? $nav['id'] : $app;
|
2017-03-26 22:15:25 +03:00
|
|
|
$order = isset($nav['order']) ? $nav['order'] : 100;
|
|
|
|
$type = isset($nav['type']) ? $nav['type'] : 'link';
|
|
|
|
$route = $this->urlGenerator->linkToRoute($nav['route']);
|
|
|
|
$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
|
|
|
}
|