CommanderAuthorizer.php 1.29 KB
Newer Older
1
2
3
4
5
6
<?php


namespace Source\UseCases\Door\Access\Authorizers;

use Carbon\Carbon;
7
use Source\Entities\Door;
8
use Source\Entities\User;
9
use Source\Authorization\Authorizer;
10
11
12
13
14
15
16
use Source\Authorization\Permissions;
use Source\Exceptions\EntityNotFoundException;
use Source\UseCases\Door\Access\AccessAuthorizer;

class CommanderAuthorizer implements AccessAuthorizer
{
    /**
17
     * @var \Source\Authorization\Authorizer
18
     */
19
    protected Authorizer $authorizer;
20

21
    public function __construct(Authorizer $authorizer)
22
23
24
25
26
27
28
    {
        $this->authorizer = $authorizer;
    }

    /**
     * @inheritDoc
     */
29
    public function check(?User $user, Carbon $date, Door $door, string $doorcode, ?string $commandString): int
30
31
32
33
34
    {
        if ($user) {
            $this->authorizer->setCurrentUserId($user->getId());

            try {
Jacob Priddy's avatar
Jacob Priddy committed
35
                if ($this->authorizer->allows(Permissions::DOOR_COMMANDER)) {
36
37
38
39
40
41
42
43
44
                    return self::ALLOW;
                }
            } catch (EntityNotFoundException $e) {
                // Keep calm and continue on
            }
        }

        return self::CONTINUE;
    }
Jacob Priddy's avatar
Jacob Priddy committed
45
46
47
48
49
50
51
52
53

    /**
     * @inheritDoc
     */
    public function getReason(): string
    {
        // We only allow, we don't deny. Can be only one reason.
        return 'User is a door commander.';
    }
54
}