-
Notifications
You must be signed in to change notification settings - Fork 114
/
ACL.php
123 lines (107 loc) · 3.14 KB
/
ACL.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
namespace AgenDAV\CalDAV\Share;
use AgenDAV\CalDAV\Share\Permissions;
/*
* Copyright (C) Jorge López Pérez <jorge@adobo.org>
*
* This file is part of AgenDAV.
*
* AgenDAV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* AgenDAV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AgenDAV. If not, see <http://www.gnu.org/licenses/>.
*/
class ACL
{
/**
* Privileges storage
*
* @var \AgenDAV\CalDAV\Share\Permissions
*/
private $permissions;
/**
* Current grants
*
* @var array $grants
*/
private $grants;
/**
* Creates a new ACL
*
* @param \AgenDAV\CalDAV\Share\Permissions $permissions
*/
public function __construct(Permissions $permissions)
{
$this->permissions = $permissions;
$this->grants = [];
}
/**
* Adds a new grant to this ACL
*
* @param string $principal Principal path
* @param string $role Principal role (e.g. owner, read-write, read-only, default)
* @throws \InvalidArgumentException if role is owner or default
* @throws \RuntimeException if grant for $principal was already set
*/
public function addGrant($principal, $role)
{
if ($role === 'owner' || $role === 'default') {
throw new \InvalidArgumentException('Forbidden ACL grant with role: ' . $role);
}
if (isset($this->grants[$principal])) {
throw new \RuntimeException('ACL grant already set for ' . $principal);
}
$this->grants[$principal] = $role;
}
/**
* Gets a list of grants provided by this ACL
*
* @return array
*/
public function getGrants()
{
return $this->grants;
}
/**
* Returns a list of privileges configured for the owner of the resource
* affected by this ACL
*
* @return array
*/
public function getOwnerPrivileges()
{
return $this->permissions->getPrivilegesFor('owner');
}
/**
* Returns a list of default privileges configured for the resource affected
* by this ACL
*
* @return array
*/
public function getDefaultPrivileges()
{
return $this->permissions->getPrivilegesFor('default');
}
/**
* Gets a list of privileges given to granted principals
*
* @return array Associative array, where keys = owner/default/{principal-URL}
* and values are arrays of privileges in Clark notation
*/
public function getGrantsPrivileges()
{
$result = [];
foreach ($this->grants as $principal => $role) {
$result[$principal] = $this->permissions->getPrivilegesFor($role);
}
return $result;
}
}