1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PermissionsProvider.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
|
|
|
use Nette\Utils; |
21
|
|
|
|
22
|
|
|
use IPub\Permissions\Entities; |
23
|
|
|
use IPub\Permissions\Exceptions; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Basic permissions provider |
27
|
|
|
* |
28
|
|
|
* @package iPublikuj:Permissions! |
29
|
|
|
* @subpackage Providers |
30
|
|
|
* |
31
|
|
|
* @author Adam Kadlec <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class PermissionsProvider implements IPermissionsProvider |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* Implement nette smart magic |
37
|
|
|
*/ |
38
|
|
|
use Nette\SmartObject; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Entities\IPermission[] |
42
|
|
|
*/ |
43
|
|
|
private $permissions = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param Entities\IResource|NULL $resource |
47
|
|
|
* @param string|NULL $privilege |
48
|
|
|
* @param array|NULL $details |
49
|
|
|
* @param callable|NULL $assertion |
50
|
|
|
* |
51
|
|
|
* @return Entities\IPermission |
52
|
|
|
*/ |
53
|
|
|
public function addPermission(Entities\IResource $resource = NULL, string $privilege = NULL, array $details = NULL, callable $assertion = NULL) : Entities\IPermission |
54
|
|
|
{ |
55
|
|
|
$permission = new Entities\Permission($resource, $privilege, $details, $assertion); |
|
|
|
|
56
|
|
|
|
57
|
|
|
if (array_key_exists((string) $permission, $this->permissions)) { |
58
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is already registered.', (string) $permission)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->permissions[(string) $permission] = $permission; |
62
|
|
|
|
63
|
|
|
return $permission; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $id |
68
|
|
|
* |
69
|
|
|
* @return Entities\IPermission |
70
|
|
|
*/ |
71
|
|
|
public function getPermission(string $id) : Entities\IPermission |
72
|
|
|
{ |
73
|
|
|
if (!array_key_exists($id, $this->permissions)) { |
74
|
|
|
throw new Exceptions\InvalidStateException(sprintf('Permission "%s" is not registered.', $id)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->permissions[$id]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return Entities\IPermission[] |
82
|
|
|
*/ |
83
|
|
|
public function findAll() : array |
84
|
|
|
{ |
85
|
|
|
return $this->permissions; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.