Completed
Pull Request — master (#331)
by Maxence
01:41
created

ConfigService::enforcePasswordProtection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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