Completed
Push — master ( a2a170...aeff54 )
by Maxence
02:40
created

ConfigService::isNonSSLLinksAllowed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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