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\permissions\ManageMember; |
17
|
|
|
use rhosocial\organization\rbac\permissions\ManageProfile; |
18
|
|
|
use rhosocial\organization\rbac\permissions\RevokeDepartment; |
19
|
|
|
use rhosocial\organization\rbac\permissions\RevokeOrganization; |
20
|
|
|
use rhosocial\organization\rbac\permissions\SetUpDepartment; |
21
|
|
|
use rhosocial\organization\rbac\permissions\SetUpOrganization; |
22
|
|
|
use rhosocial\organization\rbac\roles\DepartmentAdmin; |
23
|
|
|
use rhosocial\organization\rbac\roles\DepartmentCreator; |
24
|
|
|
use rhosocial\organization\rbac\roles\OrganizationAdmin; |
25
|
|
|
use rhosocial\organization\rbac\roles\OrganizationCreator; |
26
|
|
|
use Yii; |
27
|
|
|
use yii\base\InvalidConfigException; |
28
|
|
|
use yii\rbac\DbManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @version 1.0 |
32
|
|
|
* @author vistart <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
class m170328_063048_insertPermissionsRolesAndRules extends Migration |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @throws InvalidConfigException |
38
|
|
|
* @return DbManager |
39
|
|
|
*/ |
40
|
|
|
protected function getAuthManager() |
41
|
|
|
{ |
42
|
|
|
$authManager = Yii::$app->getAuthManager(); |
43
|
|
|
if (!$authManager instanceof DbManager) { |
44
|
|
|
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.'); |
45
|
|
|
} |
46
|
|
|
return $authManager; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function up() |
50
|
|
|
{ |
51
|
|
|
$this->addRules(); |
52
|
|
|
$this->addRoles(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function down() |
56
|
|
|
{ |
57
|
|
|
$authManager = Yii::$app->authManager; |
58
|
|
|
$db = $authManager->db; |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->removeRoles(); |
61
|
|
|
$this->removeRules(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function addRules() |
65
|
|
|
{ |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function removeRules() |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function addRoles() |
75
|
|
|
{ |
76
|
|
|
$authManager = Yii::$app->authManager; |
77
|
|
|
$db = $authManager->db; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$manageMember = new ManageMember(); |
80
|
|
|
$manageProfile = new ManageProfile(); |
81
|
|
|
$revokeDepartment = new RevokeDepartment(); |
82
|
|
|
$revokeOrganization = new RevokeOrganization(); |
83
|
|
|
$setUpDepartment = new SetUpDepartment(); |
84
|
|
|
$setUpOrganization = new SetUpOrganization(); |
85
|
|
|
|
86
|
|
|
$authManager->add($manageMember); |
87
|
|
|
$authManager->add($manageProfile); |
88
|
|
|
$authManager->add($revokeDepartment); |
89
|
|
|
$authManager->add($revokeOrganization); |
90
|
|
|
$authManager->add($setUpDepartment); |
91
|
|
|
$authManager->add($setUpOrganization); |
92
|
|
|
|
93
|
|
|
$departmentAdmin = new DepartmentAdmin(); |
94
|
|
|
$departmentCreator = new DepartmentCreator(); |
95
|
|
|
$organizationAdmin = new OrganizationAdmin(); |
96
|
|
|
$organizationCreator = new OrganizationCreator(); |
97
|
|
|
|
98
|
|
|
$authManager->add($departmentAdmin); |
99
|
|
|
$authManager->add($departmentCreator); |
100
|
|
|
$authManager->add($organizationAdmin); |
101
|
|
|
$authManager->add($organizationCreator); |
102
|
|
|
|
103
|
|
|
$authManager->addChild($departmentAdmin, $manageMember); |
104
|
|
|
$authManager->addChild($departmentAdmin, $manageProfile); |
105
|
|
|
|
106
|
|
|
$authManager->addChild($departmentCreator, $departmentAdmin); |
107
|
|
|
$authManager->addChild($departmentCreator, $revokeDepartment); |
108
|
|
|
|
109
|
|
|
$authManager->addChild($organizationAdmin, $departmentCreator); |
110
|
|
|
$authManager->addChild($organizationAdmin, $setUpDepartment); |
111
|
|
|
$authManager->addChild($organizationAdmin, $revokeDepartment); |
112
|
|
|
|
113
|
|
|
$authManager->addChild($organizationCreator, $organizationAdmin); |
114
|
|
|
$authManager->addChild($organizationCreator, $revokeOrganization); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function removeRoles() |
118
|
|
|
{ |
119
|
|
|
$authManager = Yii::$app->authManager; |
120
|
|
|
$db = $authManager->db; |
|
|
|
|
121
|
|
|
|
122
|
|
|
$departmentAdmin = new DepartmentAdmin(); |
123
|
|
|
$departmentCreator = new DepartmentCreator(); |
124
|
|
|
$organizationAdmin = new OrganizationAdmin(); |
125
|
|
|
$organizationCreator = new OrganizationCreator(); |
126
|
|
|
|
127
|
|
|
$authManager->removeChildren($departmentAdmin); |
128
|
|
|
$authManager->removeChildren($departmentCreator); |
129
|
|
|
$authManager->removeChildren($organizationAdmin); |
130
|
|
|
$authManager->removeChildren($organizationCreator); |
131
|
|
|
$authManager->remove($departmentAdmin); |
132
|
|
|
$authManager->remove($departmentCreator); |
133
|
|
|
$authManager->remove($organizationAdmin); |
134
|
|
|
$authManager->remove($organizationCreator); |
135
|
|
|
|
136
|
|
|
$manageMember = new ManageMember(); |
137
|
|
|
$manageProfile = new ManageProfile(); |
138
|
|
|
$revokeDepartment = new RevokeDepartment(); |
139
|
|
|
$revokeOrganization = new RevokeOrganization(); |
140
|
|
|
$setUpDepartment = new SetUpDepartment(); |
141
|
|
|
$setUpOrganization = new SetUpOrganization(); |
142
|
|
|
|
143
|
|
|
$authManager->remove($manageMember); |
144
|
|
|
$authManager->remove($manageProfile); |
145
|
|
|
$authManager->remove($revokeDepartment); |
146
|
|
|
$authManager->remove($revokeOrganization); |
147
|
|
|
$authManager->remove($setUpDepartment); |
148
|
|
|
$authManager->remove($setUpOrganization); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/* |
152
|
|
|
// Use safeUp/safeDown to run migration code within a transaction |
153
|
|
|
public function safeUp() |
154
|
|
|
{ |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function safeDown() |
158
|
|
|
{ |
159
|
|
|
} |
160
|
|
|
*/ |
161
|
|
|
} |
162
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.