Passed
Push — master ( 7ed858...861abc )
by Gabor
02:38
created

AbstractServiceAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2018 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\Entity\PolicyEntity;
18
use WebHemi\Data\Entity\ResourceEntity;
19
use WebHemi\Data\Entity\ApplicationEntity;
20
use WebHemi\Data\Entity\UserEntity;
21
use WebHemi\Data\Storage\PolicyStorage;
22
use WebHemi\Data\Storage\UserStorage;
23
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
24
25
/**
26
 * Class AbstractServiceAdapter.
27
 */
28
abstract class AbstractServiceAdapter implements Acl\ServiceInterface
29
{
30
    /**
31
     * @var EnvironmentInterface
32
     */
33
    protected $environment;
34
35
    /**
36
     * @var UserStorage
37
     */
38
    protected $userStorage;
39
40
    /**
41
     * @var PolicyStorage
42
     */
43
    protected $policyStorage;
44
45
    /**
46
     * ServiceAdapter constructor.
47
     *
48
     * @param EnvironmentInterface  $environment
49
     * @param UserStorage           $userStorage
50
     * @param PolicyStorage         $policyStorage
51
     */
52 2
    public function __construct(
53
        EnvironmentInterface $environment,
54
        UserStorage $userStorage,
55
        PolicyStorage $policyStorage
56
    ) {
57 2
        $this->environment = $environment;
58 2
        $this->userStorage = $userStorage;
59 2
        $this->policyStorage = $policyStorage;
60 2
    }
61
62
    /**
63
     * Checks if a User can access to a Resource in an Application
64
     *
65
     * @param  UserEntity             $userEntity
66
     * @param  ResourceEntity|null    $resourceEntity
67
     * @param  ApplicationEntity|null $applicationEntity
68
     * @return bool
69
     */
70
    abstract public function isAllowed(
71
        UserEntity $userEntity,
72
        ? ResourceEntity $resourceEntity = null,
73
        ? ApplicationEntity $applicationEntity = null
74
    ) : bool;
75
76
    /**
77
     * Checks a given policy against a resource, application and method.
78
     *
79
     * The user has access when the user or the user's group has a policy which:
80
     *  - is connected to the current resource OR any resource AND
81
     *  - is connected to the current application OR any application AND
82
     *  - allows the current request method.
83
     *
84
     * @param  PolicyEntity           $policyEntity
85
     * @param  null|ResourceEntity    $resourceEntity
86
     * @param  null|ApplicationEntity $applicationEntity
87
     * @param  null|string            $method
88
     * @return bool
89
     */
90 1
    protected function isPolicyAllowed(
91
        PolicyEntity $policyEntity,
92
        ? ResourceEntity $resourceEntity = null,
93
        ? ApplicationEntity $applicationEntity = null,
94
        ? string $method = null
95
    ) : bool {
96 1
        return $this->isResourceAllowed($policyEntity, $resourceEntity)
97 1
            && $this->isApplicationAllowed($policyEntity, $applicationEntity)
98 1
            && $this->isRequestMethodAllowed($policyEntity, $method);
99
    }
100
101
    /**
102
     * Checks whether the given resource is allowed for the given policy.
103
     *
104
     * @param  PolicyEntity        $policyEntity
105
     * @param  ResourceEntity|null $resourceEntity
106
     * @return bool
107
     */
108 1
    private function isResourceAllowed(
109
        PolicyEntity $policyEntity,
110
        ? ResourceEntity $resourceEntity = null
111
    ) : bool {
112 1
        $policyResourceId = $policyEntity->getResourceId();
113 1
        $resourceId = $resourceEntity ? $resourceEntity->getResourceId() : null;
114
115 1
        return is_null($policyResourceId) || $policyResourceId === $resourceId;
116
    }
117
118
    /**
119
     * Checks whether the given application is allowed for the given policy.
120
     *
121
     * @param  PolicyEntity                $policyEntity
122
     * @param  null|ApplicationEntity|null $applicationEntity
123
     * @return bool
124
     */
125 1
    private function isApplicationAllowed(
126
        PolicyEntity $policyEntity,
127
        ? ApplicationEntity $applicationEntity = null
128
    ) : bool {
129 1
        $policyApplicationId = $policyEntity->getApplicationId();
130 1
        $applicationId = $applicationEntity ? $applicationEntity->getApplicationId() : null;
131
132 1
        return is_null($policyApplicationId) || $policyApplicationId === $applicationId;
133
    }
134
135
    /**
136
     * Checks whether the request method is allowed for the given policy.
137
     *
138
     * @param  PolicyEntity $policyEntity
139
     * @param  null|string  $method
140
     * @return bool
141
     */
142 1
    private function isRequestMethodAllowed(PolicyEntity $policyEntity, ? string $method = null) : bool
143
    {
144 1
        $policyRequestMethod = $policyEntity->getMethod();
145 1
        $requestMethod = $method ?? $this->environment->getRequestMethod();
146 1
        $allowRequestMethod = is_null($policyRequestMethod) || $policyRequestMethod === $requestMethod;
147
148 1
        return $allowRequestMethod;
149
    }
150
}
151