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\user\rbac\migrations; |
14
|
|
|
|
15
|
|
|
use rhosocial\user\migrations\Migration; |
16
|
|
|
use rhosocial\user\rbac\roles\Admin; |
17
|
|
|
use rhosocial\user\rbac\roles\User; |
18
|
|
|
use rhosocial\user\rbac\permissions\CreateAdminUser; |
19
|
|
|
use rhosocial\user\rbac\permissions\CreateUser; |
20
|
|
|
use rhosocial\user\rbac\permissions\DeleteAdminUser; |
21
|
|
|
use rhosocial\user\rbac\permissions\DeleteMyself; |
22
|
|
|
use rhosocial\user\rbac\permissions\DeleteUser; |
23
|
|
|
use rhosocial\user\rbac\permissions\ViewUser; |
24
|
|
|
use rhosocial\user\rbac\permissions\UpdateAdminUser; |
25
|
|
|
use rhosocial\user\rbac\permissions\UpdateMyself; |
26
|
|
|
use rhosocial\user\rbac\permissions\UpdateUser; |
27
|
|
|
use rhosocial\user\rbac\roles\Webmaster; |
28
|
|
|
use Yii; |
29
|
|
|
use yii\base\InvalidConfigException; |
30
|
|
|
use yii\rbac\DbManager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create following four tables in order: |
34
|
|
|
* `{{%auth_rule}}` |
35
|
|
|
* `{{%auth_item}}` |
36
|
|
|
* `{{%auth_item_child}}` |
37
|
|
|
* `{{%auth_assignment}}` |
38
|
|
|
* |
39
|
|
|
```SQL |
40
|
|
|
CREATE TABLE `auth_rule` ( |
41
|
|
|
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL COMMENT 'Rule Name', |
42
|
|
|
`data` blob, |
43
|
|
|
`created_at` datetime NOT NULL, |
44
|
|
|
`updated_at` datetime NOT NULL, |
45
|
|
|
PRIMARY KEY (`name`) |
46
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Rule'; |
47
|
|
|
|
48
|
|
|
CREATE TABLE `auth_item` ( |
49
|
|
|
`name` varchar(64) COLLATE utf8_unicode_ci NOT NULL, |
50
|
|
|
`type` smallint(6) NOT NULL, |
51
|
|
|
`description` text COLLATE utf8_unicode_ci COMMENT 'Description', |
52
|
|
|
`rule_name` varchar(64) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Rule Name', |
53
|
|
|
`data` blob, |
54
|
|
|
`color` int(11) NOT NULL DEFAULT '-1' COMMENT 'Color', |
55
|
|
|
`created_at` datetime NOT NULL, |
56
|
|
|
`updated_at` datetime NOT NULL, |
57
|
|
|
PRIMARY KEY (`name`), |
58
|
|
|
KEY `rule_name_fk` (`rule_name`), |
59
|
|
|
KEY `idx-auth_item-type` (`type`), |
60
|
|
|
CONSTRAINT `rule_name_fk` FOREIGN KEY (`rule_name`) REFERENCES `auth_rule` (`name`) ON DELETE CASCADE ON UPDATE CASCADE |
61
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Item'; |
62
|
|
|
|
63
|
|
|
CREATE TABLE `auth_item_child` ( |
64
|
|
|
`parent` varchar(64) COLLATE utf8_unicode_ci NOT NULL, |
65
|
|
|
`child` varchar(64) COLLATE utf8_unicode_ci NOT NULL, |
66
|
|
|
PRIMARY KEY (`parent`,`child`), |
67
|
|
|
KEY `child_name_fk` (`child`), |
68
|
|
|
CONSTRAINT `child_name_fk` FOREIGN KEY (`child`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE, |
69
|
|
|
CONSTRAINT `parent_name_fk` FOREIGN KEY (`parent`) REFERENCES `auth_item` (`name`) ON DELETE CASCADE ON UPDATE CASCADE |
70
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Item Child'; |
71
|
|
|
|
72
|
|
|
CREATE TABLE `auth_assignment` ( |
73
|
|
|
`item_name` varchar(64) COLLATE utf8_unicode_ci NOT NULL, |
74
|
|
|
`user_guid` varbinary(16) NOT NULL, |
75
|
|
|
`created_at` datetime NOT NULL, |
76
|
|
|
`failed_at` datetime DEFAULT NULL, |
77
|
|
|
PRIMARY KEY (`item_name`,`user_guid`), |
78
|
|
|
KEY `user_assignment_fk` (`user_guid`), |
79
|
|
|
CONSTRAINT `user_assignment_fk` FOREIGN KEY (`user_guid`) REFERENCES `user` (`guid`) ON DELETE CASCADE ON UPDATE CASCADE |
80
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci COMMENT='Auth Assignment'; |
81
|
|
|
`` |
82
|
|
|
* |
83
|
|
|
* @codeCoverageIgnore |
84
|
|
|
* @version 1.0 |
85
|
|
|
* @author vistart <[email protected]> |
86
|
|
|
*/ |
87
|
|
|
class M170310150337CreateAuthTables extends Migration |
88
|
|
|
{ |
89
|
|
|
/** |
90
|
|
|
* @throws yii\base\InvalidConfigException |
91
|
|
|
* @return DbManager |
92
|
|
|
*/ |
93
|
|
|
protected function getAuthManager() |
94
|
|
|
{ |
95
|
|
|
$authManager = Yii::$app->getAuthManager(); |
96
|
|
|
if (!$authManager instanceof DbManager) { |
97
|
|
|
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.'); |
98
|
|
|
} |
99
|
|
|
return $authManager; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* |
104
|
|
|
*/ |
105
|
|
|
public function up() |
106
|
|
|
{ |
107
|
|
|
$authManager = $this->getAuthManager(); |
108
|
|
|
$this->db = $authManager->db; |
109
|
|
|
|
110
|
|
|
$tableOptions = null; |
|
|
|
|
111
|
|
|
if ($this->db->driverName === 'mysql') { |
112
|
|
|
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
113
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
114
|
|
|
|
115
|
|
|
$this->createTable($authManager->ruleTable, [ |
116
|
|
|
'name' => $this->varchar(64)->notNull()->comment('Rule Name'), |
117
|
|
|
'data' => $this->blob(), |
118
|
|
|
'created_at' => $this->dateTime()->notNull(), |
119
|
|
|
'updated_at' => $this->dateTime()->notNull(), |
120
|
|
|
], $tableOptions . " COMMENT 'Auth Rule'"); |
121
|
|
|
$this->addPrimaryKey('rule_name_pk', $authManager->ruleTable, 'name'); |
122
|
|
|
|
123
|
|
|
$this->createTable($authManager->itemTable, [ |
124
|
|
|
'name' => $this->varchar(64)->notNull(), |
125
|
|
|
'type' => $this->smallInteger()->notNull(), |
126
|
|
|
'description' => $this->text()->comment('Description'), |
127
|
|
|
'rule_name' => $this->varchar(64)->comment('Rule Name'), |
128
|
|
|
'data' => $this->blob(), |
129
|
|
|
'color' => $this->integer()->defaultValue(-1)->notNull()->comment('Color'), |
130
|
|
|
'created_at' => $this->dateTime()->notNull(), |
131
|
|
|
'updated_at' => $this->dateTime()->notNull(), |
132
|
|
|
], $tableOptions . " COMMENT 'Auth Item'"); |
133
|
|
|
$this->addPrimaryKey('item_name_pk', $authManager->itemTable, 'name'); |
134
|
|
|
$this->addForeignKey('rule_name_fk', $authManager->itemTable, 'rule_name', $authManager->ruleTable, 'name', 'CASCADE', 'CASCADE'); |
135
|
|
|
$this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type'); |
136
|
|
|
|
137
|
|
|
$this->createTable($authManager->itemChildTable, [ |
138
|
|
|
'parent' => $this->varchar(64)->notNull(), |
139
|
|
|
'child' => $this->varchar(64)->notNull(), |
140
|
|
|
], $tableOptions . " COMMENT 'Auth Item Child'"); |
141
|
|
|
$this->addPrimaryKey('parent_child_pk', $authManager->itemChildTable, ['parent', 'child']); |
142
|
|
|
$this->addForeignKey('parent_name_fk', $authManager->itemChildTable, 'parent', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE'); |
143
|
|
|
$this->addForeignKey('child_name_fk', $authManager->itemChildTable, 'child', $authManager->itemTable, 'name', 'CASCADE', 'CASCADE'); |
144
|
|
|
|
145
|
|
|
$this->createTable($authManager->assignmentTable, [ |
146
|
|
|
'item_name' => $this->varchar(64)->notNull(), |
147
|
|
|
'user_guid' => $this->varbinary(16)->notNull(), |
148
|
|
|
'created_at' => $this->dateTime()->notNull(), |
149
|
|
|
'failed_at' => $this->dateTime(), |
150
|
|
|
], $tableOptions . " COMMENT 'Auth Assignment'"); |
151
|
|
|
$this->addPrimaryKey('user_item_name_pk', $authManager->assignmentTable, ['item_name', 'user_guid']); |
152
|
|
|
$this->addForeignKey('user_assignment_fk', $authManager->assignmentTable, 'user_guid', '{{%user}}', 'guid', 'CASCADE', 'CASCADE'); |
153
|
|
|
} |
154
|
|
|
$this->addRules(); |
155
|
|
|
$this->addRoles(); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* |
160
|
|
|
*/ |
161
|
|
|
public function down() |
162
|
|
|
{ |
163
|
|
|
$authManager = $this->getAuthManager(); |
164
|
|
|
$this->db = $authManager->db; |
165
|
|
|
$this->dropTable($authManager->assignmentTable); |
166
|
|
|
$this->dropTable($authManager->itemChildTable); |
167
|
|
|
$this->dropTable($authManager->itemTable); |
168
|
|
|
$this->dropTable($authManager->ruleTable); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
protected function addRules() |
172
|
|
|
{ |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
protected function addRoles() |
176
|
|
|
{ |
177
|
|
|
$authManager = $this->getAuthManager(); |
178
|
|
|
$this->db = $authManager->db; |
179
|
|
|
|
180
|
|
|
$createUser = new CreateUser(); |
181
|
|
|
$viewUser = new ViewUser(); |
182
|
|
|
$updateUser = new UpdateUser(); |
183
|
|
|
$deleteUser = new DeleteUser(); |
184
|
|
|
$updateMyself = new UpdateMyself(); |
185
|
|
|
$deleteMyself = new DeleteMyself(); |
186
|
|
|
$createAdminUser = new CreateAdminUser(); |
187
|
|
|
$updateAdminUser = new UpdateAdminUser(); |
188
|
|
|
$deleteAdminUser = new DeleteAdminUser(); |
189
|
|
|
|
190
|
|
|
$authManager->add($createUser); |
|
|
|
|
191
|
|
|
$authManager->add($viewUser); |
|
|
|
|
192
|
|
|
$authManager->add($updateUser); |
|
|
|
|
193
|
|
|
$authManager->add($deleteUser); |
|
|
|
|
194
|
|
|
$authManager->add($updateMyself); |
|
|
|
|
195
|
|
|
$authManager->add($deleteMyself); |
|
|
|
|
196
|
|
|
$authManager->add($createAdminUser); |
|
|
|
|
197
|
|
|
$authManager->add($updateAdminUser); |
|
|
|
|
198
|
|
|
$authManager->add($deleteAdminUser); |
|
|
|
|
199
|
|
|
|
200
|
|
|
$admin = new Admin(); |
201
|
|
|
$user = new User(); |
202
|
|
|
$webmaster = new Webmaster(); |
203
|
|
|
|
204
|
|
|
$authManager->add($admin); |
|
|
|
|
205
|
|
|
$authManager->add($user); |
|
|
|
|
206
|
|
|
$authManager->add($webmaster); |
|
|
|
|
207
|
|
|
|
208
|
|
|
$authManager->addChild($user, $updateMyself); |
209
|
|
|
$authManager->addChild($user, $deleteMyself); |
210
|
|
|
$authManager->addChild($admin, $user); |
211
|
|
|
|
212
|
|
|
$authManager->addChild($admin, $createUser); |
213
|
|
|
$authManager->addChild($admin, $viewUser); |
214
|
|
|
$authManager->addChild($admin, $updateUser); |
215
|
|
|
$authManager->addChild($admin, $deleteUser); |
216
|
|
|
|
217
|
|
|
$authManager->addChild($webmaster, $admin); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/* |
221
|
|
|
// Use safeUp/safeDown to run migration code within a transaction |
222
|
|
|
public function safeUp() |
223
|
|
|
{ |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
public function safeDown() |
227
|
|
|
{ |
228
|
|
|
} |
229
|
|
|
*/ |
230
|
|
|
} |
231
|
|
|
|
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.