Passed
Push — master ( 8587fa...100bd0 )
by Gabor
04:15
created

AbstractServiceAdapter::isResourceAllowed()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
nop 2
crap 4
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Acl\ServiceAdapter;
15
16
use WebHemi\Acl;
17
use WebHemi\Data\Coupler\UserGroupToPolicyCoupler;
18
use WebHemi\Data\Coupler\UserToGroupCoupler;
19
use WebHemi\Data\Coupler\UserToPolicyCoupler;
20
use WebHemi\Data\Entity\AccessManagement\PolicyEntity;
21
use WebHemi\Data\Entity\AccessManagement\ResourceEntity;
22
use WebHemi\Data\Entity\ApplicationEntity;
23
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
24
25
/**
26
 * Class AbstractServiceAdapter
27
 */
28
abstract class AbstractServiceAdapter implements Acl\ServiceInterface
29
{
30
    /** @var EnvironmentInterface */
31
    protected $environment;
32
    /** @var UserToPolicyCoupler */
33
    protected $userToPolicyCoupler;
34
    /** @var UserToGroupCoupler */
35
    protected $userToGroupCoupler;
36
    /** @var UserGroupToPolicyCoupler */
37
    protected $userGroupToPolicyCoupler;
38
39
    /**
40
     * ServiceAdapter constructor.
41
     *
42
     * @param EnvironmentInterface     $environment
43
     * @param UserToPolicyCoupler      $userToPolicyCoupler
44
     * @param UserToGroupCoupler       $userToGroupCoupler
45
     * @param UserGroupToPolicyCoupler $userGroupToPolicyCoupler
46
     */
47 2
    public function __construct(
48
        EnvironmentInterface $environment,
49
        UserToPolicyCoupler $userToPolicyCoupler,
50
        UserToGroupCoupler $userToGroupCoupler,
51
        UserGroupToPolicyCoupler $userGroupToPolicyCoupler
52
    ) {
53 2
        $this->environment = $environment;
54 2
        $this->userToPolicyCoupler = $userToPolicyCoupler;
55 2
        $this->userToGroupCoupler = $userToGroupCoupler;
56 2
        $this->userGroupToPolicyCoupler = $userGroupToPolicyCoupler;
57 2
    }
58
59
    /**
60
     * Checks a given policy against a resource, application and method.
61
     *
62
     * The user has access when the user or the user's group has a policy which:
63
     *  - is connected to the current resource OR any resource AND
64
     *  - is connected to the current application OR any application AND
65
     *  - allows the current request method.
66
     *
67
     * @param PolicyEntity           $policyEntity
68
     * @param null|ResourceEntity    $resourceEntity
69
     * @param null|ApplicationEntity $applicationEntity
70
     * @return bool
71
     */
72 1
    protected function isPolicyAllowed(
73
        PolicyEntity $policyEntity,
74
        ? ResourceEntity $resourceEntity = null,
75
        ? ApplicationEntity $applicationEntity = null
76
    ) : bool {
77 1
        return $this->isResourceAllowed($policyEntity, $resourceEntity)
78 1
            && $this->isApplicationAllowed($policyEntity, $applicationEntity)
79 1
            && $this->isRequestMethodAllowed($policyEntity);
80
    }
81
82
    /**
83
     * Checks whether the given resource is allowed for the given policy.
84
     *
85
     * @param PolicyEntity        $policyEntity
86
     * @param ResourceEntity|null $resourceEntity
87
     * @return bool
88
     */
89 1
    private function isResourceAllowed(
90
        PolicyEntity $policyEntity,
91
        ? ResourceEntity $resourceEntity = null
92
    ) : bool {
93 1
        $policyResourceId = $policyEntity->getResourceId();
94 1
        $resourceId = $resourceEntity ? $resourceEntity->getResourceId() : null;
95 1
        $allowResurce = is_null($policyResourceId) || $policyResourceId === $resourceId;
96
97 1
        return $allowResurce ? $policyEntity->getAllowed() : false;
98
    }
99
100
    /**
101
     * Checks whether the given application is allowed for the given policy.
102
     *
103
     * @param PolicyEntity                $policyEntity
104
     * @param null|ApplicationEntity|null $applicationEntity
105
     * @return bool
106
     */
107 1
    private function isApplicationAllowed(
108
        PolicyEntity $policyEntity,
109
        ? ApplicationEntity $applicationEntity = null
110
    ) : bool {
111 1
        $policyApplicationId = $policyEntity->getApplicationId();
112 1
        $applicationId = $applicationEntity ? $applicationEntity->getApplicationId() : null;
113 1
        $allowApplication = is_null($policyApplicationId) || $policyApplicationId === $applicationId;
114
115 1
        return $allowApplication ? $policyEntity->getAllowed() : false;
116
    }
117
118
    /**
119
     * Checks whether the request method is allowed for the given policy.
120
     *
121
     * @param PolicyEntity $policyEntity
122
     * @return bool
123
     */
124 1
    private function isRequestMethodAllowed(PolicyEntity $policyEntity) : bool
125
    {
126 1
        $policyRequestMethod = $policyEntity->getMethod();
127 1
        $requestMethod = $this->environment->getRequestMethod();
128 1
        $allowRequestMethod = is_null($policyRequestMethod) || $policyRequestMethod === $requestMethod;
129
130 1
        return $allowRequestMethod ? $policyEntity->getAllowed() : false;
131
    }
132
}
133