ai-sf /
lepton
| 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\Http\Response\HttpResponse; |
||
| 9 | use Lepton\Middleware\BaseAccessControlMiddleware; |
||
| 10 | use Lepton\Authenticator\AccessControlAttributes\PermissionRequired; |
||
| 11 | |||
| 12 | class RBACMiddleware extends BaseAccessControlMiddleware |
||
| 13 | { |
||
| 14 | private string $rbac_class; |
||
| 15 | private string $user_class; |
||
| 16 | |||
| 17 | protected function handle(mixed ...$middlewareParams): HttpResponse|Request |
||
| 18 | { |
||
| 19 | $this->rbac_class = $middlewareParams["rbac_class"] ?? throw new \Exception("You have to define a RBAC class"); |
||
| 20 | |||
| 21 | $rbac_interfaces = class_implements($this->rbac_class); |
||
| 22 | if(! in_array(\Lepton\Authenticator\RBAC\RBACInterface::class, $rbac_interfaces)) { |
||
| 23 | throw new \Exception("RBAC class has to implement \Lepton\Authenticator\RBAC\RBACInterface"); |
||
| 24 | } |
||
| 25 | |||
| 26 | $this->user_class = $middlewareParams["user_class"] ?? throw new \Exception("You have to define a User class"); |
||
| 27 | |||
| 28 | $user_interfaces = class_implements($this->user_class); |
||
| 29 | if(! in_array(\Lepton\Authenticator\RBAC\UserInterface::class, $user_interfaces)) { |
||
| 30 | throw new \Exception("User class has to implement \Lepton\Authenticator\RBAC\UserInterface"); |
||
| 31 | } |
||
| 32 | |||
| 33 | return parent::handle(...$middlewareParams); |
||
| 34 | } |
||
| 35 | |||
| 36 | |||
| 37 | protected function checkPermissions(string $modifier, mixed ...$params): bool |
||
| 38 | { |
||
| 39 | |||
| 40 | if($modifier == LoginRequired::class) { |
||
| 41 | |||
| 42 | $level = isset($params[0]) ? $params[0] : 1; |
||
| 43 | $authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
||
| 44 | $loggedIn = $authenticator->isLoggedIn(); |
||
| 45 | if(! $loggedIn) { |
||
| 46 | return false; |
||
| 47 | } |
||
| 48 | $user = $authenticator->getLoggedUser(); |
||
| 49 | $num_privileges = $user->privileges->and(livello__gte: $level)->count(); |
||
| 50 | return ($num_privileges > 0); |
||
| 51 | } elseif($modifier == PermissionRequired::class){ |
||
| 52 | $user = (new UserAuthenticator)->getLoggedUser(); |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 53 | die(print_r($params)); |
||
|
0 ignored issues
–
show
In this branch, the function will implicitly return
null which is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: interface ReturnsInt {
public function returnsIntHinted(): int;
}
class MyClass implements ReturnsInt {
public function returnsIntHinted(): int
{
if (foo()) {
return 123;
}
// here: null is implicitly returned
}
}
Loading history...
|
|||
| 54 | } |
||
| 55 | return true; |
||
| 56 | } |
||
| 57 | } |
||
| 58 |