Completed
Pull Request — master (#27)
by Dmitry
04:13 queued 13s
created

RbacController::actionGenerateDescriptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 9
ccs 0
cts 5
cp 0
crap 2
rs 10
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-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\rbac\console;
12
13
use hipanel\rbac\AbstractIniter;
14
use hipanel\rbac\AuthManager;
15
use hipanel\rbac\RbacIniterInterface;
16
use hiqdev\yii\compat\yii;
17
use yii\base\Module;
18
use yii\helpers\Console;
19
20
/**
21
 * Class RbacController.
22
 *
23
 * Usage: `hidev rbac/init`
24
 */
25
class RbacController extends \yii\console\Controller
26
{
27
    /**
28
     * @var RbacIniterInterface|AbstractIniter
29
     */
30
    protected $initer;
31
32
    public function __construct($id, Module $module, RbacIniterInterface $initer, $config = [])
33
    {
34
        parent::__construct($id, $module, $config);
35
36
        $this->initer = $initer;
37
    }
38
39
    public $defaultAction = 'show';
40
41
    public function actionInit()
42
    {
43
        $auth = yii::getApp()->get('authManager');
44
        $this->initer->init($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\AbstractIniter::init() does only seem to accept hipanel\rbac\AuthManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $this->initer->init(/** @scrutinizer ignore-type */ $auth);
Loading history...
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\RbacIniterInterface::init() does only seem to accept hipanel\rbac\AuthManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        $this->initer->init(/** @scrutinizer ignore-type */ $auth);
Loading history...
45
        $this->stdout('Consider running rbac/generate-descriptions to generate default role descriptions', Console::FG_YELLOW);
46
    }
47
48
    public function actionReinit()
49
    {
50
        $auth = yii::getApp()->get('authManager');
51
        $this->initer->reinit($auth);
0 ignored issues
show
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\AbstractIniter::reinit() does only seem to accept hipanel\rbac\AuthManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->initer->reinit(/** @scrutinizer ignore-type */ $auth);
Loading history...
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $auth of hipanel\rbac\RbacIniterInterface::reinit() does only seem to accept hipanel\rbac\AuthManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->initer->reinit(/** @scrutinizer ignore-type */ $auth);
Loading history...
52
53
        $path = dirname(__DIR__) . '/files/source/metadata.php';
54
        (new MetadataGenerator($auth, $this->initer))->dump($path);
0 ignored issues
show
Bug introduced by
It seems like $auth can also be of type mixed and null; however, parameter $authManager of hipanel\rbac\console\Met...enerator::__construct() does only seem to accept hipanel\rbac\AuthManager, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

54
        (new MetadataGenerator(/** @scrutinizer ignore-type */ $auth, $this->initer))->dump($path);
Loading history...
55
56
        $this->initer->reinit($auth); // Reinit using newly generated descriptions
57
    }
58
59
    public function actionShow()
60
    {
61
        $auth = yii::getApp()->get('authManager');
62
63
        echo "Permissions:\n";
64
        $permissions = $auth->getPermissions();
65
        ksort($permissions);
66
        foreach ($permissions as $name => $perm) {
67
            echo "   $perm->name – $perm->description\n";
68
        }
69
70
        echo "Roles:\n";
71
        foreach ($auth->getRoles() as $name => $role) {
72
            $children = implode(', ', array_keys($auth->getChildren($name)));
73
            printf("   %-12s\n        %s\n\n", "$role->name – $role->description", $children);
74
        }
75
76
        echo "Assignments:\n";
77
        foreach ($auth->getAllAssignments() as $userId => $roles) {
78
            $roles = implode(', ', array_keys($roles));
79
            printf("   %-12s %s\n", "$userId:", $roles);
80
        }
81
    }
82
83
    public function actionGenerateDescriptions()
84
    {
85
        /** @var AuthManager $auth */
86
        $auth = yii::getApp()->get('authManager');
87
88
        $path = dirname(__DIR__) . '/files/source/metadata.php';
89
        (new MetadataGenerator($auth, $this->initer))->dump($path);
90
91
        $this->initer->reinit($auth);
92
    }
93
94
    public function actionPlantuml()
95
    {
96
        $path = dirname(__DIR__, 2) . '/docs/test.txt';
97
98
        $auth = yii::getApp()->get('authManager');
99
        $plant = new PlantUML($auth);
100
101
        $uml = $plant->build();
102
103
        file_put_contents($path, $uml);
104
    }
105
}
106