Completed
Push — master ( e7a609...6d1b90 )
by Thomas
31:06 queued 17:33
created

NavigationManager::isSubAdmin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Bart Visscher <[email protected]>
4
 * @author Joas Schilling <[email protected]>
5
 * @author Morris Jobke <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Thomas Müller <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2018, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC;
27
28
use OCP\App\IAppManager;
29
use OCP\IGroupManager;
30
use OCP\INavigationManager;
31
use OCP\IURLGenerator;
32
use OCP\IUserSession;
33
use OCP\L10N\IFactory;
34
35
/**
36
 * Manages the ownCloud navigation
37
 */
38
class NavigationManager implements INavigationManager {
39
	protected $entries = [];
40
	protected $closureEntries = [];
41
	protected $activeEntry;
42
	/** @var bool */
43
	protected $init = false;
44
	/** @var IAppManager */
45
	protected $appManager;
46
	/** @var IURLGenerator */
47
	private $urlGenerator;
48
	/** @var IFactory */
49
	private $l10nFac;
50
	/** @var IUserSession */
51
	private $userSession;
52
	/** @var IGroupManager */
53
	private $groupManager;
54
55
	function __construct(IAppManager $appManager = null,
56
						 IURLGenerator $urlGenerator = null,
57
						 IFactory $l10nFac = null,
58
						 IUserSession $userSession = null,
59
						 IGroupManager$groupManager = null) {
60
		$this->appManager = $appManager;
61
		$this->urlGenerator = $urlGenerator;
62
		$this->l10nFac = $l10nFac;
63
		$this->userSession = $userSession;
64
		$this->groupManager = $groupManager;
65
	}
66
67
	/**
68
	 * Creates a new navigation entry
69
	 *
70
	 * @param array|\Closure $entry Array containing: id, name, order, icon and href key
71
	 *					The use of a closure is preferred, because it will avoid
72
	 * 					loading the routing of your app, unless required.
73
	 * @return void
74
	 */
75
	public function add($entry) {
76
		if ($entry instanceof \Closure) {
77
			$this->closureEntries[] = $entry;
78
			return;
79
		}
80
81
		$entry['active'] = false;
82
		if(!isset($entry['icon'])) {
83
			$entry['icon'] = '';
84
		}
85
		$this->entries[] = $entry;
86
	}
87
88
	/**
89
	 * returns all the added Menu entries
90
	 * @return array an array of the added entries
91
	 */
92
	public function getAll() {
93
		$this->init();
94
		foreach ($this->closureEntries as $c) {
95
			$this->add($c());
96
		}
97
		$this->closureEntries = [];
98
		return $this->entries;
99
	}
100
101
	/**
102
	 * removes all the entries
103
	 */
104
	public function clear() {
105
		$this->entries = [];
106
		$this->closureEntries = [];
107
		$this->init = false;
108
	}
109
110
	/**
111
	 * Sets the current navigation entry of the currently running app
112
	 * @param string $id of the app entry to activate (from added $entry)
113
	 */
114
	public function setActiveEntry($id) {
115
		$this->activeEntry = $id;
116
	}
117
118
	/**
119
	 * gets the active Menu entry
120
	 * @return string id or empty string
121
	 *
122
	 * This function returns the id of the active navigation entry (set by
123
	 * setActiveEntry
124
	 */
125
	public function getActiveEntry() {
126
		return $this->activeEntry;
127
	}
128
129
	private function init() {
130
		if ($this->init) {
131
			return;
132
		}
133
		$this->init = true;
134
		if ($this->appManager === null) {
135
			return;
136
		}
137
		foreach ($this->appManager->getInstalledApps() as $app) {
138
			// load plugins and collections from info.xml
139
			$info = $this->appManager->getAppInfo($app);
140
			if (!isset($info['navigation'])) {
141
				continue;
142
			}
143
			$nav = $info['navigation'];
144
			if (!isset($nav['route'])) {
145
				continue;
146
			}
147
			$role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
148
			if ($role === 'admin' && !$this->isAdmin()) {
149
				continue;
150
			}
151
			if ($role === 'sub-admin' && !$this->isSubAdmin()) {
152
				continue;
153
			}
154
			$l = $this->l10nFac->get($app);
155
			$order = isset($nav['order']) ? $nav['order'] : 100;
156
			$route = $this->urlGenerator->linkToRoute($nav['route']);
157
			$name = isset($nav['name']) ? $nav['name'] : ucfirst($app);
158
			$icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
159
			$iconPath = null;
160
			foreach ([$icon, "$app.svg"] as $i) {
161
				try {
162
					$iconPath = $this->urlGenerator->imagePath($app, $i);
163
					break;
164
				} catch (\RuntimeException $ex) {
165
					// no icon? - ignore it then
166
				}
167
			}
168
169
			if ($iconPath === null) {
170
				$iconPath = $this->urlGenerator->imagePath('core', 'default-app-icon.svg');
171
			}
172
173
			$this->add([
174
				'id' => $app,
175
				'order' => $order,
176
				'href' => $route,
177
				'icon' => $iconPath,
178
				'name' => $l->t($name),
179
			]);
180
		}
181
	}
182
183 View Code Duplication
	private function isAdmin() {
184
		$user = $this->userSession->getUser();
185
		if ($user !== null) {
186
			return $this->groupManager->isAdmin($user->getUID());
187
		}
188
		return false;
189
	}
190
191
	private function isSubAdmin() {
192
		$user = $this->userSession->getUser();
193
		if ($user !== null) {
194
			return $this->groupManager->getSubAdmin()->isSubAdmin($user);
195
		}
196
		return false;
197
	}
198
199
}
200