1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Lepton\Middleware; |
4
|
|
|
|
5
|
|
|
use Lepton\Authenticator\AccessControlAttributes\LoginRequired; |
6
|
|
|
use Lepton\Authenticator\UserAuthenticator; |
7
|
|
|
use Lepton\Http\Request; |
8
|
|
|
use Lepton\Routing\Match\{BaseMatch, MatchRoute}; |
9
|
|
|
use Lepton\Http\Response\{SuccessResponse, HttpResponse, RedirectResponse}; |
10
|
|
|
use Lepton\Middleware\BaseAccessControlMiddleware; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
|
13
|
|
|
class RBACMiddleware extends BaseAccessControlMiddleware |
14
|
|
|
{ |
15
|
|
|
private string $rbac_class; |
16
|
|
|
private string $user_class; |
17
|
|
|
|
18
|
|
|
protected function handle(mixed ...$middlewareParams): HttpResponse|Request |
19
|
|
|
{ |
20
|
|
|
$this->rbac_class = $middlewareParams["rbac_class"] ?? throw new \Exception("You have to define a RBAC class"); |
21
|
|
|
|
22
|
|
|
$rbac_interfaces = class_implements($this->rbac_class); |
23
|
|
|
if(! in_array(\Lepton\Authenticator\RBAC\RBACInterface::class, $rbac_interfaces)) { |
24
|
|
|
throw new \Exception("RBAC class has to implement \Lepton\Authenticator\RBAC\RBACInterface"); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->user_class = $middlewareParams["user_class"] ?? throw new \Exception("You have to define a User class"); |
28
|
|
|
|
29
|
|
|
$user_interfaces = class_implements($this->user_class); |
30
|
|
|
if(! in_array(\Lepton\Authenticator\RBAC\UserInterface::class, $user_interfaces)) { |
31
|
|
|
throw new \Exception("User class has to implement \Lepton\Authenticator\RBAC\UserInterface"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return parent::handle(...$middlewareParams); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
protected function checkPermissions(string $modifier, mixed ...$params): bool |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
if($modifier == LoginRequired::class) { |
42
|
|
|
|
43
|
|
|
$level = isset($params[0]) ? $params[0] : 1; |
44
|
|
|
$authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
45
|
|
|
$loggedIn = $authenticator->isLoggedIn(); |
46
|
|
|
if(! $loggedIn) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
$user = $authenticator->getLoggedUser(); |
50
|
|
|
$num_privileges = $user->privileges->and(livello__gte: $level)->count(); |
51
|
|
|
return ($num_privileges > 0); |
52
|
|
|
} elseif($modifier == PermissionRequired::class){ |
|
|
|
|
53
|
|
|
$user = (new UserAuthenticator)->getLoggedUser(); |
|
|
|
|
54
|
|
|
die(print_r($params)); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
return true; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths