1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WebComplete\rbac\resource; |
4
|
|
|
|
5
|
|
|
use WebComplete\rbac\entity\Permission; |
6
|
|
|
use WebComplete\rbac\entity\PermissionInterface; |
7
|
|
|
use WebComplete\rbac\entity\Role; |
8
|
|
|
use WebComplete\rbac\entity\RoleInterface; |
9
|
|
|
use WebComplete\rbac\exception\RbacException; |
10
|
|
|
|
11
|
|
|
abstract class AbstractResource implements ResourceInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Role[] |
15
|
|
|
*/ |
16
|
|
|
protected $roles = []; |
17
|
|
|
/** |
18
|
|
|
* @var Permission[] |
19
|
|
|
*/ |
20
|
|
|
protected $permissions = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $name |
24
|
|
|
* @param string $description |
25
|
|
|
* |
26
|
|
|
* @return RoleInterface |
27
|
|
|
* @throws RbacException |
28
|
|
|
*/ |
29
|
|
|
public function createRole(string $name, string $description = ''): RoleInterface |
30
|
|
|
{ |
31
|
|
|
if (isset($this->roles[$name])) { |
32
|
|
|
throw new RbacException('Role already exists'); |
33
|
|
|
} |
34
|
|
|
$role = new Role($name, $description, $this); |
35
|
|
|
$this->roles[$name] = $role; |
36
|
|
|
return $role; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @param string $description |
42
|
|
|
* |
43
|
|
|
* @return PermissionInterface |
44
|
|
|
* @throws \WebComplete\rbac\exception\RbacException |
45
|
|
|
*/ |
46
|
|
|
public function createPermission(string $name, string $description = ''): PermissionInterface |
47
|
|
|
{ |
48
|
|
|
if (isset($this->permissions[$name])) { |
49
|
|
|
throw new RbacException('Permission already exists'); |
50
|
|
|
} |
51
|
|
|
$permission = new Permission($name, $description, $this); |
52
|
|
|
$this->permissions[$name] = $permission; |
53
|
|
|
return $permission; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return RoleInterface[] |
58
|
|
|
*/ |
59
|
|
|
public function getRoles(): array |
60
|
|
|
{ |
61
|
|
|
return $this->roles; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $name |
66
|
|
|
* |
67
|
|
|
* @return RoleInterface|null |
68
|
|
|
*/ |
69
|
|
|
public function getRole(string $name) |
70
|
|
|
{ |
71
|
|
|
return $this->roles[$name] ?? null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $name |
76
|
|
|
*/ |
77
|
|
|
public function deleteRole(string $name) |
78
|
|
|
{ |
79
|
|
|
unset($this->roles[$name]); |
80
|
|
|
foreach ($this->getRoles() as $role) { |
81
|
|
|
$role->removeChild($name); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return PermissionInterface[] |
87
|
|
|
*/ |
88
|
|
|
public function getPermissions(): array |
89
|
|
|
{ |
90
|
|
|
return $this->permissions; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* |
96
|
|
|
* @return PermissionInterface|null |
97
|
|
|
*/ |
98
|
|
|
public function getPermission(string $name) |
99
|
|
|
{ |
100
|
|
|
return $this->permissions[$name] ?? null; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $name |
105
|
|
|
*/ |
106
|
|
|
public function deletePermission(string $name) |
107
|
|
|
{ |
108
|
|
|
unset($this->permissions[$name]); |
109
|
|
|
foreach ($this->getRoles() as $role) { |
110
|
|
|
$role->removePermission($name); |
111
|
|
|
} |
112
|
|
|
foreach ($this->getPermissions() as $permission) { |
113
|
|
|
$permission->removeChild($name); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
*/ |
119
|
|
|
public function clear() |
120
|
|
|
{ |
121
|
|
|
$this->roles = []; |
122
|
|
|
$this->permissions = []; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|