CommanderAuthorizer.php 1.12 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php


namespace Source\UseCases\Door\Access\Authorizers;


use Carbon\Carbon;
use Source\Entities\User;
use Source\Authorization\Permissions;
use Source\Authorization\ApiAuthorizer;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Door\Access\AccessAuthorizer;

class CommanderAuthorizer implements AccessAuthorizer
{
    /**
     * @var \Source\Authorization\ApiAuthorizer
     */
    protected ApiAuthorizer $authorizer;

    public function __construct(ApiAuthorizer $authorizer)
    {
        $this->authorizer = $authorizer;
    }

    /**
     * @inheritDoc
     */
    public function check(?User $user, Carbon $date, string $doorId, string $doorcode, ?string $commandString): int
    {
        if ($user) {
            $this->authorizer->setCurrentUserId($user->getId());

            try {
                if ($this->authorizer->allowsOne([Permissions::DOOR_COMMANDER, Permissions::MANAGE_DOORS])) {
                    return self::ALLOW;
                }
            } catch (EntityNotFoundException $e) {
                // Keep calm and continue on
            }
        }

        return self::CONTINUE;
    }
}