1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\GRBAC; |
11
|
|
|
|
12
|
|
|
final class Role implements RoleInterface |
13
|
|
|
{ |
14
|
|
|
private string $name; |
15
|
|
|
|
16
|
|
|
private ?string $description = null; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array<bool> |
20
|
|
|
*/ |
21
|
|
|
private array $permissions = []; |
22
|
|
|
|
23
|
13 |
|
public function __construct(string $name, string $description = null) |
24
|
|
|
{ |
25
|
13 |
|
$this->name = $name; |
26
|
13 |
|
$this->description = $description; |
27
|
13 |
|
} |
28
|
|
|
|
29
|
8 |
|
public function name(): string |
30
|
|
|
{ |
31
|
8 |
|
return $this->name; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
public function description(): ?string |
35
|
|
|
{ |
36
|
2 |
|
return $this->description; |
37
|
|
|
} |
38
|
|
|
|
39
|
9 |
|
public function grant(PermissionInterface $permission): void |
40
|
|
|
{ |
41
|
9 |
|
$this->permissions[$permission->slug()] = true; |
42
|
9 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function revoke(PermissionInterface $permission): void |
45
|
|
|
{ |
46
|
1 |
|
unset($this->permissions[$permission->slug()]); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
4 |
|
public function deny(PermissionInterface $permission): void |
50
|
|
|
{ |
51
|
4 |
|
$this->permissions[$permission->slug()] = false; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
5 |
|
public function has(string $slug): bool |
55
|
|
|
{ |
56
|
5 |
|
return isset($this->permissions[$slug]); |
57
|
|
|
} |
58
|
|
|
|
59
|
10 |
|
public function allow(string $slug): bool |
60
|
|
|
{ |
61
|
10 |
|
return $this->permissions[$slug] ?? false; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function allowAny(array $slugs): bool |
65
|
|
|
{ |
66
|
1 |
|
foreach ($slugs as $slug) { |
67
|
1 |
|
if ($this->allow($slug)) { |
68
|
1 |
|
return true; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function allowAll(array $slugs): bool |
76
|
|
|
{ |
77
|
1 |
|
foreach ($slugs as $slug) { |
78
|
1 |
|
if (!$this->allow($slug)) { |
79
|
1 |
|
return false; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
1 |
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|