Completed
Push — master ( 17691e...d906ba )
by Sujith
16:09 queued 04:29
created

StorageConfig::jsonSerialize()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 23
nc 256
nop 0
dl 0
loc 31
rs 3
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Jesús Macias <[email protected]>
4
 * @author Lukas Reschke <[email protected]>
5
 * @author Robin Appelman <[email protected]>
6
 * @author Robin McCorkell <[email protected]>
7
 * @author Vincent Petry <[email protected]>
8
 *
9
 * @copyright Copyright (c) 2017, ownCloud GmbH
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OC\Files\External;
27
28
use OCP\Files\External\IStorageConfig;
29
use OCP\Files\External\Auth\IUserProvided;
30
use OCP\Files\External\Auth\AuthMechanism;
31
use OCP\Files\External\Backend\Backend;
32
33
/**
34
 * External storage configuration
35
 */
36
class StorageConfig implements IStorageConfig {
37
	/**
38
	 * Storage config id
39
	 *
40
	 * @var int
41
	 */
42
	private $id;
43
44
	/**
45
	 * Backend
46
	 *
47
	 * @var Backend
48
	 */
49
	private $backend;
50
51
	/**
52
	 * Authentication mechanism
53
	 *
54
	 * @var AuthMechanism
55
	 */
56
	private $authMechanism;
57
58
	/**
59
	 * Backend options
60
	 *
61
	 * @var array
62
	 */
63
	private $backendOptions = [];
64
65
	/**
66
	 * Mount point path, relative to the user's "files" folder
67
	 *
68
	 * @var string
69
	 */
70
	private $mountPoint;
71
72
	/**
73
	 * Storage status
74
	 *
75
	 * @var int
76
	 */
77
	private $status;
78
79
	/**
80
	 * Status message
81
	 *
82
	 * @var string
83
	 */
84
	private $statusMessage;
85
86
	/**
87
	 * Priority
88
	 *
89
	 * @var int
90
	 */
91
	private $priority;
92
93
	/**
94
	 * List of users who have access to this storage
95
	 *
96
	 * @var array
97
	 */
98
	private $applicableUsers = [];
99
100
	/**
101
	 * List of groups that have access to this storage
102
	 *
103
	 * @var array
104
	 */
105
	private $applicableGroups = [];
106
107
	/**
108
	 * Mount-specific options
109
	 *
110
	 * @var array
111
	 */
112
	private $mountOptions = [];
113
114
	/**
115
	 * Whether it's a personal or admin mount
116
	 *
117
	 * @var int
118
	 */
119
	private $type;
120
121
	/**
122
	 * Creates a storage config
123
	 *
124
	 * @param int|null $id config id or null for a new config
125
	 */
126
	public function __construct($id = null) {
127
		$this->id = $id;
128
		$this->mountOptions['enable_sharing'] = false;
129
	}
130
131
	/**
132
	 * Returns the configuration id
133
	 *
134
	 * @return int
135
	 */
136
	public function getId() {
137
		return $this->id;
138
	}
139
140
	/**
141
	 * Sets the configuration id
142
	 *
143
	 * @param int $id configuration id
144
	 */
145
	public function setId($id) {
146
		$this->id = $id;
147
	}
148
149
	/**
150
	 * Returns mount point path relative to the user's
151
	 * "files" folder.
152
	 *
153
	 * @return string path
154
	 */
155
	public function getMountPoint() {
156
		return $this->mountPoint;
157
	}
158
159
	/**
160
	 * Sets mount point path relative to the user's
161
	 * "files" folder.
162
	 * The path will be normalized.
163
	 *
164
	 * @param string $mountPoint path
165
	 */
166
	public function setMountPoint($mountPoint) {
167
		$this->mountPoint = \OC\Files\Filesystem::normalizePath($mountPoint);
168
	}
169
170
	/**
171
	 * @return Backend
172
	 */
173
	public function getBackend() {
174
		return $this->backend;
175
	}
176
177
	/**
178
	 * @param Backend $backend
179
	 */
180
	public function setBackend(Backend $backend) {
181
		$this->backend= $backend;
182
	}
183
184
	/**
185
	 * @return AuthMechanism
186
	 */
187
	public function getAuthMechanism() {
188
		return $this->authMechanism;
189
	}
190
191
	/**
192
	 * @param AuthMechanism $authMechanism
193
	 */
194
	public function setAuthMechanism(AuthMechanism $authMechanism) {
195
		$this->authMechanism = $authMechanism;
196
	}
197
198
	/**
199
	 * Returns the external storage backend-specific options
200
	 *
201
	 * @return array backend options
202
	 */
203
	public function getBackendOptions() {
204
		return $this->backendOptions;
205
	}
206
207
	/**
208
	 * Sets the external storage backend-specific options
209
	 *
210
	 * @param array $backendOptions backend options
211
	 */
212
	public function setBackendOptions($backendOptions) {
213
		if($this->getBackend() instanceof  Backend) {
214
			$parameters = $this->getBackend()->getParameters();
215
			foreach($backendOptions as $key => $value) {
216
				if(isset($parameters[$key])) {
217
					switch ($parameters[$key]->getType()) {
218
						case \OCP\Files\External\DefinitionParameter::VALUE_BOOLEAN:
219
							$value = (bool)$value;
220
							break;
221
					}
222
					$backendOptions[$key] = $value;
223
				}
224
				if(is_string($backendOptions[$key])) {
225
					if (($key === 'public_key') || ($key === 'private_key')) {
226
						if (base64_decode($backendOptions[$key], true) === false) {
227
							$backendOptions[$key] = base64_encode($backendOptions[$key]);
228
						}
229
					}
230
231
					$backendOptions[$key] = str_replace(["\n", "\r"], "", $backendOptions[$key]);
232
				}
233
			}
234
		}
235
236
		$this->backendOptions = $backendOptions;
237
	}
238
239
	/**
240
	 * @param string $key
241
	 * @return mixed
242
	 */
243
	public function getBackendOption($key) {
244
		if (isset($this->backendOptions[$key])) {
245
			if (($key === 'private_key') || ($key === 'public_key')) {
246
				$decodedString = base64_decode($this->backendOptions[$key], true);
247
				if ($decodedString !== false) {
248
					return $decodedString;
249
				}
250
			}
251
252
			return $this->backendOptions[$key];
253
		}
254
		return null;
255
	}
256
257
	/**
258
	 * @param string $key
259
	 * @param mixed $value
260
	 */
261
	public function setBackendOption($key, $value) {
262
		$this->backendOptions[$key] = $value;
263
	}
264
265
	/**
266
	 * Returns the mount priority
267
	 *
268
	 * @return int priority
269
	 */
270
	public function getPriority() {
271
		return $this->priority;
272
	}
273
274
	/**
275
	 * Sets the mount priotity
276
	 *
277
	 * @param int $priority priority
278
	 */
279
	public function setPriority($priority) {
280
		$this->priority = $priority;
281
	}
282
283
	/**
284
	 * Returns the users for which to mount this storage
285
	 *
286
	 * @return array applicable users
287
	 */
288
	public function getApplicableUsers() {
289
		return $this->applicableUsers;
290
	}
291
292
	/**
293
	 * Sets the users for which to mount this storage
294
	 *
295
	 * @param array|null $applicableUsers applicable users
296
	 */
297
	public function setApplicableUsers($applicableUsers) {
298
		if (is_null($applicableUsers)) {
299
			$applicableUsers = [];
300
		}
301
		$this->applicableUsers = $applicableUsers;
302
	}
303
304
	/**
305
	 * Returns the groups for which to mount this storage
306
	 *
307
	 * @return array applicable groups
308
	 */
309
	public function getApplicableGroups() {
310
		return $this->applicableGroups;
311
	}
312
313
	/**
314
	 * Sets the groups for which to mount this storage
315
	 *
316
	 * @param array|null $applicableGroups applicable groups
317
	 */
318
	public function setApplicableGroups($applicableGroups) {
319
		if (is_null($applicableGroups)) {
320
			$applicableGroups = [];
321
		}
322
		$this->applicableGroups = $applicableGroups;
323
	}
324
325
	/**
326
	 * Returns the mount-specific options
327
	 *
328
	 * @return array mount specific options
329
	 */
330
	public function getMountOptions() {
331
		return $this->mountOptions;
332
	}
333
334
	/**
335
	 * Sets the mount-specific options
336
	 *
337
	 * @param array $mountOptions applicable groups
338
	 */
339
	public function setMountOptions($mountOptions) {
340
		if (is_null($mountOptions)) {
341
			$mountOptions = [];
342
		}
343
		$this->mountOptions = $mountOptions;
344
	}
345
346
	/**
347
	 * @param string $key
348
	 * @return mixed
349
	 */
350
	public function getMountOption($key) {
351
		if (isset($this->mountOptions[$key])) {
352
			return $this->mountOptions[$key];
353
		}
354
		return null;
355
	}
356
357
	/**
358
	 * @param string $key
359
	 * @param mixed $value
360
	 */
361
	public function setMountOption($key, $value) {
362
		$this->mountOptions[$key] = $value;
363
	}
364
365
	/**
366
	 * Gets the storage status, whether the config worked last time
367
	 *
368
	 * @return int $status status
369
	 */
370
	public function getStatus() {
371
		return $this->status;
372
	}
373
374
	/**
375
	 * Gets the message describing the storage status
376
	 *
377
	 * @return string|null
378
	 */
379
	public function getStatusMessage() {
380
		return $this->statusMessage;
381
	}
382
383
	/**
384
	 * Sets the storage status, whether the config worked last time
385
	 *
386
	 * @param int $status status
387
	 * @param string|null $message optional message
388
	 */
389
	public function setStatus($status, $message = null) {
390
		$this->status = $status;
391
		$this->statusMessage = $message;
392
	}
393
394
	/**
395
	 * @return int self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
396
	 */
397
	public function getType() {
398
		return $this->type;
399
	}
400
401
	/**
402
	 * @param int $type self::MOUNT_TYPE_ADMIN or self::MOUNT_TYPE_PERSONAl
403
	 */
404
	public function setType($type) {
405
		$this->type = $type;
406
	}
407
408
	/**
409
	 * Serialize config to JSON
410
	 *
411
	 * @return array
412
	 */
413
	public function jsonSerialize() {
414
		$result = [];
415
		if (!is_null($this->id)) {
416
			$result['id'] = $this->id;
417
		}
418
		$result['mountPoint'] = $this->mountPoint;
419
		$result['backend'] = $this->backend->getIdentifier();
420
		$result['authMechanism'] = $this->authMechanism->getIdentifier();
421
		$result['backendOptions'] = $this->backendOptions;
422
		if (!is_null($this->priority)) {
423
			$result['priority'] = $this->priority;
424
		}
425
		if (!empty($this->applicableUsers)) {
426
			$result['applicableUsers'] = $this->applicableUsers;
427
		}
428
		if (!empty($this->applicableGroups)) {
429
			$result['applicableGroups'] = $this->applicableGroups;
430
		}
431
		if (!empty($this->mountOptions)) {
432
			$result['mountOptions'] = $this->mountOptions;
433
		}
434
		if (!is_null($this->status)) {
435
			$result['status'] = $this->status;
436
		}
437
		if (!is_null($this->statusMessage)) {
438
			$result['statusMessage'] = $this->statusMessage;
439
		}
440
		$result['userProvided'] = $this->authMechanism instanceof IUserProvided;
441
		$result['type'] = ($this->getType() === self::MOUNT_TYPE_PERSONAl) ? 'personal': 'system';
442
		return $result;
443
	}
444
}
445