-
Notifications
You must be signed in to change notification settings - Fork 0
/
RouteGuard.php
116 lines (97 loc) · 3.06 KB
/
RouteGuard.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
<?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\Rbac\Guard\Exception\RuntimeException;
use Dot\Rbac\Role\RoleServiceInterface;
use Psr\Http\Message\ServerRequestInterface;
use Mezzio\Router\RouteResult;
/**
* Class RouteGuard
* @package Dot\Rbac\Guard\Route
*/
class RouteGuard extends AbstractGuard
{
const PRIORITY = 100;
/**
* @var RoleServiceInterface
*/
protected $roleService;
/**
* RouteGuard constructor.
* @param array $options
*/
public function __construct(array $options = null)
{
$options = $options ?? [];
parent::__construct($options);
if (isset($options['role_service']) && $options['role_service'] instanceof RoleServiceInterface) {
$this->setRoleService($options['role_service']);
}
if (!$this->roleService instanceof RoleServiceInterface) {
throw new RuntimeException('RoleService 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);
$roles = [];
} else {
$routeName = strtolower($key);
$roles = (array)$value;
}
$this->rules[$routeName] = $roles;
}
}
/**
* @return RoleServiceInterface
*/
public function getRoleService(): RoleServiceInterface
{
return $this->roleService;
}
/**
* @param RoleServiceInterface $roleService
*/
public function setRoleService(RoleServiceInterface $roleService)
{
$this->roleService = $roleService;
}
/**
* @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());
$allowedRoles = null;
foreach (array_keys($this->rules) as $routeName) {
if ($routeName === $matchedRouteName) {
$allowedRoles = $this->rules[$routeName];
break;
}
}
if (null === $allowedRoles) {
return $this->protectionPolicy === self::POLICY_ALLOW;
}
if (in_array('*', $allowedRoles)) {
return true;
}
return $this->roleService->matchIdentityRoles($allowedRoles);
}
}