1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* RolesProvider.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license https://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec <[email protected]> |
8
|
|
|
* @package iPublikuj:Permissions! |
9
|
|
|
* @subpackage Providers |
10
|
|
|
* @since 2.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 30.11.16 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\Permissions\Providers; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
|
21
|
|
|
use IPub\Permissions\Entities; |
22
|
|
|
use IPub\Permissions\Exceptions; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Basic roles provider |
26
|
|
|
* |
27
|
|
|
* @package iPublikuj:Permissions! |
28
|
|
|
* @subpackage Providers |
29
|
|
|
* |
30
|
|
|
* @author Adam Kadlec <[email protected]> |
31
|
|
|
*/ |
32
|
1 |
|
class RolesProvider implements IRolesProvider |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Implement nette smart magic |
36
|
|
|
*/ |
37
|
1 |
|
use Nette\SmartObject; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Entities\IRole[] |
41
|
|
|
*/ |
42
|
|
|
private $roles = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $id |
46
|
|
|
* @param Entities\IRole|NULL $parent |
47
|
|
|
* @param Entities\IPermission|Entities\IPermission[]|NULL $permissions |
48
|
|
|
* |
49
|
|
|
* @return Entities\IRole |
50
|
|
|
*/ |
51
|
|
|
public function addRole(string $id, ?Entities\IRole $parent = NULL, $permissions = NULL) : Entities\IRole |
52
|
|
|
{ |
53
|
1 |
|
if (array_key_exists($id, $this->roles)) { |
54
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Role "%s" has been already added.', $id)); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
if ($permissions instanceof Entities\IPermission) { |
58
|
1 |
|
$permissions = [$permissions]; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$role = new Entities\Role($id); |
62
|
|
|
|
63
|
1 |
|
if ($parent) { |
64
|
1 |
|
$role->setParent($parent); |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if ($permissions) { |
68
|
1 |
|
$role->setPermissions($permissions); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
$this->roles[$id] = $role; |
72
|
|
|
|
73
|
1 |
|
return $role; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $roleName |
78
|
|
|
* |
79
|
|
|
* @return Entities\IRole |
80
|
|
|
*/ |
81
|
|
|
public function getRole(string $roleName) : Entities\IRole |
82
|
|
|
{ |
83
|
1 |
|
if (!array_key_exists($roleName, $this->roles)) { |
84
|
|
|
throw new Exceptions\InvalidArgumentException(sprintf('Role "%s" is not registered.', $roleName)); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->roles[$roleName]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return Entities\IRole[] |
92
|
|
|
*/ |
93
|
|
|
public function findAll() : array |
94
|
|
|
{ |
95
|
1 |
|
return $this->roles; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|