Completed
Pull Request — master (#362)
by Maxence
02:45 queued 01:07
created

ConfigService::getTrustedDomains()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
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
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Service;
28
29
use OCA\Circles\Exceptions\GSStatusException;
30
use OCA\Circles\Model\Circle;
31
use OCP\IConfig;
32
use OCP\IRequest;
33
use OCP\PreConditionNotMetException;
34
use OCP\Util;
35
36
class ConfigService {
37
38
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
39
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
40
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
41
	const CIRCLES_GS_ENABLED = 'gs_enabled';
42
	const CIRCLES_MEMBERS_LIMIT = 'members_limit';
43
	const CIRCLES_ACCOUNTS_ONLY = 'accounts_only';
44
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
45
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
46
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
47
	const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity';
48
	const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles';
49
50
	const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock';
51
	const CIRCLES_TEST_ASYNC_INIT = 'test_async_init';
52
	const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand';
53
	const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count';
54
55
	const GS_ENABLED = 'enabled';
56
	const GS_MODE = 'mode';
57
	const GS_KEY = 'key';
58
	const GS_LOOKUP = 'lookup';
59
60
61
	private $defaults = [
62
		self::CIRCLES_ALLOW_CIRCLES           => Circle::CIRCLES_ALL,
63
		self::CIRCLES_TEST_ASYNC_INIT         => '0',
64
		self::CIRCLES_SWAP_TO_TEAMS           => '0',
65
		self::CIRCLES_ACCOUNTS_ONLY           => '0',
66
		self::CIRCLES_MEMBERS_LIMIT           => '50',
67
		self::CIRCLES_ALLOW_LINKED_GROUPS     => '0',
68
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0',
69
		self::CIRCLES_GS_ENABLED              => '0',
70
		self::CIRCLES_ALLOW_NON_SSL_LINKS     => '0',
71
		self::CIRCLES_NON_SSL_LOCAL           => '0',
72
		self::CIRCLES_ACTIVITY_ON_CREATION    => '1',
73
		self::CIRCLES_SKIP_INVITATION_STEP    => '0'
74
	];
75
76
	/** @var string */
77
	private $appName;
78
79
	/** @var IConfig */
80
	private $config;
81
82
	/** @var string */
83
	private $userId;
84
85
	/** @var IRequest */
86
	private $request;
87
88
	/** @var MiscService */
89
	private $miscService;
90
91
	/** @var int */
92
	private $allowedCircle = -1;
93
94
	/** @var int */
95
	private $allowedLinkedGroups = -1;
96
97
	/** @var int */
98
	private $allowedFederatedCircles = -1;
99
100
	/** @var int */
101
	private $allowedNonSSLLinks = -1;
102
103
	/** @var int */
104
	private $localNonSSL = -1;
105
106
	/**
107
	 * ConfigService constructor.
108
	 *
109
	 * @param string $appName
110
	 * @param IConfig $config
111
	 * @param IRequest $request
112
	 * @param string $userId
113
	 * @param MiscService $miscService
114
	 */
115
	public function __construct(
116
		$appName, IConfig $config, IRequest $request, $userId, MiscService $miscService
117
	) {
118
		$this->appName = $appName;
119
		$this->config = $config;
120
		$this->request = $request;
121
		$this->userId = $userId;
122
		$this->miscService = $miscService;
123
	}
124
125
126
	public function getLocalAddress() {
127
		return (($this->isLocalNonSSL()) ? 'http://' : '')
128
			   . $this->request->getServerHost();
129
	}
130
131
132
	/**
133
	 * returns if this type of circle is allowed by the current configuration.
134
	 *
135
	 * @param $type
136
	 *
137
	 * @return int
138
	 */
139
	public function isCircleAllowed($type) {
140
		if ($this->allowedCircle === -1) {
141
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
142
		}
143
144
		return ((int)$type & (int)$this->allowedCircle);
145
	}
146
147
148
	/**
149
	 * @return bool
150
	 */
151
	public function isLinkedGroupsAllowed() {
152
		if ($this->allowedLinkedGroups === -1) {
153
			$this->allowedLinkedGroups =
154
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
155
		}
156
157
		return ($this->allowedLinkedGroups === 1);
158
	}
159
160
161
	/**
162
	 * @return bool
163
	 */
164
	public function isFederatedCirclesAllowed() {
165
		if ($this->allowedFederatedCircles === -1) {
166
			$this->allowedFederatedCircles =
167
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
168
		}
169
170
		return ($this->allowedFederatedCircles === 1);
171
	}
172
173
	/**
174
	 * @return bool
175
	 */
176
	public function isInvitationSkipped() {
177
		return (int)$this->getAppValue(self::CIRCLES_SKIP_INVITATION_STEP) === 1;
178
	}
179
180
	/**
181
	 * @return bool
182
	 */
183
	public function isLocalNonSSL() {
184
		if ($this->localNonSSL === -1) {
185
			$this->localNonSSL =
186
				(int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL);
187
		}
188
189
		return ($this->localNonSSL === 1);
190
	}
191
192
193
	/**
194
	 * @return bool
195
	 */
196
	public function isNonSSLLinksAllowed() {
197
		if ($this->allowedNonSSLLinks === -1) {
198
			$this->allowedNonSSLLinks =
199
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
200
		}
201
202
		return ($this->allowedNonSSLLinks === 1);
203
	}
204
205
206
	/**
207
	 * @param string $remote
208
	 *
209
	 * @return string
210
	 */
211
	public function generateRemoteHost($remote) {
212
		if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0)
213
			&& strpos($remote, 'https://') !== 0
214
		) {
215
			$remote = 'https://' . $remote;
216
		}
217
218
		return rtrim($remote, '/');
219
	}
220
221
222
	/**
223
	 * Get a value by key
224
	 *
225
	 * @param string $key
226
	 *
227
	 * @return string
228
	 */
229
	public function getCoreValue($key) {
230
		$defaultValue = null;
231
232
		return $this->config->getAppValue('core', $key, $defaultValue);
233
	}
234
235
236
	/**
237
	 * Get a value by key
238
	 *
239
	 * @param string $key
240
	 *
241
	 * @return string
242
	 */
243
	public function getAppValue($key) {
244
		$defaultValue = null;
245
246
		if (array_key_exists($key, $this->defaults)) {
247
			$defaultValue = $this->defaults[$key];
248
		}
249
250
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
251
	}
252
253
	/**
254
	 * Set a value by key
255
	 *
256
	 * @param string $key
257
	 * @param string $value
258
	 *
259
	 * @return void
260
	 */
261
	public function setAppValue($key, $value) {
262
		$this->config->setAppValue($this->appName, $key, $value);
263
	}
264
265
	/**
266
	 * remove a key
267
	 *
268
	 * @param string $key
269
	 *
270
	 * @return string
271
	 */
272
	public function deleteAppValue($key) {
273
		return $this->config->deleteAppValue($this->appName, $key);
274
	}
275
276
	/**
277
	 * Get a user value by key
278
	 *
279
	 * @param string $key
280
	 *
281
	 * @return string
282
	 */
283
	public function getUserValue($key) {
284
		return $this->config->getUserValue($this->userId, $this->appName, $key);
285
	}
286
287
	/**
288
	 * Set a user value by key
289
	 *
290
	 * @param string $key
291
	 * @param string $value
292
	 *
293
	 * @return string
294
	 * @throws PreConditionNotMetException
295
	 */
296
	public function setUserValue($key, $value) {
297
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
298
	}
299
300
301
	/**
302
	 * Get a user value by key and user
303
	 *
304
	 * @param string $userId
305
	 * @param string $key
306
	 *
307
	 * @param string $default
308
	 *
309
	 * @return string
310
	 */
311
	public function getCoreValueForUser($userId, $key, $default = '') {
312
		return $this->config->getUserValue($userId, 'core', $key, $default);
313
	}
314
315
316
	/**
317
	 * Get a user value by key and user
318
	 *
319
	 * @param string $userId
320
	 * @param string $key
321
	 *
322
	 * @return string
323
	 */
324
	public function getValueForUser($userId, $key) {
325
		return $this->config->getUserValue($userId, $this->appName, $key);
326
	}
327
328
	/**
329
	 * Set a user value by key
330
	 *
331
	 * @param string $userId
332
	 * @param string $key
333
	 * @param string $value
334
	 *
335
	 * @return string
336
	 * @throws PreConditionNotMetException
337
	 */
338
	public function setValueForUser($userId, $key, $value) {
339
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
340
	}
341
342
	/**
343
	 * return the cloud version.
344
	 * if $complete is true, return a string x.y.z
345
	 *
346
	 * @param boolean $complete
347
	 *
348
	 * @return string|integer
349
	 */
350
	public function getCloudVersion($complete = false) {
351
		$ver = Util::getVersion();
352
353
		if ($complete) {
354
			return implode('.', $ver);
355
		}
356
357
		return $ver[0];
358
	}
359
360
361
	/**
362
	 * @return bool
363
	 */
364
	public function isAccountOnly() {
365
		return ($this->getAppValue(self::CIRCLES_ACCOUNTS_ONLY) === '1');
366
	}
367
368
369
	/**
370
	 * should the password for a mail share be send to the recipient
371
	 *
372
	 * @return bool
373
	 */
374
	public function sendPasswordByMail() {
375
		return ($this->config->getAppValue('sharebymail', 'sendpasswordmail', 'yes') === 'yes');
376
	}
377
378
	/**
379
	 * do we require a share by mail to be password protected
380
	 *
381
	 * @return bool
382
	 */
383
	public function enforcePasswordProtection() {
384
		return ($this->config->getAppValue('sharebymail', 'enforcePasswordProtection', 'no') === 'yes');
385
	}
386
387
388
	/**
389
	 * @param string $type
390
	 *
391
	 * @throws GSStatusException
392
	 */
393
	public function getGSStatus(string $type = '') {
394
		$enabled = $this->config->getSystemValueBool('gs.enabled', false);
395
		$lookup = $this->config->getSystemValue('lookup_server', '');
396
397
		if ($lookup === '' || !$enabled) {
398
			if ($type === self::GS_ENABLED) {
399
				return false;
400
			}
401
402
			throw new GSStatusException('GS and lookup are not configured : ' . $lookup . ', ' . $enabled);
403
		}
404
405
		$clef = $this->config->getSystemValue('gss.jwt.key', '');
406
		$mode = $this->config->getSystemValue('gss.mode', '');
407
408
		switch ($type) {
409
			case self::GS_ENABLED:
410
				return $enabled;
411
412
			case self::GS_MODE:
413
				return $mode;
414
415
			case self::GS_KEY:
416
				return $clef;
417
418
			case self::GS_LOOKUP:
419
				return $lookup;
420
		}
421
422
		return [
423
			self::GS_ENABLED => $enabled,
424
			self::GS_LOOKUP  => $lookup,
425
			self::GS_MODE    => $clef,
426
			self::GS_KEY     => $mode,
427
		];
428
	}
429
430
431
	/**
432
	 * @return array
433
	 */
434
	public function getTrustedDomains(): array {
435
		$domains = [];
436
		foreach ($this->config->getSystemValue('trusted_domains', []) as $v) {
437
			$domains[] = $v;
438
		}
439
440
		return $domains;
441
	}
442
443
444
	/**
445
	 * @return string
446
	 */
447
	public function getLocalCloudId(): string {
448
		return $this->getTrustedDomains()[0];
449
	}
450
451
}
452
453