Acl::setType()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 *  This program is free software: you can redistribute it and/or modify
10
 *  it under the terms of the GNU Affero General Public License as
11
 *  published by the Free Software Foundation, either version 3 of the
12
 *  License, or (at your option) any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU Affero General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU Affero General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\Deck\Db;
25
26
class Acl extends RelationalEntity implements \JsonSerializable {
27
28
	const PERMISSION_READ = 0;
29
	const PERMISSION_EDIT = 1;
30
	const PERMISSION_SHARE = 2;
31
	const PERMISSION_MANAGE = 3;
32
33
	const PERMISSION_TYPE_USER = 0;
34
	const PERMISSION_TYPE_GROUP = 1;
35
36
	public $id;
37
	protected $participant;
38
	protected $type;
39
	protected $boardId;
40
	protected $permissionEdit = false;
41
	protected $permissionShare = false;
42
	protected $permissionManage = false;
43
	protected $owner = false;
44
45
	public function __construct() {
46
		$this->addType('id', 'integer');
47
		$this->addType('boardId', 'integer');
48
		$this->addType('permissionEdit', 'boolean');
49
		$this->addType('permissionShare', 'boolean');
50
		$this->addType('permissionManage', 'boolean');
51
		$this->addType('owner', 'boolean');
52
		$this->addType('type', 'integer');
53
		$this->addRelation('owner');
54
		$this->setPermissionEdit(false);
55
		$this->setPermissionShare(false);
56
		$this->setPermissionManage(false);
57
	}
58
59
	public function getPermission($permission) {
60
		switch ($permission) {
61
			case Acl::PERMISSION_READ:
62
				return true;
63
			case Acl::PERMISSION_EDIT:
64
				return $this->getPermissionEdit();
65
			case Acl::PERMISSION_SHARE:
66
				return $this->getPermissionShare();
67
			case Acl::PERMISSION_MANAGE:
68
				return $this->getPermissionManage();
69
		}
70
		return false;
71
	}
72
73
	public function jsonSerialize() {
74
		return [
75
			'id' => $this->id,
76
			'participant' => $this->participant,
77
			'type' => $this->getTypeString(),
78
			'boardId' => $this->boardId,
79
			'permissionEdit' => $this->getPermissionEdit(),
80
			'permissionShare' => $this->getPermissionShare(),
81
			'permissionManage' => $this->getPermissionManage(),
82
			'owner' => $this->getOwner()
83
		];
84
	}
85
	
86
	public function getTypeString() {
87
		if ($this->type === Acl::PERMISSION_TYPE_GROUP) {
88
			return 'group';
89
		}
90
		return 'user';
91
	}
92
93
	public function setType($type) {
94
		// FIXME: Remove when all javascript uses numeric types
95
		if ($type === 'group' || $type === '1') {
96
			$typeInt = Acl::PERMISSION_TYPE_GROUP;
97
		} else {
98
			$typeInt = Acl::PERMISSION_TYPE_USER;
99
		}
100
		$this->markFieldUpdated('type');
101
		$this->type = $typeInt;
102
	}
103
}