1 | <?php |
||||
2 | |||||
3 | namespace Lepton\Middleware; |
||||
4 | |||||
5 | use Lepton\Authenticator\AccessControlAttributes\{LoginRequired, AbstractAccessControlAttribute}; |
||||
6 | use Lepton\Core\Application; |
||||
7 | use Lepton\Http\Request; |
||||
8 | use Lepton\Routing\Match\MatchRoute; |
||||
9 | use Lepton\Http\Response\{HttpResponse, RedirectResponse}; |
||||
10 | |||||
11 | |||||
12 | class BaseAccessControlMiddleware extends AbstractMiddleware |
||||
13 | { |
||||
14 | |||||
15 | protected function handle(mixed ...$middlewareParams): HttpResponse|Request |
||||
16 | { |
||||
17 | if($this->match instanceof MatchRoute) { |
||||
18 | $reflection = new \ReflectionMethod($this->match->controller, $this->match->method); |
||||
19 | $attributes = $reflection->getAttributes(); |
||||
20 | |||||
21 | foreach ($attributes as $attribute) { |
||||
22 | if(is_subclass_of($attribute->getName(), AbstractAccessControlAttribute::class)) { |
||||
23 | return |
||||
24 | $this->checkPermissions($attribute->getName(), ...($attribute->getArguments()))? |
||||
25 | $this->request : |
||||
26 | new RedirectResponse( |
||||
27 | Application::getAuthConfig()->login_url, |
||||
28 | redirect_after: $this->request->url |
||||
29 | ); |
||||
30 | } |
||||
31 | |||||
32 | } |
||||
33 | return $this->request; |
||||
34 | } |
||||
0 ignored issues
–
show
|
|||||
35 | } |
||||
36 | |||||
37 | |||||
38 | protected function checkPermissions(string $modifier, mixed ...$params):bool{ |
||||
0 ignored issues
–
show
The parameter
$params is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
39 | if($modifier == LoginRequired::class){ |
||||
40 | $authenticator = new \Lepton\Authenticator\UserAuthenticator(); |
||||
41 | return $authenticator->isLoggedIn(); |
||||
42 | } |
||||
43 | return true; |
||||
44 | } |
||||
45 | |||||
46 | } |
||||
47 |
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: