BuildRbacController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 29
nc 1
nop 0
dl 0
loc 43
c 1
b 0
f 0
cc 1
rs 9.456
1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\console\Controller;
7
use yii\console\ExitCode;
8
use yii\rbac\BaseManager;
9
use yii\rbac\Item;
10
11
/**
12
 * Class BuildRbacController
13
 *
14
 * @package app\commands
15
 */
16
class BuildRbacController extends Controller
17
{
18
    /**
19
     * @var BaseManager
20
     */
21
    private $authManager;
22
23
    public function init()
24
    {
25
        $this->authManager = Yii::$app->get('authManager');
0 ignored issues
show
Documentation Bug introduced by
It seems like Yii::app->get('authManager') can also be of type mixed. However, the property $authManager is declared as type yii\rbac\BaseManager. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
26
27
        parent::init();
28
    }
29
30
    /**
31
     * @return int
32
     */
33
    public function actionIndex()
34
    {
35
        /* CREATE ROLES */
36
        $this->tryCreateRole('admin', 'Admin role');
37
        $this->tryCreateRole('manager', 'Manager');
38
        $this->tryCreateRole('user', 'Simple user');
39
40
        /* CREATE PERMISSIONS */
41
        $this->tryCreatePermission('CREATE', 'Create entries');
42
        $this->tryCreatePermission('UPDATE', 'Update entries');
43
        $this->tryCreatePermission('DELETE', 'Delete entries');
44
        $this->tryCreatePermission('SET_ROLES', 'Set roles for users');
45
        $this->tryCreatePermission('VIEW_BACKSIDE', 'View back side content');
46
        $this->tryCreatePermission('VIEW_FRONTSIDE', 'View front side content');
47
48
        /* DEFINE PERMISSIONS TO ROLES */
49
        $item_role_admin = $this->authManager->getRole('admin');
50
        $item_role_manager = $this->authManager->getRole('manager');
51
        $item_role_user = $this->authManager->getRole('user');
52
53
        $item_permission_create = $this->authManager->getPermission('CREATE');
54
        $item_permission_update = $this->authManager->getPermission('UPDATE');
55
        $item_permission_delete = $this->authManager->getPermission('DELETE');
56
        $item_permission_set_roles = $this->authManager->getPermission('SET_ROLES');
57
        $item_permission_view_backside = $this->authManager->getPermission('VIEW_BACKSIDE');
58
        $item_permission_view_frontside = $this->authManager->getPermission('VIEW_FRONTSIDE');
59
60
        $this->tryAddChild($item_role_admin, $item_permission_create);
61
        $this->tryAddChild($item_role_admin, $item_permission_update);
62
        $this->tryAddChild($item_role_admin, $item_permission_delete);
63
        $this->tryAddChild($item_role_admin, $item_permission_set_roles);
64
        $this->tryAddChild($item_role_admin, $item_permission_view_backside);
65
        $this->tryAddChild($item_role_admin, $item_permission_view_frontside);
66
67
68
        $this->tryAddChild($item_role_manager, $item_permission_view_backside);
69
        $this->tryAddChild($item_role_manager, $item_permission_view_frontside);
70
71
        $this->tryAddChild($item_role_user, $item_permission_view_frontside);
72
73
        echo 'Done' . "\n";
74
75
        return ExitCode::OK;
76
    }
77
78
    /**
79
     * @param string $name
80
     * @param string $description
81
     */
82
    private function tryCreateRole(string $name, string $description): void
83
    {
84
        if (empty($this->authManager->getRole($name))) {
85
            $item_role = $this->authManager->createRole($name);
86
            $item_role->description = $description;
87
            $this->authManager->add($item_role);
88
        }
89
    }
90
91
    /**
92
     * @param string $name
93
     * @param string $description
94
     */
95
    private function tryCreatePermission(string $name, string $description): void
96
    {
97
        if (empty($this->authManager->getPermission($name))) {
98
            $item_permission = $this->authManager->createPermission($name);
99
            $item_permission->description = $description;
100
            $this->authManager->add($item_permission);
101
        }
102
    }
103
104
    /**
105
     * @param Item|null $item_role
106
     * @param Item|null $item_permission
107
     */
108
    private function tryAddChild(Item $item_role = null, Item $item_permission = null): void
109
    {
110
        if ($item_role == null || $item_permission == null) {
111
            return;
112
        }
113
114
        if (!$this->authManager->hasChild($item_role, $item_permission)) {
115
            $this->authManager->addChild($item_role, $item_permission);
116
        }
117
    }
118
}
119