Completed
Pull Request — master (#203)
by Maxence
16:54
created

ConfigService::getCoreValueForUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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