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

ServiceAdapter::checkPolicy()   B

Complexity

Conditions 7
Paths 32

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 14
nc 32
nop 3
crap 7
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