Completed
Push — master ( 5cc268...51fb7b )
by Maxence
01:54 queued 11s
created

ConfigService::getCoreValueForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 3
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\Model\Circle;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\PreConditionNotMetException;
33
use OCP\Util;
34
35
class ConfigService {
36
37
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
38
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
39
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
40
	const CIRCLES_MEMBERS_LIMIT = 'members_limit';
41
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
42
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
43
	const CIRCLES_NON_SSL_LOCAL = 'local_is_non_ssl';
44
	const CIRCLES_ACTIVITY_ON_CREATION = 'creation_activity';
45
	const CIRCLES_SKIP_INVITATION_STEP = 'skip_invitation_to_closed_circles';
46
47
	const CIRCLES_TEST_ASYNC_LOCK = 'test_async_lock';
48
	const CIRCLES_TEST_ASYNC_INIT = 'test_async_init';
49
	const CIRCLES_TEST_ASYNC_HAND = 'test_async_hand';
50
	const CIRCLES_TEST_ASYNC_COUNT = 'test_async_count';
51
52
	private $defaults = [
53
		self::CIRCLES_ALLOW_CIRCLES           => Circle::CIRCLES_ALL,
54
		self::CIRCLES_TEST_ASYNC_INIT         => '0',
55
		self::CIRCLES_SWAP_TO_TEAMS           => '0',
56
		self::CIRCLES_MEMBERS_LIMIT           => '50',
57
		self::CIRCLES_ALLOW_LINKED_GROUPS     => '0',
58
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0',
59
		self::CIRCLES_ALLOW_NON_SSL_LINKS     => '0',
60
		self::CIRCLES_NON_SSL_LOCAL           => '0',
61
		self::CIRCLES_ACTIVITY_ON_CREATION    => '1',
62
		self::CIRCLES_SKIP_INVITATION_STEP    => '0'
63
	];
64
65
	/** @var string */
66
	private $appName;
67
68
	/** @var IConfig */
69
	private $config;
70
71
	/** @var string */
72
	private $userId;
73
74
	/** @var IRequest */
75
	private $request;
76
77
	/** @var MiscService */
78
	private $miscService;
79
80
	/** @var int */
81
	private $allowedCircle = -1;
82
83
	/** @var int */
84
	private $allowedLinkedGroups = -1;
85
86
	/** @var int */
87
	private $allowedFederatedCircles = -1;
88
89
	/** @var int */
90
	private $allowedNonSSLLinks = -1;
91
92
	/** @var int */
93
	private $localNonSSL = -1;
94
95
	/**
96
	 * ConfigService constructor.
97
	 *
98
	 * @param string $appName
99
	 * @param IConfig $config
100
	 * @param IRequest $request
101
	 * @param string $userId
102
	 * @param MiscService $miscService
103
	 */
104
	public function __construct(
105
		$appName, IConfig $config, IRequest $request, $userId, MiscService $miscService
106
	) {
107
		$this->appName = $appName;
108
		$this->config = $config;
109
		$this->request = $request;
110
		$this->userId = $userId;
111
		$this->miscService = $miscService;
112
	}
113
114
115
	public function getLocalAddress() {
116
		return (($this->isLocalNonSSL()) ? 'http://' : '')
117
			   . $this->request->getServerHost();
118
	}
119
120
121
	/**
122
	 * returns if this type of circle is allowed by the current configuration.
123
	 *
124
	 * @param $type
125
	 *
126
	 * @return int
127
	 */
128
	public function isCircleAllowed($type) {
129
		if ($this->allowedCircle === -1) {
130
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
131
		}
132
133
		return ((int)$type & (int)$this->allowedCircle);
134
	}
135
136
137
	/**
138
	 * @return bool
139
	 */
140
	public function isLinkedGroupsAllowed() {
141
		if ($this->allowedLinkedGroups === -1) {
142
			$this->allowedLinkedGroups =
143
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
144
		}
145
146
		return ($this->allowedLinkedGroups === 1);
147
	}
148
149
150
	/**
151
	 * @return bool
152
	 */
153
	public function isFederatedCirclesAllowed() {
154
		if ($this->allowedFederatedCircles === -1) {
155
			$this->allowedFederatedCircles =
156
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
157
		}
158
159
		return ($this->allowedFederatedCircles === 1);
160
	}
161
162
	/**
163
	 * @return bool
164
	 */
165
	public function isInvitationSkipped() {
166
		return (int)$this->getAppValue(self::CIRCLES_SKIP_INVITATION_STEP) === 1;
167
	}
168
169
	/**
170
	 * @return bool
171
	 */
172
	public function isLocalNonSSL() {
173
		if ($this->localNonSSL === -1) {
174
			$this->localNonSSL =
175
				(int)$this->getAppValue(self::CIRCLES_NON_SSL_LOCAL);
176
		}
177
178
		return ($this->localNonSSL === 1);
179
	}
180
181
182
	/**
183
	 * @return bool
184
	 */
185
	public function isNonSSLLinksAllowed() {
186
		if ($this->allowedNonSSLLinks === -1) {
187
			$this->allowedNonSSLLinks =
188
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
189
		}
190
191
		return ($this->allowedNonSSLLinks === 1);
192
	}
193
194
195
	/**
196
	 * @param string $remote
197
	 *
198
	 * @return string
199
	 */
200
	public function generateRemoteHost($remote) {
201
		if ((!$this->isNonSSLLinksAllowed() || strpos($remote, 'http://') !== 0)
202
			&& strpos($remote, 'https://') !== 0
203
		) {
204
			$remote = 'https://' . $remote;
205
		}
206
207
		return rtrim($remote, '/');
208
	}
209
210
211
	/**
212
	 * Get a value by key
213
	 *
214
	 * @param string $key
215
	 *
216
	 * @return string
217
	 */
218
	public function getCoreValue($key) {
219
		$defaultValue = null;
220
221
		return $this->config->getAppValue('core', $key, $defaultValue);
222
	}
223
224
225
	/**
226
	 * Get a value by key
227
	 *
228
	 * @param string $key
229
	 *
230
	 * @return string
231
	 */
232
	public function getAppValue($key) {
233
		$defaultValue = null;
234
235
		if (array_key_exists($key, $this->defaults)) {
236
			$defaultValue = $this->defaults[$key];
237
		}
238
239
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
240
	}
241
242
	/**
243
	 * Set a value by key
244
	 *
245
	 * @param string $key
246
	 * @param string $value
247
	 *
248
	 * @return void
249
	 */
250
	public function setAppValue($key, $value) {
251
		$this->config->setAppValue($this->appName, $key, $value);
252
	}
253
254
	/**
255
	 * remove a key
256
	 *
257
	 * @param string $key
258
	 *
259
	 * @return string
260
	 */
261
	public function deleteAppValue($key) {
262
		return $this->config->deleteAppValue($this->appName, $key);
263
	}
264
265
	/**
266
	 * Get a user value by key
267
	 *
268
	 * @param string $key
269
	 *
270
	 * @return string
271
	 */
272
	public function getUserValue($key) {
273
		return $this->config->getUserValue($this->userId, $this->appName, $key);
274
	}
275
276
	/**
277
	 * Set a user value by key
278
	 *
279
	 * @param string $key
280
	 * @param string $value
281
	 *
282
	 * @return string
283
	 * @throws PreConditionNotMetException
284
	 */
285
	public function setUserValue($key, $value) {
286
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
287
	}
288
289
290
	/**
291
	 * Get a user value by key and user
292
	 *
293
	 * @param string $userId
294
	 * @param string $key
295
	 *
296
	 * @param string $default
297
	 *
298
	 * @return string
299
	 */
300
	public function getCoreValueForUser($userId, $key, $default = '') {
301
		return $this->config->getUserValue($userId, 'core', $key, $default);
302
	}
303
304
305
	/**
306
	 * Get a user value by key and user
307
	 *
308
	 * @param string $userId
309
	 * @param string $key
310
	 *
311
	 * @return string
312
	 */
313
	public function getValueForUser($userId, $key) {
314
		return $this->config->getUserValue($userId, $this->appName, $key);
315
	}
316
317
	/**
318
	 * Set a user value by key
319
	 *
320
	 * @param string $userId
321
	 * @param string $key
322
	 * @param string $value
323
	 *
324
	 * @return string
325
	 * @throws PreConditionNotMetException
326
	 */
327
	public function setValueForUser($userId, $key, $value) {
328
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
329
	}
330
331
	/**
332
	 * return the cloud version.
333
	 * if $complete is true, return a string x.y.z
334
	 *
335
	 * @param boolean $complete
336
	 *
337
	 * @return string|integer
338
	 */
339
	public function getCloudVersion($complete = false) {
340
		$ver = Util::getVersion();
341
342
		if ($complete) {
343
			return implode('.', $ver);
344
		}
345
346
		return $ver[0];
347
	}
348
}
349