RbacController::setupPermissionsAndRoles()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 8.3709
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace app\commands;
4
5
use Yii;
6
use yii\console\Controller;
7
8
class RbacController extends Controller
9
{
10
    public function reset()
11
    {
12
        $auth = Yii::$app->authManager;
13
        $auth->removeAll();
14
    }
15
16
    public function actionInit()
17
    {
18
        $this->reset();
19
        $this->setupPermissionsAndRoles();
20
        $this->setupDefaultAssignments();
21
    }
22
23
    public function setupDefaultAssignments()
24
    {
25
        $auth = Yii::$app->authManager;
26
27
        $admin = $auth->getRole('Administrator');
28
        $auth->assign($admin, 'admin');
29
    }
30
31
    public function actionAddContentTypes()
32
    {
33
        $auth = Yii::$app->authManager;
34
35
        // Content Type
36
        $setContentTypes = $auth->createPermission('setContentTypes');
37
        $setContentTypes->description = 'Manage content types';
38
        $auth->add($setContentTypes);
39
40
        $admin = $auth->getRole('Administrator');
41
        $auth->addChild($admin, $setContentTypes);
42
    }
43
44
    public function setupPermissionsAndRoles()
45
    {
46
        $auth = Yii::$app->authManager;
47
48
        /*
49
         * Permissions
50
         */
51
52
        // Admin
53
        $administration = $auth->createPermission('admin');
54
        $administration->description = 'Administrate everyting';
55
        $auth->add($administration);
56
57
        // Devices
58
        $setDevices = $auth->createPermission('setDevices');
59
        $setDevices->description = 'Manage devices';
60
        $auth->add($setDevices);
61
62
        // Screens
63
        $setScreens = $auth->createPermission('setScreens');
64
        $setScreens->description = 'Manage screens';
65
        $auth->add($setScreens);
66
67
        $previewScreen = $auth->createPermission('previewScreen');
68
        $previewScreen->description = 'Preview screens';
69
        $auth->add($previewScreen);
70
71
        // Templates
72
        $setTemplates = $auth->createPermission('setTemplates');
73
        $setTemplates->description = 'Manage screen templates';
74
        $auth->add($setTemplates);
75
76
        // Flows
77
        $setFlows = $auth->createPermission('setFlows');
78
        $setFlows->description = 'Manage flows';
79
        $auth->add($setFlows);
80
81
        // Flow content
82
        $setFlowContent = $auth->createPermission('setFlowContent');
83
        $setFlowContent->description = 'Manage flow content';
84
        $auth->add($setFlowContent);
85
86
        // Own flow content
87
        $setOwnFlowContent = $auth->createPermission('setOwnFlowContent');
88
        $setOwnFlowContent->description = 'Manage owned flow content';
89
        $auth->add($setOwnFlowContent);
90
91
        // Content
92
        $setContent = $auth->createPermission('setContent');
93
        $setContent->description = 'Manage all content';
94
        $auth->add($setContent);
95
96
        // Upload
97
        $upload = $auth->createPermission('upload');
98
        $upload->description = 'Upload content';
99
        $auth->add($upload);
100
101
        /*
102
         * Roles
103
         */
104
105
        $contentCreator = $auth->createRole('Content creator');
106
        $contentCreator->data = ['requireFlow' => true];
107
        $auth->add($contentCreator);
108
        $auth->addChild($contentCreator, $upload);
109
        $auth->addChild($contentCreator, $setOwnFlowContent);
110
111
        $screenManager = $auth->createRole('Screen manager');
112
        $auth->add($screenManager);
113
        $auth->addChild($screenManager, $setFlowContent);
114
        $auth->addChild($screenManager, $setFlows);
115
        $auth->addChild($screenManager, $setTemplates);
116
        $auth->addChild($screenManager, $setScreens);
117
        $auth->addChild($screenManager, $previewScreen);
118
        $auth->addChild($screenManager, $contentCreator);
119
120
        $admin = $auth->createRole('Administrator');
121
        $auth->add($admin);
122
        $auth->addChild($admin, $administration);
123
        $auth->addChild($admin, $setDevices);
124
        $auth->addChild($admin, $setContent);
125
        $auth->addChild($admin, $screenManager);
126
    }
127
}
128