|
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\Base; |
|
15
|
|
|
|
|
16
|
|
|
use WebHemi\Acl\ServiceAdapter\AbstractServiceAdapter; |
|
17
|
|
|
use WebHemi\Data\Entity\AccessManagement\PolicyEntity; |
|
18
|
|
|
use WebHemi\Data\Entity\AccessManagement\ResourceEntity; |
|
19
|
|
|
use WebHemi\Data\Entity\ApplicationEntity; |
|
20
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class ServiceAdapter. |
|
24
|
|
|
*/ |
|
25
|
|
|
class ServiceAdapter extends AbstractServiceAdapter |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Checks if a User can access to a Resource in an Application |
|
29
|
|
|
* |
|
30
|
|
|
* @param UserEntity $userEntity |
|
31
|
|
|
* @param ResourceEntity|null $resourceEntity |
|
32
|
|
|
* @param ApplicationEntity|null $applicationEntity |
|
33
|
|
|
* @return bool |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function isAllowed( |
|
36
|
|
|
UserEntity $userEntity, |
|
37
|
|
|
? ResourceEntity $resourceEntity = null, |
|
38
|
|
|
? ApplicationEntity $applicationEntity = null |
|
39
|
|
|
) : bool { |
|
40
|
|
|
// We assume the best case: the user has access |
|
41
|
1 |
|
$allowed = false; |
|
42
|
|
|
|
|
43
|
|
|
/** @var PolicyEntity[] $policies */ |
|
44
|
1 |
|
$policies = array_merge($this->getUserPolicies($userEntity), $this->getUserGroupPolicies($userEntity)); |
|
45
|
|
|
|
|
46
|
1 |
|
foreach ($policies as $policyEntity) { |
|
47
|
1 |
|
if ($this->isPolicyAllowed($policyEntity, $resourceEntity, $applicationEntity)) { |
|
48
|
1 |
|
$allowed = true; |
|
49
|
1 |
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return $allowed; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Gets the policies assigned to the user. |
|
58
|
|
|
* |
|
59
|
|
|
* @param UserEntity $userEntity |
|
60
|
|
|
* @return PolicyEntity[] |
|
61
|
|
|
*/ |
|
62
|
1 |
|
private function getUserPolicies(UserEntity $userEntity) : array |
|
63
|
|
|
{ |
|
64
|
|
|
/** @var PolicyEntity[] $userPolicies */ |
|
65
|
1 |
|
return $this->userToPolicyCoupler->getEntityDependencies($userEntity); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Gets the policies assigned to the group in which the user is. |
|
70
|
|
|
* |
|
71
|
|
|
* @param UserEntity $userEntity |
|
72
|
|
|
* @return PolicyEntity[] |
|
73
|
|
|
*/ |
|
74
|
1 |
|
private function getUserGroupPolicies(UserEntity $userEntity) : array |
|
75
|
|
|
{ |
|
76
|
|
|
/** @var PolicyEntity[] $userGroupPolicies */ |
|
77
|
1 |
|
$userGroupPolicies = []; |
|
78
|
|
|
/** @var array<UserGroupEntity> $userGroups */ |
|
79
|
1 |
|
$userGroups = $this->userToGroupCoupler->getEntityDependencies($userEntity); |
|
80
|
|
|
|
|
81
|
1 |
|
foreach ($userGroups as $userGroupEntity) { |
|
82
|
|
|
/** @var PolicyEntity[] $groupPolicies */ |
|
83
|
1 |
|
$groupPolicies = $this->userGroupToPolicyCoupler->getEntityDependencies($userGroupEntity); |
|
84
|
1 |
|
$userGroupPolicies = array_merge($userGroupPolicies, $groupPolicies); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
return $userGroupPolicies; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|