Completed
Push — master ( d1eae8...f6867f )
by Luc
14s
created

Security::currentUiTIDUserCanEditOffer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\Security;
4
5
use CultuurNet\UDB3\Offer\Commands\AuthorizableCommandInterface;
6
use CultuurNet\UDB3\Offer\Commands\PreflightCommand;
7
use CultuurNet\UDB3\Offer\Security\Permission\PermissionVoterInterface;
8
use CultuurNet\UDB3\Role\ValueObjects\Permission;
9
use CultuurNet\UDB3\Security\SecurityInterface;
10
use CultuurNet\UDB3\Security\UserIdentificationInterface;
11
use ValueObjects\StringLiteral\StringLiteral;
12
13
class Security implements SecurityInterface
14
{
15
    /**
16
     * @var UserIdentificationInterface
17
     */
18
    private $userIdentification;
19
20
    /**
21
     * @var PermissionVoterInterface
22
     */
23
    private $permissionVoter;
24
25
    /**
26
     * Security constructor.
27
     * @param UserIdentificationInterface $userIdentification
28
     * @param PermissionVoterInterface $permissionVoter
29
     */
30
    public function __construct(
31
        UserIdentificationInterface $userIdentification,
32
        PermissionVoterInterface $permissionVoter
33
    ) {
34
        $this->userIdentification = $userIdentification;
35
        $this->permissionVoter = $permissionVoter;
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41
    public function allowsUpdateWithCdbXml(StringLiteral $offerId)
42
    {
43
        return $this->currentUiTIDUserCanEditOffer(
44
            $offerId,
45
            new PreflightCommand($offerId->toNative(), Permission::AANBOD_BEWERKEN())
46
        );
47
    }
48
49
    /**
50
     * @inheritdoc
51
     */
52
    public function isAuthorized(AuthorizableCommandInterface $command)
53
    {
54
        $offerId = new StringLiteral($command->getItemId());
55
56
        return $this->currentUiTIDUserCanEditOffer($offerId, $command);
57
    }
58
59
    /**
60
     * @param StringLiteral $offerId
61
     * @param AuthorizableCommandInterface $command
62
     * @return bool
63
     */
64
    private function currentUiTIDUserCanEditOffer(
65
        StringLiteral $offerId,
66
        AuthorizableCommandInterface $command
67
    ) {
68
        if (!$this->userIdentification->getId()) {
69
            return false;
70
        }
71
72
        return $this->permissionVoter->isAllowed(
73
            $command->getPermission(),
74
            $offerId,
75
            $this->userIdentification->getId()
76
        );
77
    }
78
}
79