Completed
Push — master ( 8e890d...fd9088 )
by Julius
02:16
created

Acl::setParticipant()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 8
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
	protected $participantDisplayname;
45
46
	public function __construct() {
47
		$this->addType('id', 'integer');
48
		$this->addType('boardId', 'integer');
49
		$this->addType('permissionEdit', 'boolean');
50
		$this->addType('permissionShare', 'boolean');
51
		$this->addType('permissionManage', 'boolean');
52
		$this->addType('owner', 'boolean');
53
		$this->addType('type', 'integer');
54
		$this->addRelation('owner');
55
		$this->addRelation('participantDisplayname');
56
		$this->setPermissionEdit(false);
57
		$this->setPermissionShare(false);
58
		$this->setPermissionManage(false);
59
	}
60
61
	public function getPermission($permission) {
62
		switch ($permission) {
63
			case Acl::PERMISSION_READ:
64
				return true;
65
			case Acl::PERMISSION_EDIT:
66
				return $this->getPermissionEdit();
67
			case Acl::PERMISSION_SHARE:
68
				return $this->getPermissionShare();
69
			case Acl::PERMISSION_MANAGE:
70
				return $this->getPermissionManage();
71
		}
72
		return false;
73
	}
74
75
	public function jsonSerialize() {
76
		return [
77
			'id' => $this->id,
78
			'participant' => $this->participant,
79
			'displayname' => $this->getParticipantDisplayname(),
80
			'type' => $this->getTypeString(),
81
			'boardId' => $this->boardId,
82
			'permissionEdit' => $this->getPermissionEdit(),
83
			'permissionShare' => $this->getPermissionShare(),
84
			'permissionManage' => $this->getPermissionManage(),
85
			'owner' => $this->getOwner()
86
		];
87
	}
88
	
89
	public function getTypeString() {
90
		if ($this->type === Acl::PERMISSION_TYPE_GROUP) {
91
			return 'group';
92
		}
93
		return 'user';
94
	}
95
96
	public function setType($type) {
97
		// FIXME: Remove when all javascript uses numeric types
98
		if ($type === 'group' || $type === '1') {
99
			$typeInt = Acl::PERMISSION_TYPE_GROUP;
100
		} else {
101
			$typeInt = Acl::PERMISSION_TYPE_USER;
102
		}
103
		$this->markFieldUpdated('type');
104
		$this->type = $typeInt;
105
	}
106
107
	public function setParticipant($participant) {
108
		if($this->getType() === Acl::PERMISSION_TYPE_USER) {
109
			$dn = \OC::$server->getUserManager()->get($participant)->getDisplayName();
110
		} else {
111
			$dn = \OC::$server->getGroupManager()->get($participant)->getDisplayName();
112
		}
113
		$this->participantDisplayname = $dn;
114
		$this->markFieldUpdated('participant');
115
		$this->participant = $participant;
116
	}
117
}