addRules()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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