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

ServiceAdapter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 65
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserPolicies() 0 3 1
A getUserGroupPolicies() 0 14 3
A isAllowed() 0 21 3
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\Base;
15
16
use WebHemi\Acl\ServiceAdapter\AbstractServiceAdapter;
17
use WebHemi\Data\Entity\EntitySet;
18
use WebHemi\Data\Entity\PolicyEntity;
19
use WebHemi\Data\Entity\ResourceEntity;
20
use WebHemi\Data\Entity\ApplicationEntity;
21
use WebHemi\Data\Entity\UserEntity;
22
use WebHemi\Data\Entity\UserGroupEntity;
23
24
/**
25
 * Class ServiceAdapter.
26
 */
27
class ServiceAdapter extends AbstractServiceAdapter
28
{
29
    /**
30
     * Checks if a User can access to a Resource in an Application
31
     *
32
     * @param  UserEntity             $userEntity
33
     * @param  ResourceEntity|null    $resourceEntity
34
     * @param  ApplicationEntity|null $applicationEntity
35
     * @param  string|null            $method
36
     * @return bool
37
     */
38 1
    public function isAllowed(
39
        UserEntity $userEntity,
40
        ? ResourceEntity $resourceEntity = null,
41
        ? ApplicationEntity $applicationEntity = null,
42
        ? string $method = null
43
    ) : bool {
44
        // By default we block everything.
45 1
        $allowed = false;
46
47 1
        $policies = $this->getUserPolicies($userEntity);
48 1
        $policies->merge($this->getUserGroupPolicies($userEntity));
49
50
        /** @var PolicyEntity $policyEntity */
51 1
        foreach ($policies as $policyEntity) {
52 1
            if ($this->isPolicyAllowed($policyEntity, $resourceEntity, $applicationEntity, $method)) {
53 1
                $allowed = true;
54 1
                break;
55
            }
56
        }
57
58 1
        return $allowed;
59
    }
60
61
    /**
62
     * Gets the policies assigned to the user.
63
     *
64
     * @param  UserEntity $userEntity
65
     * @return EntitySet
66
     */
67 1
    private function getUserPolicies(UserEntity $userEntity) : EntitySet
68
    {
69 1
        return $this->policyStorage->getPolicyListByUser((int) $userEntity->getUserId());
70
    }
71
72
    /**
73
     * Gets the policies assigned to the group in which the user is.
74
     *
75
     * @param  UserEntity $userEntity
76
     * @return EntitySet
77
     */
78 1
    private function getUserGroupPolicies(UserEntity $userEntity) : EntitySet
79
    {
80 1
        $userGroups = $this->userStorage->getUserGroupListByUser((int) $userEntity->getUserId());
81 1
        $groupPolicies = $this->userStorage->createEntitySet();
82
83
        /** @var UserGroupEntity $userGroupEntity */
84 1
        foreach ($userGroups as $userGroupEntity) {
85 1
            $policyList = $this->policyStorage->getPolicyListByUserGroup((int) $userGroupEntity->getUserGroupId());
86
87 1
            if (!empty($policyList)) {
88 1
                $groupPolicies->merge($policyList);
89
            }
90
        }
91 1
        return $groupPolicies;
92
    }
93
}
94