|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.2 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2019 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
|
4 |
|
public function __construct( |
|
53
|
|
|
EnvironmentInterface $environment, |
|
54
|
|
|
UserStorage $userStorage, |
|
55
|
|
|
PolicyStorage $policyStorage |
|
56
|
|
|
) { |
|
57
|
4 |
|
$this->environment = $environment; |
|
58
|
4 |
|
$this->userStorage = $userStorage; |
|
59
|
4 |
|
$this->policyStorage = $policyStorage; |
|
60
|
4 |
|
} |
|
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
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
2 |
|
protected function isPolicyAllowed( |
|
90
|
|
|
PolicyEntity $policyEntity, |
|
91
|
|
|
? ResourceEntity $resourceEntity = null, |
|
92
|
|
|
? ApplicationEntity $applicationEntity = null |
|
93
|
|
|
) : bool { |
|
94
|
2 |
|
return $this->isResourceAllowed($policyEntity, $resourceEntity) |
|
95
|
2 |
|
&& $this->isApplicationAllowed($policyEntity, $applicationEntity); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Checks whether the given resource is allowed for the given policy. |
|
100
|
|
|
* |
|
101
|
|
|
* @param PolicyEntity $policyEntity |
|
102
|
|
|
* @param ResourceEntity|null $resourceEntity |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
2 |
|
private function isResourceAllowed( |
|
106
|
|
|
PolicyEntity $policyEntity, |
|
107
|
|
|
? ResourceEntity $resourceEntity = null |
|
108
|
|
|
) : bool { |
|
109
|
2 |
|
$policyResourceId = $policyEntity->getResourceId(); |
|
110
|
2 |
|
$resourceId = $resourceEntity ? $resourceEntity->getResourceId() : null; |
|
111
|
|
|
|
|
112
|
2 |
|
return $policyResourceId === null || $policyResourceId === $resourceId; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Checks whether the given application is allowed for the given policy. |
|
117
|
|
|
* |
|
118
|
|
|
* @param PolicyEntity $policyEntity |
|
119
|
|
|
* @param null|ApplicationEntity|null $applicationEntity |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
2 |
|
private function isApplicationAllowed( |
|
123
|
|
|
PolicyEntity $policyEntity, |
|
124
|
|
|
? ApplicationEntity $applicationEntity = null |
|
125
|
|
|
) : bool { |
|
126
|
2 |
|
$policyApplicationId = $policyEntity->getApplicationId(); |
|
127
|
2 |
|
$applicationId = $applicationEntity ? $applicationEntity->getApplicationId() : null; |
|
128
|
|
|
|
|
129
|
2 |
|
return $policyApplicationId === null || $policyApplicationId === $applicationId; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|