Completed
Pull Request — master (#32303)
by Victor
10:56
created

Permissions::toOcmPermissions()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 1
dl 0
loc 16
rs 9.4222
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Viktar Dubiniuk <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\FederatedFileSharing\Ocm;
23
24
use OCP\Constants;
25
26
/**
27
 * Class Permissions
28
 *
29
 * @package OCA\FederatedFileSharing\Ocm
30
 */
31
class Permissions {
32
	const OCM_PERMISSION_READ = 'read';
33
	const OCM_PERMISSION_WRITE = 'write';
34
	const OCM_PERMISSION_SHARE = 'share';
35
36
	/**
37
	 * Maps numeric permissions to an array of string permissions
38
	 *
39
	 * @param int $ocPermissions
40
	 *
41
	 * @return array
42
	 */
43
	public function toOcmPermissions($ocPermissions) {
44
		$ocPermissions = (int) $ocPermissions;
45
		$ocmPermissions = [];
46
		if ($ocPermissions & Constants::PERMISSION_READ) {
47
			$ocmPermissions[] = self::OCM_PERMISSION_READ . '';
48
		}
49
		if (($ocPermissions & Constants::PERMISSION_CREATE)
50
			|| ($ocPermissions & Constants::PERMISSION_UPDATE)
51
		) {
52
			$ocmPermissions[] = self::OCM_PERMISSION_WRITE;
53
		}
54
		if ($ocPermissions & Constants::PERMISSION_SHARE) {
55
			$ocmPermissions[] = self::OCM_PERMISSION_SHARE;
56
		}
57
		return $ocmPermissions;
58
	}
59
60
	/**
61
	 * @param string[] $ocmPermissions
62
	 *
63
	 * @return int
64
	 */
65
	public function toOcPermissions($ocmPermissions) {
66
		$permissionMap = [
67
			self::OCM_PERMISSION_READ => Constants::PERMISSION_READ,
68
			self::OCM_PERMISSION_WRITE => Constants::PERMISSION_CREATE + Constants::PERMISSION_UPDATE,
69
			self::OCM_PERMISSION_SHARE => Constants::PERMISSION_SHARE
70
		];
71
		$ocPermissions = 0;
72
		foreach ($ocmPermissions as $ocmPermission) {
73
			if (isset($permissionMap[$ocmPermission])) {
74
				$ocPermissions += $permissionMap[$ocmPermission];
75
			}
76
		}
77
78
		return $ocPermissions;
79
	}
80
}
81