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