Passed
Push — master ( 25fc25...eec792 )
by Blizzz
14:01 queued 12s
created

NavigationManager::setUnreadCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud GmbH
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Bart Visscher <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Daniel Kesselberg <[email protected]>
9
 * @author Georg Ehrke <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author John Molakvoæ <[email protected]>
12
 * @author Julius Härtl <[email protected]>
13
 * @author Lukas Reschke <[email protected]>
14
 * @author Morris Jobke <[email protected]>
15
 * @author Thomas Müller <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
namespace OC;
33
34
use OC\App\AppManager;
35
use OC\Group\Manager;
36
use OCP\App\IAppManager;
37
use OCP\IConfig;
38
use OCP\IGroupManager;
39
use OCP\INavigationManager;
40
use OCP\IURLGenerator;
41
use OCP\IUserSession;
42
use OCP\L10N\IFactory;
43
44
/**
45
 * Manages the ownCloud navigation
46
 */
47
48
class NavigationManager implements INavigationManager {
49
	protected $entries = [];
50
	protected $closureEntries = [];
51
	protected $activeEntry;
52
	protected $unreadCounters = [];
53
54
	/** @var bool */
55
	protected $init = false;
56
	/** @var IAppManager|AppManager */
57
	protected $appManager;
58
	/** @var IURLGenerator */
59
	private $urlGenerator;
60
	/** @var IFactory */
61
	private $l10nFac;
62
	/** @var IUserSession */
63
	private $userSession;
64
	/** @var IGroupManager|Manager */
65
	private $groupManager;
66
	/** @var IConfig */
67
	private $config;
68
69
	public function __construct(IAppManager $appManager,
70
						 IURLGenerator $urlGenerator,
71
						 IFactory $l10nFac,
72
						 IUserSession $userSession,
73
						 IGroupManager $groupManager,
74
						 IConfig $config) {
75
		$this->appManager = $appManager;
76
		$this->urlGenerator = $urlGenerator;
77
		$this->l10nFac = $l10nFac;
78
		$this->userSession = $userSession;
79
		$this->groupManager = $groupManager;
80
		$this->config = $config;
81
	}
82
83
	/**
84
	 * @inheritDoc
85
	 */
86
	public function add($entry) {
87
		if ($entry instanceof \Closure) {
88
			$this->closureEntries[] = $entry;
89
			return;
90
		}
91
92
		$entry['active'] = false;
93
		if (!isset($entry['icon'])) {
94
			$entry['icon'] = '';
95
		}
96
		if (!isset($entry['classes'])) {
97
			$entry['classes'] = '';
98
		}
99
		if (!isset($entry['type'])) {
100
			$entry['type'] = 'link';
101
		}
102
103
		$id = $entry['id'];
104
		$entry['unread'] = isset($this->unreadCounters[$id]) ? $this->unreadCounters[$id] : 0;
105
106
		$this->entries[$id] = $entry;
107
	}
108
109
	/**
110
	 * @inheritDoc
111
	 */
112
	public function getAll(string $type = 'link'): array {
113
		$this->init();
114
		foreach ($this->closureEntries as $c) {
115
			$this->add($c());
116
		}
117
		$this->closureEntries = [];
118
119
		$result = $this->entries;
120
		if ($type !== 'all') {
121
			$result = array_filter($this->entries, function ($entry) use ($type) {
122
				return $entry['type'] === $type;
123
			});
124
		}
125
126
		return $this->proceedNavigation($result);
127
	}
128
129
	/**
130
	 * Sort navigation entries by order, name and set active flag
131
	 *
132
	 * @param array $list
133
	 * @return array
134
	 */
135
	private function proceedNavigation(array $list): array {
136
		uasort($list, function ($a, $b) {
137
			if (isset($a['order']) && isset($b['order'])) {
138
				return ($a['order'] < $b['order']) ? -1 : 1;
139
			} elseif (isset($a['order']) || isset($b['order'])) {
140
				return isset($a['order']) ? -1 : 1;
141
			} else {
142
				return ($a['name'] < $b['name']) ? -1 : 1;
143
			}
144
		});
145
146
		$activeApp = $this->getActiveEntry();
147
		if ($activeApp !== null) {
0 ignored issues
show
introduced by
The condition $activeApp !== null is always true.
Loading history...
148
			foreach ($list as $index => &$navEntry) {
149
				if ($navEntry['id'] == $activeApp) {
150
					$navEntry['active'] = true;
151
				} else {
152
					$navEntry['active'] = false;
153
				}
154
			}
155
			unset($navEntry);
156
		}
157
158
		return $list;
159
	}
160
161
162
	/**
163
	 * removes all the entries
164
	 */
165
	public function clear($loadDefaultLinks = true) {
166
		$this->entries = [];
167
		$this->closureEntries = [];
168
		$this->init = !$loadDefaultLinks;
169
	}
170
171
	/**
172
	 * @inheritDoc
173
	 */
174
	public function setActiveEntry($id) {
175
		$this->activeEntry = $id;
176
	}
177
178
	/**
179
	 * @inheritDoc
180
	 */
181
	public function getActiveEntry() {
182
		return $this->activeEntry;
183
	}
184
185
	private function init() {
186
		if ($this->init) {
187
			return;
188
		}
189
		$this->init = true;
190
191
		$l = $this->l10nFac->get('lib');
192
		if ($this->config->getSystemValue('knowledgebaseenabled', true)) {
193
			$this->add([
194
				'type' => 'settings',
195
				'id' => 'help',
196
				'order' => 6,
197
				'href' => $this->urlGenerator->linkToRoute('settings.Help.help'),
198
				'name' => $l->t('Help'),
199
				'icon' => $this->urlGenerator->imagePath('settings', 'help.svg'),
200
			]);
201
		}
202
203
		if ($this->userSession->isLoggedIn()) {
204
			if ($this->isAdmin()) {
205
				// App management
206
				$this->add([
207
					'type' => 'settings',
208
					'id' => 'core_apps',
209
					'order' => 4,
210
					'href' => $this->urlGenerator->linkToRoute('settings.AppSettings.viewApps'),
211
					'icon' => $this->urlGenerator->imagePath('settings', 'apps.svg'),
212
					'name' => $l->t('Apps'),
213
				]);
214
			}
215
216
			// Personal and (if applicable) admin settings
217
			$this->add([
218
				'type' => 'settings',
219
				'id' => 'settings',
220
				'order' => 2,
221
				'href' => $this->urlGenerator->linkToRoute('settings.PersonalSettings.index'),
222
				'name' => $l->t('Settings'),
223
				'icon' => $this->urlGenerator->imagePath('settings', 'admin.svg'),
224
			]);
225
226
			$logoutUrl = \OC_User::getLogoutUrl($this->urlGenerator);
227
			if ($logoutUrl !== '') {
228
				// Logout
229
				$this->add([
230
					'type' => 'settings',
231
					'id' => 'logout',
232
					'order' => 99999,
233
					'href' => $logoutUrl,
234
					'name' => $l->t('Log out'),
235
					'icon' => $this->urlGenerator->imagePath('core', 'actions/logout.svg'),
236
				]);
237
			}
238
239
			if ($this->isSubadmin()) {
240
				// User management
241
				$this->add([
242
					'type' => 'settings',
243
					'id' => 'core_users',
244
					'order' => 5,
245
					'href' => $this->urlGenerator->linkToRoute('settings.Users.usersList'),
246
					'name' => $l->t('Users'),
247
					'icon' => $this->urlGenerator->imagePath('settings', 'users.svg'),
248
				]);
249
			}
250
		}
251
252
		if ($this->appManager === 'null') {
0 ignored issues
show
introduced by
The condition $this->appManager === 'null' is always false.
Loading history...
253
			return;
254
		}
255
256
		if ($this->userSession->isLoggedIn()) {
257
			$apps = $this->appManager->getEnabledAppsForUser($this->userSession->getUser());
258
		} else {
259
			$apps = $this->appManager->getInstalledApps();
260
		}
261
262
		foreach ($apps as $app) {
263
			if (!$this->userSession->isLoggedIn() && !$this->appManager->isEnabledForUser($app, $this->userSession->getUser())) {
264
				continue;
265
			}
266
267
			// load plugins and collections from info.xml
268
			$info = $this->appManager->getAppInfo($app);
269
			if (!isset($info['navigations']['navigation'])) {
270
				continue;
271
			}
272
			foreach ($info['navigations']['navigation'] as $key => $nav) {
273
				if (!isset($nav['name'])) {
274
					continue;
275
				}
276
				if (!isset($nav['route'])) {
277
					continue;
278
				}
279
				$role = isset($nav['@attributes']['role']) ? $nav['@attributes']['role'] : 'all';
280
				if ($role === 'admin' && !$this->isAdmin()) {
281
					continue;
282
				}
283
				$l = $this->l10nFac->get($app);
284
				$id = $nav['id'] ?? $app . ($key === 0 ? '' : $key);
285
				$order = isset($nav['order']) ? $nav['order'] : 100;
286
				$type = isset($nav['type']) ? $nav['type'] : 'link';
287
				$route = $nav['route'] !== '' ? $this->urlGenerator->linkToRoute($nav['route']) : '';
288
				$icon = isset($nav['icon']) ? $nav['icon'] : 'app.svg';
289
				foreach ([$icon, "$app.svg"] as $i) {
290
					try {
291
						$icon = $this->urlGenerator->imagePath($app, $i);
292
						break;
293
					} catch (\RuntimeException $ex) {
294
						// no icon? - ignore it then
295
					}
296
				}
297
				if ($icon === null) {
298
					$icon = $this->urlGenerator->imagePath('core', 'default-app-icon');
299
				}
300
301
				$this->add([
302
					'id' => $id,
303
					'order' => $order,
304
					'href' => $route,
305
					'icon' => $icon,
306
					'type' => $type,
307
					'name' => $l->t($nav['name']),
308
				]);
309
			}
310
		}
311
	}
312
313
	private function isAdmin() {
314
		$user = $this->userSession->getUser();
315
		if ($user !== null) {
316
			return $this->groupManager->isAdmin($user->getUID());
317
		}
318
		return false;
319
	}
320
321
	private function isSubadmin() {
322
		$user = $this->userSession->getUser();
323
		if ($user !== null) {
324
			return $this->groupManager->getSubAdmin()->isSubAdmin($user);
0 ignored issues
show
Bug introduced by
The method getSubAdmin() does not exist on OCP\IGroupManager. Since it exists in all sub-types, consider adding an abstract or default implementation to OCP\IGroupManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

324
			return $this->groupManager->/** @scrutinizer ignore-call */ getSubAdmin()->isSubAdmin($user);
Loading history...
325
		}
326
		return false;
327
	}
328
329
	public function setUnreadCounter(string $id, int $unreadCounter): void {
330
		$this->unreadCounters[$id] = $unreadCounter;
331
	}
332
}
333