Completed
Push — master ( c96dfb...142578 )
by Igor
12:57 queued 10:25
created

RbacController::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\base\InvalidConfigException;
7
use yii\console\Controller;
8
use yii\helpers\Console;
9
10
/**
11
 * Command for create permissions
12
 * @see config/permissions.php
13
 */
14
class RbacController extends Controller
15
{
16
    /**
17
     * @var authManager
18
     */
19
    private $auth;
20
    /**
21
     * @var string
22
     */
23
    public $path;
24
25
    /**
26
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
27
     */
28 1
    public function options($actionId = '')
29
    {
30 1
        return ['path'];
31
    }
32
33 3
    public function beforeAction($action)
34
    {
35 3
        if (parent::beforeAction($action)) {
36 3
            if (empty($this->path)) {
37 1
                throw new InvalidConfigException('`path` should be specified');
38
            }
39 2
        }
40 2
        return true;
41
    }
42
43 4
    public function init()
44
    {
45 4
        $this->auth = Yii::$app->authManager;
46 4
        Yii::$app->cache->delete('rbac-permissions');
47 4
    }
48
49 2
    public function actionInit()
50
    {
51 2
        $currentPermissions = $this->auth->getPermissions();
52 2
        $newPermissions = require Yii::getAlias($this->path);
53
54 2
        $this->cleanUnusedPermissions($currentPermissions, $newPermissions);
55 2
        $this->updatePermissions($currentPermissions, $newPermissions);
56
57 2
        $this->stdout("Done!\n", Console::FG_GREEN);
58 2
    }
59
60 2
    private function cleanUnusedPermissions($currentPermissions, $newPermissions)
61
    {
62 2
        foreach ($currentPermissions as $currentPermission) {
63 2
            if (!isset($newPermissions[$currentPermission->name])) {
64 1
                $this->auth->remove($currentPermission);
65 1
            }
66 2
        }
67 2
    }
68
69 2
    private function updatePermissions($currentPermissions, $newPermissions)
70
    {
71 2
        foreach ($newPermissions as $name => $description) {
72 2
            $isNew = !isset($currentPermissions[$name]);
73 2
            if ($isNew) {
74 1
                $this->add($name, $description);
75 1
                continue;
76
            }
77 2
            $this->update($name, $description);
78 2
        }
79 2
    }
80
81 1
    private function add($name, $description)
82
    {
83 1
        $permission = $this->auth->createPermission($name);
84 1
        $permission->description = $description;
85 1
        $this->auth->add($permission);
86 1
        $this->stdout("Added: $name\n", Console::FG_GREEN);
87 1
    }
88
89 2
    private function update($name, $description)
90
    {
91 2
        $permission = $this->auth->getPermission($name);
92 2
        $permission->description = $description;
93 2
        $this->auth->update($name, $permission);
94 2
        $this->stdout("Updated: $name\n", Console::FG_GREEN);
95 2
    }
96
}
97