Passed
Push — master ( a24d5d...5d2fe9 )
by Julien
05:02
created

Acl   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 90.32%

Importance

Changes 0
Metric Value
wmc 13
eloc 32
c 0
b 0
f 0
dl 0
loc 66
ccs 28
cts 31
cp 0.9032
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C get() 0 54 13
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Acl;
13
14
use Phalcon\Di\AbstractInjectionAware;
15
use Phalcon\Acl\Adapter\Memory;
16
use Phalcon\Acl\Component;
17
use Phalcon\Acl\Role;
18
use Zemit\Support\Options\Options;
19
20
/**
21
 * Class Acl
22
 *
23
 * This class represents an Access Control List (ACL) and is used 
24
 * to configure and manage access permissions for different components.
25
 */
26
class Acl extends AbstractInjectionAware
27
{
28
    use Options;
29
    
30
    /**
31
     * Return an ACL for the specified components name
32
     * @param array $componentsName
33
     * @param array|null $permissions
34
     * @param string $inherit
35
     * @return Memory
36
     * @todo cache the ACL
37
     */
38 1
    public function get(array $componentsName = ['components'], ?array $permissions = null, string $inherit = 'inherit'): Memory
39
    {
40 1
        $acl = new Memory();
41 1
        $aclRoleList = [];
42
        
43 1
        $permissions = $this->getOption('permissions', []);
44 1
        $featureList = $permissions['features'] ?? [];
45 1
        $roleList = $permissions['roles'] ?? [];
46
        
47 1
        foreach ($roleList as $role => $rolePermission) {
48
            
49 1
            $role = $role === '*' ? 'everyone' : $role;
50 1
            $aclRole = new Role($role);
51 1
            $aclRoleList[$role] = $aclRole;
52 1
            $acl->addRole($aclRole);
53
            
54 1
            if (isset($rolePermission['features'])) {
55 1
                foreach ($rolePermission['features'] as $feature) {
56 1
                    $rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []);
57
                    // @todo remove duplicates
58
                }
59
            }
60
            
61 1
            foreach ($componentsName as $componentName) {
62 1
                $components = $rolePermission[$componentName] ?? [];
63 1
                $components = is_array($components) ? $components : [$components];
64
                
65 1
                foreach ($components as $component => $accessList) {
66 1
                    if (empty($component)) {
67
                        $component = $accessList;
68
                        $accessList = '*';
69
                    }
70
                    
71 1
                    if ($component !== '*') {
72 1
                        $aclComponent = new Component($component);
73 1
                        $acl->addComponent($aclComponent, $accessList);
74 1
                        $acl->allow($aclRole, $aclComponent, $accessList);
75
                    }
76
                }
77
            }
78
        }
79
        
80
        /**
81
         * Add inheritance (role extends)
82
         */
83 1
        foreach ($aclRoleList as $role => $aclRole) {
84 1
            $inheritList = $permissions[$role][$inherit] ?? [];
85 1
            $inheritList = is_array($inheritList) ? $inheritList : [$inheritList];
86 1
            foreach ($inheritList as $inheritRole) {
87
                $acl->addInherit($aclRole, $aclRoleList[$inheritRole]);
88
            }
89
        }
90
        
91 1
        return $acl;
92
    }
93
}
94