RBACMiddleware   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 17 3
A checkPermissions() 0 19 5
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
The assignment to $user is dead and can be removed.
Loading history...
53
            die(print_r($params));
0 ignored issues
show
Bug Best Practice introduced by
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...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
54
        }
55
        return true;
56
    }
57
}
58