Completed
Push — master ( a8bd66...b8a5a5 )
by vistart
04:29
created

m170328_063048_insertPermissionsRolesAndRules   B

Complexity

Total Complexity 8

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 18

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 18
dl 0
loc 144
rs 7.3333

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAuthManager() 0 8 2
A up() 0 5 1
A down() 0 5 1
A addRules() 0 15 1
A removeRules() 0 15 1
B addRoles() 0 40 1
B removeRoles() 0 32 1
1
<?php
2
3
/**
4
 *  _   __ __ _____ _____ ___  ____  _____
5
 * | | / // // ___//_  _//   ||  __||_   _|
6
 * | |/ // /(__  )  / / / /| || |     | |
7
 * |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\organization\rbac\migrations;
14
15
use rhosocial\user\migrations\Migration;
16
use rhosocial\organization\rbac\rules\ManageMemberRule;
17
use rhosocial\organization\rbac\rules\ManageProfileRule;
18
use rhosocial\organization\rbac\rules\RevokeDepartmentRule;
19
use rhosocial\organization\rbac\rules\RevokeOrganizationRule;
20
use rhosocial\organization\rbac\rules\SetUpDepartmentRule;
21
use rhosocial\organization\rbac\permissions\ManageMember;
22
use rhosocial\organization\rbac\permissions\ManageProfile;
23
use rhosocial\organization\rbac\permissions\RevokeDepartment;
24
use rhosocial\organization\rbac\permissions\RevokeOrganization;
25
use rhosocial\organization\rbac\permissions\SetUpDepartment;
26
use rhosocial\organization\rbac\permissions\SetUpOrganization;
27
use rhosocial\organization\rbac\roles\DepartmentAdmin;
28
use rhosocial\organization\rbac\roles\DepartmentCreator;
29
use rhosocial\organization\rbac\roles\OrganizationAdmin;
30
use rhosocial\organization\rbac\roles\OrganizationCreator;
31
use Yii;
32
use yii\base\InvalidConfigException;
33
use yii\rbac\DbManager;
34
35
/**
36
 * @version 1.0
37
 * @author vistart <[email protected]>
38
 */
39
class m170328_063048_insertPermissionsRolesAndRules extends Migration
40
{
41
    /**
42
     * @throws InvalidConfigException
43
     * @return DbManager
44
     */
45
    protected function getAuthManager()
46
    {
47
        $authManager = Yii::$app->getAuthManager();
48
        if (!$authManager instanceof DbManager) {
49
            throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.');
50
        }
51
        return $authManager;
52
    }
53
54
    public function up()
55
    {
56
        $this->addRules();
57
        $this->addRoles();
58
    }
59
60
    public function down()
61
    {
62
        $this->removeRoles();
63
        $this->removeRules();
64
    }
65
66
    protected function addRules()
67
    {
68
        $authManager = Yii::$app->authManager;
69
        
70
        $manageMemberRule = new ManageMemberRule();
71
        $manageProfileRule = new ManageProfileRule();
72
        $revokeDepartmentRule = new RevokeDepartmentRule();
73
        $revokeOrganizationRule = new RevokeOrganizationRule();
74
        $setUpDepartmentRule = new SetUpDepartmentRule();
75
        $authManager->add($manageMemberRule);
76
        $authManager->add($manageProfileRule);
77
        $authManager->add($revokeDepartmentRule);
78
        $authManager->add($revokeOrganizationRule);
79
        $authManager->add($setUpDepartmentRule);
80
    }
81
82
    protected function removeRules()
83
    {
84
        $authManager = Yii::$app->authManager;
85
        
86
        $manageMemberRule = new ManageMemberRule();
87
        $manageProfileRule = new ManageProfileRule();
88
        $revokeDepartmentRule = new RevokeDepartmentRule();
89
        $revokeOrganizationRule = new RevokeOrganizationRule();
90
        $setUpDepartmentRule = new SetUpDepartmentRule();
91
        $authManager->remove($manageMemberRule);
92
        $authManager->remove($manageProfileRule);
93
        $authManager->remove($revokeDepartmentRule);
94
        $authManager->remove($revokeOrganizationRule);
95
        $authManager->remove($setUpDepartmentRule);
96
    }
97
98
    protected function addRoles()
99
    {
100
        $authManager = Yii::$app->authManager;
101
102
        $manageMember = new ManageMember();
103
        $manageProfile = new ManageProfile();
104
        $revokeDepartment = new RevokeDepartment();
105
        $revokeOrganization = new RevokeOrganization();
106
        $setUpDepartment = new SetUpDepartment();
107
        $setUpOrganization = new SetUpOrganization();
108
109
        $authManager->add($manageMember);
110
        $authManager->add($manageProfile);
111
        $authManager->add($revokeDepartment);
112
        $authManager->add($revokeOrganization);
113
        $authManager->add($setUpDepartment);
114
        $authManager->add($setUpOrganization);
115
116
        $departmentAdmin = new DepartmentAdmin();
117
        $departmentCreator = new DepartmentCreator();
118
        $organizationAdmin = new OrganizationAdmin();
119
        $organizationCreator = new OrganizationCreator();
120
121
        $authManager->add($departmentAdmin);
122
        $authManager->add($departmentCreator);
123
        $authManager->add($organizationAdmin);
124
        $authManager->add($organizationCreator);
125
126
        $authManager->addChild($departmentAdmin, $manageMember);
127
        $authManager->addChild($departmentAdmin, $manageProfile);
128
        $authManager->addChild($departmentAdmin, $setUpDepartment);
129
        $authManager->addChild($departmentAdmin, $revokeDepartment);
130
131
        $authManager->addChild($departmentCreator, $departmentAdmin);
132
133
        $authManager->addChild($organizationAdmin, $departmentCreator);
134
135
        $authManager->addChild($organizationCreator, $organizationAdmin);
136
        $authManager->addChild($organizationCreator, $revokeOrganization);
137
    }
138
139
    protected function removeRoles()
140
    {
141
        $authManager = Yii::$app->authManager;
142
143
        $departmentAdmin = new DepartmentAdmin();
144
        $departmentCreator = new DepartmentCreator();
145
        $organizationAdmin = new OrganizationAdmin();
146
        $organizationCreator = new OrganizationCreator();
147
148
        $authManager->removeChildren($departmentAdmin);
149
        $authManager->removeChildren($departmentCreator);
150
        $authManager->removeChildren($organizationAdmin);
151
        $authManager->removeChildren($organizationCreator);
152
        $authManager->remove($departmentAdmin);
153
        $authManager->remove($departmentCreator);
154
        $authManager->remove($organizationAdmin);
155
        $authManager->remove($organizationCreator);
156
157
        $manageMember = new ManageMember();
158
        $manageProfile = new ManageProfile();
159
        $revokeDepartment = new RevokeDepartment();
160
        $revokeOrganization = new RevokeOrganization();
161
        $setUpDepartment = new SetUpDepartment();
162
        $setUpOrganization = new SetUpOrganization();
163
164
        $authManager->remove($manageMember);
165
        $authManager->remove($manageProfile);
166
        $authManager->remove($revokeDepartment);
167
        $authManager->remove($revokeOrganization);
168
        $authManager->remove($setUpDepartment);
169
        $authManager->remove($setUpOrganization);
170
    }
171
172
    /*
173
    // Use safeUp/safeDown to run migration code within a transaction
174
    public function safeUp()
175
    {
176
    }
177
178
    public function safeDown()
179
    {
180
    }
181
    */
182
}
183