Completed
Push — master ( efaaf4...d4221f )
by Maxence
02:49
created

ConfigService::isLocalNonSSL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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\Model\Circle;
30
use OCP\IConfig;
31
use OCP\Util;
32
33
class ConfigService {
34
35
	const CIRCLES_ALLOW_CIRCLES = 'allow_circles';
36
	const CIRCLES_SWAP_TO_TEAMS = 'swap_to_teams';
37
	const CIRCLES_ALLOW_FEDERATED_CIRCLES = 'allow_federated';
38
	const CIRCLES_ALLOW_LINKED_GROUPS = 'allow_linked_groups';
39
	const CIRCLES_ALLOW_NON_SSL_LINKS = 'allow_non_ssl_links';
40
	const CIRCLES_ALLOW_NON_SSL_LOCAL = 'local_is_non_ssl';
41
42
	private $defaults = [
43
		self::CIRCLES_ALLOW_CIRCLES           => Circle::CIRCLES_ALL,
44
		self::CIRCLES_SWAP_TO_TEAMS           => '0',
45
		self::CIRCLES_ALLOW_LINKED_GROUPS     => '0',
46
		self::CIRCLES_ALLOW_FEDERATED_CIRCLES => '0',
47
		self::CIRCLES_ALLOW_NON_SSL_LINKS     => '0',
48
		self::CIRCLES_ALLOW_NON_SSL_LOCAL     => '0'
49
	];
50
51
	/** @var string */
52
	private $appName;
53
54
	/** @var IConfig */
55
	private $config;
56
57
	/** @var string */
58
	private $userId;
59
60
	/** @var MiscService */
61
	private $miscService;
62
63
	/** @var int */
64
	private $allowedCircle = -1;
65
66
	/** @var int */
67
	private $allowedLinkedGroups = -1;
68
69
	/** @var int */
70
	private $allowedFederatedCircles = -1;
71
72
	/** @var int */
73
	private $allowedNonSSLLinks = -1;
74
75
	/** @var int */
76
	private $localNonSSL = -1;
77
78
	public function __construct($appName, IConfig $config, $userId, MiscService $miscService) {
79
		$this->appName = $appName;
80
		$this->config = $config;
81
		$this->userId = $userId;
82
		$this->miscService = $miscService;
83
	}
84
85
86
	/**
87
	 * returns if this type of circle is allowed by the current configuration.
88
	 *
89
	 * @param $type
90
	 *
91
	 * @return int
92
	 */
93
	public function isCircleAllowed($type) {
94
		if ($this->allowedCircle === -1) {
95
			$this->allowedCircle = (int)$this->getAppValue(self::CIRCLES_ALLOW_CIRCLES);
96
		}
97
98
		return ((int)$type & (int)$this->allowedCircle);
99
	}
100
101
102
	/**
103
	 * @return bool
104
	 */
105
	public function isLinkedGroupsAllowed() {
106
		if ($this->allowedLinkedGroups === -1) {
107
			$this->allowedLinkedGroups =
108
				(int)$this->getAppValue(self::CIRCLES_ALLOW_LINKED_GROUPS);
109
		}
110
111
		return ($this->allowedLinkedGroups === 1);
112
	}
113
114
115
	/**
116
	 * @return bool
117
	 */
118
	public function isFederatedCirclesAllowed() {
119
		if ($this->allowedFederatedCircles === -1) {
120
			$this->allowedFederatedCircles =
121
				(int)$this->getAppValue(self::CIRCLES_ALLOW_FEDERATED_CIRCLES);
122
		}
123
124
		return ($this->allowedFederatedCircles === 1);
125
	}
126
127
128
	/**
129
	 * @return bool
130
	 */
131
	public function isLocalNonSSL() {
132
		if ($this->localNonSSL === -1) {
133
			$this->localNonSSL =
134
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LOCAL);
135
		}
136
137
		return ($this->localNonSSL === 1);
138
	}
139
140
141
	/**
142
	 * @return bool
143
	 */
144
	public function isNonSSLLinksAllowed() {
145
		if ($this->allowedNonSSLLinks === -1) {
146
			$this->allowedNonSSLLinks =
147
				(int)$this->getAppValue(self::CIRCLES_ALLOW_NON_SSL_LINKS);
148
		}
149
150
		return ($this->allowedNonSSLLinks === 1);
151
	}
152
153
	/**
154
	 * Get a value by key
155
	 *
156
	 * @param string $key
157
	 *
158
	 * @return string
159
	 */
160
	public function getAppValue($key) {
161
		$defaultValue = null;
162
163
		if (array_key_exists($key, $this->defaults)) {
164
			$defaultValue = $this->defaults[$key];
165
		}
166
167
		return $this->config->getAppValue($this->appName, $key, $defaultValue);
168
	}
169
170
	/**
171
	 * Set a value by key
172
	 *
173
	 * @param string $key
174
	 * @param string $value
175
	 *
176
	 * @return void
177
	 */
178
	public function setAppValue($key, $value) {
179
		$this->config->setAppValue($this->appName, $key, $value);
180
	}
181
182
	/**
183
	 * remove a key
184
	 *
185
	 * @param string $key
186
	 *
187
	 * @return string
188
	 */
189
	public function deleteAppValue($key) {
190
		return $this->config->deleteAppValue($this->appName, $key);
191
	}
192
193
	/**
194
	 * Get a user value by key
195
	 *
196
	 * @param string $key
197
	 *
198
	 * @return string
199
	 */
200
	public function getUserValue($key) {
201
		return $this->config->getUserValue($this->userId, $this->appName, $key);
202
	}
203
204
	/**
205
	 * Set a user value by key
206
	 *
207
	 * @param string $key
208
	 * @param string $value
209
	 *
210
	 * @return string
211
	 */
212
	public function setUserValue($key, $value) {
213
		return $this->config->setUserValue($this->userId, $this->appName, $key, $value);
214
	}
215
216
	/**
217
	 * Get a user value by key and user
218
	 *
219
	 * @param string $userId
220
	 * @param string $key
221
	 *
222
	 * @return string
223
	 */
224
	public function getValueForUser($userId, $key) {
225
		return $this->config->getUserValue($userId, $this->appName, $key);
226
	}
227
228
	/**
229
	 * Set a user value by key
230
	 *
231
	 * @param string $userId
232
	 * @param string $key
233
	 * @param string $value
234
	 *
235
	 * @return string
236
	 */
237
	public function setValueForUser($userId, $key, $value) {
238
		return $this->config->setUserValue($userId, $this->appName, $key, $value);
239
	}
240
241
	/**
242
	 * return the cloud version.
243
	 * if $complete is true, return a string x.y.z
244
	 *
245
	 * @param boolean $complete
246
	 *
247
	 * @return string|integer
248
	 */
249
	public function getCloudVersion($complete = false) {
250
		$ver = Util::getVersion();
251
252
		if ($complete) {
253
			return implode('.', $ver);
254
		}
255
256
		return $ver[0];
257
	}
258
}
259