Completed
Pull Request — master (#94)
by Maxence
02:28
created

ConfigService::isFederatedAllowed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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