Completed
Push — master ( 243dc1...429fcc )
by Dmitry
14:10
created

Endpoint/Middleware/CheckPermissionsMiddleware.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace hiapi\Core\Endpoint\Middleware;
4
5
use hiapi\Core\Endpoint\Endpoint;
6
use hiapi\exceptions\InsufficientPermissionsException;
7
use yii\web\User;
8
use League\Tactician\Middleware;
9
10
/**
11
 * Class CheckPermissionsMiddleware
12
 *
13
 * @author Dmytro Naumenko <[email protected]>
14
 */
15
class CheckPermissionsMiddleware implements Middleware
16
{
17
    private User $user;
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
18
    private Endpoint $endpoint;
19
20
    public function __construct(Endpoint $endpoint, User $user)
21
    {
22
        $this->endpoint = $endpoint;
23
        $this->user = $user;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function execute($command, callable $next)
30
    {
31
        foreach ($this->endpoint->permissions as $permission) {
32
            if (! $this->user->can($permission)) {
33
                throw new InsufficientPermissionsException($permission);
34
            }
35
        }
36
37
        return $next($command);
38
    }
39
}
40