Completed
Push — master ( d69705...774ae2 )
by Dmitry
03:43
created

RbacController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
nc 1
cc 1
eloc 3
nop 4
crap 2
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-2017, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac\console;
12
13
use hipanel\rbac\RbacIniterInterface;
14
use yii\base\Module;
15
use Yii;
16
17
/**
18
 * Class RbacController.
19
 *
20
 * Usage: `hidev rbac/init`
21
 */
22
class RbacController extends \yii\console\Controller
23
{
24
    /**
25
     * @var RbacIniterInterface
26
     */
27
    protected $initer;
28
29
    public function __construct($id, Module $module, RbacIniterInterface $initer, $config = [])
30
    {
31
        parent::__construct($id, $module, $config);
32
33
        $this->initer = $initer;
34
    }
35
36
    public $defaultAction = 'show';
37
38
    public function actionInit()
39
    {
40
        $auth = Yii::$app->get('authManager');
41
        $this->initer->init($auth);
42
    }
43
44
    public function actionReinit()
45
    {
46
        $auth = Yii::$app->get('authManager');
47
        $this->initer->reinit($auth);
48
    }
49
50
    public function actionShow()
51
    {
52
        $auth = Yii::$app->get('authManager');
53
54
        echo "Permissions:\n";
55
        $permissions = $auth->getPermissions();
56
        ksort($permissions);
57
        foreach ($permissions as $name => $perm) {
58
            echo "   $perm->name $perm->description\n";
59
        }
60
61
        echo "Roles:\n";
62
        foreach ($auth->getRoles() as $name => $role) {
63
            $children = implode(',', array_keys($auth->getChildren($name)));
64
            printf("   %-12s %s\n", "$role->name:", $children);
65
        }
66
67
        echo "Assignments:\n";
68
        foreach ($auth->getAllAssignments() as $userId => $roles) {
69
            $roles = implode(',', array_keys($roles));
70
            printf("   %-12s %s\n", "$userId:", $roles);
71
        }
72
    }
73
}
74