AbstractIniter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 52
rs 10
ccs 16
cts 16
cp 1
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A reinit() 0 4 1
A init() 0 18 5
1
<?php
2
/**
3
 * RBAC implementation for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-rbac
6
 * @package   hipanel-rbac
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2016-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac;
12
13
/**
14
 * Initer for AuthManager.
15
 *
16
 * @author Andrii Vasyliev <[email protected]>
17
 */
18
abstract class AbstractIniter implements RbacIniterInterface
19
{
20
    /**
21
     * Provides a tree of permissions to be set in AuthManager.
22
     *
23
     * @return array where:
24
     * - key: role name
25
     * - value: role items
26
     */
27
    abstract public function getTree();
28
29
    /**
30
     * Provides a tree of permissions to be set in AuthManager.
31
     *
32
     * @return array where:
33
     * - key: permission or role name
34
     * - array: role additional data according to `\yii\rbac\Permission` properties
35
     */
36
    abstract public function getMetadata();
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 1
    public function init(AuthManager $auth)
42
    {
43 1
        $metadata = new SafeMetadata($this->getMetadata());
44
45 1
        foreach (array_keys($this->getTree()) as $roleName) {
46 1
            $auth->setRole($roleName, $metadata->getItemDescription($roleName));
47
        }
48
49 1
        foreach ($this->getTree() as $roleName => $items) {
50 1
            foreach ($items as $name) {
51 1
                $item = $auth->getItem($name);
52 1
                if ($item === null) {
53 1
                    $item = $auth->setPermission($name, $metadata->getItemDescription($name));
54
55 1
                    $denyName = "deny:$name";
56 1
                    $auth->setPermission($denyName, $metadata->getItemDescription($denyName));
57
                }
58 1
                $auth->setChild($roleName, $item);
59
            }
60
        }
61 1
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 1
    public function reinit(AuthManager $auth)
67
    {
68 1
        $auth->removeAll();
69 1
        $this->init($auth);
70 1
    }
71
}
72