|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Spiral Framework. |
|
4
|
|
|
* |
|
5
|
|
|
* @license MIT |
|
6
|
|
|
* @author Anton Titov (Wolfy-J) |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace Spiral\Security\Traits; |
|
9
|
|
|
|
|
10
|
|
|
use Interop\Container\ContainerInterface; |
|
11
|
|
|
use Spiral\Core\Exceptions\ScopeException; |
|
12
|
|
|
use Spiral\Security\GuardInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Embeds GuardInterface functionality into class and provides ability to isolate permissions |
|
16
|
|
|
* using guard namespace. |
|
17
|
|
|
*/ |
|
18
|
|
|
trait GuardedTrait |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* Instance specific guard instance. |
|
22
|
|
|
* |
|
23
|
|
|
* @see getGuard() |
|
24
|
|
|
* @var GuardInterface|null |
|
25
|
|
|
*/ |
|
26
|
|
|
private $guard = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Set instance specific guard. |
|
30
|
|
|
* |
|
31
|
|
|
* @param GuardInterface $guard |
|
32
|
|
|
*/ |
|
33
|
|
|
public function setGuard(GuardInterface $guard) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->guard = $guard; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return GuardInterface |
|
40
|
|
|
* |
|
41
|
|
|
* @throws ScopeException |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getGuard(): GuardInterface |
|
44
|
|
|
{ |
|
45
|
|
|
$container = $this->iocContainer(); |
|
46
|
|
|
|
|
47
|
|
|
if (empty($container)) { |
|
48
|
|
|
throw new ScopeException("Unable to create guard, no container is available"); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (empty($this->guard)) { |
|
52
|
|
|
$this->guard = $this->iocContainer()->get(GuardInterface::class); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $this->guard; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $permission |
|
60
|
|
|
* @param array $context |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function allows(string $permission, array $context = []): bool |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->getGuard()->allows($this->resolvePermission($permission), $context); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $permission |
|
71
|
|
|
* @param array $context |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function denies(string $permission, array $context = []): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return !$this->allows($permission, $context); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Automatically prepend permission name with local RBAC namespace. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $permission |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function resolvePermission(string $permission): string |
|
88
|
|
|
{ |
|
89
|
|
|
if (defined('self::GUARD_NAMESPACE')) { |
|
90
|
|
|
//Yay! Isolation |
|
91
|
|
|
$permission = constant(get_called_class() . '::' . 'GUARD_NAMESPACE') . GuardInterface::NS_SEPARATOR . $permission; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $permission; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return ContainerInterface|null |
|
99
|
|
|
*/ |
|
100
|
|
|
abstract protected function iocContainer(); |
|
101
|
|
|
} |