Completed
Pull Request — master (#300)
by
unknown
13:49
created

Security   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 6
dl 0
loc 66
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A allowsUpdateWithCdbXml() 0 7 1
A isAuthorized() 0 6 1
A currentUiTIDUserCanEditOffer() 0 14 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\ReadModel\Permission\PermissionQueryInterface;
8
use CultuurNet\UDB3\Offer\Security\Permission\PermissionVoterInterface;
9
use CultuurNet\UDB3\Role\ValueObjects\Permission;
10
use CultuurNet\UDB3\Security\SecurityInterface;
11
use CultuurNet\UDB3\Security\UserIdentificationInterface;
12
use ValueObjects\StringLiteral\StringLiteral;
13
14
class Security implements SecurityInterface
15
{
16
    /**
17
     * @var UserIdentificationInterface
18
     */
19
    private $userIdentification;
20
21
    /**
22
     * @var PermissionVoterInterface
23
     */
24
    private $permissionVoter;
25
26
    /**
27
     * Security constructor.
28
     * @param UserIdentificationInterface $userIdentification
29
     * @param PermissionVoterInterface $permissionVoter
30
     */
31
    public function __construct(
32
        UserIdentificationInterface $userIdentification,
33
        PermissionVoterInterface $permissionVoter
34
    ) {
35
        $this->userIdentification = $userIdentification;
36
        $this->permissionVoter = $permissionVoter;
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function allowsUpdateWithCdbXml(StringLiteral $offerId)
43
    {
44
        return $this->currentUiTIDUserCanEditOffer(
45
            $offerId,
46
            new PreflightCommand($offerId->toNative(), Permission::AANBOD_BEWERKEN())
47
        );
48
    }
49
50
    /**
51
     * @inheritdoc
52
     */
53
    public function isAuthorized(AuthorizableCommandInterface $command)
54
    {
55
        $offerId = new StringLiteral($command->getItemId());
56
57
        return $this->currentUiTIDUserCanEditOffer($offerId, $command);
58
    }
59
60
    /**
61
     * @param StringLiteral $offerId
62
     * @param AuthorizableCommandInterface $command
63
     * @return bool
64
     */
65
    private function currentUiTIDUserCanEditOffer(
66
        StringLiteral $offerId,
67
        AuthorizableCommandInterface $command
68
    ) {
69
        if (!$this->userIdentification->getId()) {
70
            return false;
71
        }
72
73
        return $this->permissionVoter->isAllowed(
74
            $command->getPermission(),
75
            $offerId,
76
            $this->userIdentification->getId()
77
        );
78
    }
79
}
80