-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoutePermissionGuard.php
142 lines (122 loc) · 4.32 KB
/
RoutePermissionGuard.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* @see https://github.com/dotkernel/dot-rbac-guard/ for the canonical source repository
* @copyright Copyright (c) 2017 Apidemia (https://www.apidemia.com)
* @license https://github.com/dotkernel/dot-rbac-guard/blob/master/LICENSE.md MIT License
*/
declare(strict_types = 1);
namespace Dot\Rbac\Guard\Guard;
use Dot\Authorization\AuthorizationInterface;
use Dot\Rbac\Guard\Exception\InvalidArgumentException;
use Dot\Rbac\Guard\Exception\RuntimeException;
use Psr\Http\Message\ServerRequestInterface;
use Mezzio\Router\RouteResult;
/**
* Class RoutePermissionGuard
* @package Dot\Rbac\Guard\Route
*/
class RoutePermissionGuard extends AbstractGuard
{
const PRIORITY = 70;
/**
* @var AuthorizationInterface
*/
protected $authorizationService;
/**\
* RoutePermissionGuard constructor.
* @param array $options
*/
public function __construct(array $options = null)
{
$options = $options ?? [];
parent::__construct($options);
if (isset($options['authorization_service'])
&& $options['authorization_service'] instanceof AuthorizationInterface
) {
$this->setAuthorizationService($options['authorization_service']);
}
if (!$this->authorizationService instanceof AuthorizationInterface) {
throw new RuntimeException('Authorization service is required by this guard and was not set');
}
}
/**
* @param array $rules
*/
public function setRules(array $rules)
{
$this->rules = [];
foreach ($rules as $key => $value) {
if (is_int($key)) {
$routeName = strtolower($value);
$permissions = [];
} else {
$routeName = strtolower($key);
$permissions = (array)$value;
}
$this->rules[$routeName] = $permissions;
}
}
/**
* @param ServerRequestInterface $request
* @return bool
*/
public function isGranted(ServerRequestInterface $request): bool
{
$routeResult = $request->getAttribute(RouteResult::class, false);
//if we dont have a matched route(probably 404 not found) let it go to the final handler
if (!$routeResult instanceof RouteResult) {
return $this->protectionPolicy === self::POLICY_ALLOW;
}
$matchedRouteName = strtolower($routeResult->getMatchedRouteName());
$allowedPermissions = null;
foreach (array_keys($this->rules) as $routeName) {
if ($matchedRouteName === $routeName) {
$allowedPermissions = $this->rules[$routeName];
break;
}
}
// If no rules apply, it is considered as granted or not based on the protection policy
if (null === $allowedPermissions) {
return $this->protectionPolicy === self::POLICY_ALLOW;
}
if (in_array('*', $allowedPermissions)) {
return true;
}
$permissions = $allowedPermissions['permissions'] ?? $allowedPermissions;
$condition = $allowedPermissions['condition'] ?? GuardInterface::CONDITION_AND;
if (GuardInterface::CONDITION_AND === $condition) {
foreach ($permissions as $permission) {
if (!$this->getAuthorizationService()->isGranted($permission)) {
return false;
}
}
return true;
}
if (GuardInterface::CONDITION_OR === $condition) {
foreach ($permissions as $permission) {
if ($this->getAuthorizationService()->isGranted($permission)) {
return true;
}
}
return false;
}
throw new InvalidArgumentException(sprintf(
'Condition must be either "AND" or "OR", %s given',
is_object($condition) ? get_class($condition) : gettype($condition)
));
}
/**
* @return AuthorizationInterface
*/
public function getAuthorizationService(): AuthorizationInterface
{
return $this->authorizationService;
}
/**
* @param AuthorizationInterface $authorizationService
*/
public function setAuthorizationService(AuthorizationInterface $authorizationService)
{
$this->authorizationService = $authorizationService;
}
}