Passed
Push — main ( 1e821c...d0f83f )
by Roberto
02:00
created

RBACMiddleware::checkPermissions()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 19
rs 9.5222
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){
0 ignored issues
show
Bug introduced by
The type Lepton\Middleware\PermissionRequired was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
            $user = (new UserAuthenticator)->getLoggedUser();
0 ignored issues
show
Unused Code introduced by
The assignment to $user is dead and can be removed.
Loading history...
54
            die(print_r($params));
0 ignored issues
show
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...
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...
55
        }
56
        return true;
57
    }
58
}
59