1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
use yii\base\InvalidConfigException; |
9
|
|
|
use yii\rbac\DbManager; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Initializes RBAC tables |
13
|
|
|
* |
14
|
|
|
* @author Alexander Kochetov <[email protected]> |
15
|
|
|
* @since 2.0 |
16
|
|
|
*/ |
17
|
|
|
class m140506_102106_rbac_init extends \yii\db\Migration |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @throws yii\base\InvalidConfigException |
21
|
|
|
* @return DbManager |
22
|
|
|
*/ |
23
|
|
|
protected function getAuthManager() |
24
|
|
|
{ |
25
|
|
|
$authManager = Yii::$app->getAuthManager(); |
26
|
|
|
if (!$authManager instanceof DbManager) { |
27
|
|
|
throw new InvalidConfigException('You should configure "authManager" component to use database before executing this migration.'); |
28
|
|
|
} |
29
|
|
|
return $authManager; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
protected function isMSSQL() |
36
|
|
|
{ |
37
|
|
|
return $this->db->driverName === 'mssql' || $this->db->driverName === 'sqlsrv' || $this->db->driverName === 'dblib'; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function isPgSQL() |
41
|
|
|
{ |
42
|
|
|
return $this->db->driverName === 'oci'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritdoc |
47
|
|
|
*/ |
48
|
|
|
public function up() |
49
|
|
|
{ |
50
|
|
|
$authManager = $this->getAuthManager(); |
51
|
|
|
$this->db = $authManager->db; |
52
|
|
|
|
53
|
|
|
$tableOptions = null; |
54
|
|
|
if ($this->db->driverName === 'mysql') { |
55
|
|
|
// http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
56
|
|
|
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$this->createTable($authManager->ruleTable, [ |
60
|
|
|
'name' => $this->string(64)->notNull(), |
61
|
|
|
'data' => $this->binary(), |
62
|
|
|
'created_at' => $this->integer(), |
63
|
|
|
'updated_at' => $this->integer(), |
64
|
|
|
'PRIMARY KEY ([[name]])', |
65
|
|
|
], $tableOptions); |
66
|
|
|
|
67
|
|
|
$this->createTable($authManager->itemTable, [ |
68
|
|
|
'name' => $this->string(64)->notNull(), |
69
|
|
|
'type' => $this->smallInteger()->notNull(), |
70
|
|
|
'description' => $this->text(), |
71
|
|
|
'rule_name' => $this->string(64), |
72
|
|
|
'data' => $this->binary(), |
73
|
|
|
'created_at' => $this->integer(), |
74
|
|
|
'updated_at' => $this->integer(), |
75
|
|
|
'PRIMARY KEY ([[name]])', |
76
|
|
|
'FOREIGN KEY ([[rule_name]]) REFERENCES ' . $authManager->ruleTable . ' ([[name]])'. |
77
|
|
|
$this->buildFkClause() |
78
|
|
|
], $tableOptions); |
79
|
|
|
$this->createIndex('idx-auth_item-type', $authManager->itemTable, 'type'); |
80
|
|
|
|
81
|
|
|
$this->createTable($authManager->itemChildTable, [ |
82
|
|
|
'parent' => $this->string(64)->notNull(), |
83
|
|
|
'child' => $this->string(64)->notNull(), |
84
|
|
|
'PRIMARY KEY ([[parent]], [[child]])', |
85
|
|
|
'FOREIGN KEY ([[parent]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])'. |
86
|
|
|
$this->buildFkClause(), |
87
|
|
|
'FOREIGN KEY ([[child]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])'. |
88
|
|
|
$this->buildFkClause(), |
89
|
|
|
], $tableOptions); |
90
|
|
|
|
91
|
|
|
$this->createTable($authManager->assignmentTable, [ |
92
|
|
|
'item_name' => $this->string(64)->notNull(), |
93
|
|
|
'user_id' => $this->string(64)->notNull(), |
94
|
|
|
'created_at' => $this->integer(), |
95
|
|
|
'PRIMARY KEY ([[item_name]], [[user_id]])', |
96
|
|
|
'FOREIGN KEY ([[item_name]]) REFERENCES ' . $authManager->itemTable . ' ([[name]])' . |
97
|
|
|
$this->buildFkClause(), |
98
|
|
|
], $tableOptions); |
99
|
|
|
|
100
|
|
|
if ($this->isMSSQL()) { |
101
|
|
|
$this->execute("CREATE TRIGGER dbo.trigger_auth_item_child |
102
|
|
|
ON dbo.{$authManager->itemTable} |
103
|
|
|
INSTEAD OF DELETE, UPDATE |
104
|
|
|
AS |
105
|
|
|
DECLARE @old_name VARCHAR (64) = (SELECT name FROM deleted) |
106
|
|
|
DECLARE @new_name VARCHAR (64) = (SELECT name FROM inserted) |
107
|
|
|
BEGIN |
108
|
|
|
IF COLUMNS_UPDATED() > 0 |
109
|
|
|
BEGIN |
110
|
|
|
IF @old_name <> @new_name |
111
|
|
|
BEGIN |
112
|
|
|
ALTER TABLE {$authManager->itemChildTable} NOCHECK CONSTRAINT FK__auth_item__child; |
113
|
|
|
UPDATE {$authManager->itemChildTable} SET child = @new_name WHERE child = @old_name; |
114
|
|
|
END |
115
|
|
|
UPDATE {$authManager->itemTable} |
116
|
|
|
SET name = (SELECT name FROM inserted), |
117
|
|
|
type = (SELECT type FROM inserted), |
118
|
|
|
description = (SELECT description FROM inserted), |
119
|
|
|
rule_name = (SELECT rule_name FROM inserted), |
120
|
|
|
data = (SELECT data FROM inserted), |
121
|
|
|
created_at = (SELECT created_at FROM inserted), |
122
|
|
|
updated_at = (SELECT updated_at FROM inserted) |
123
|
|
|
WHERE name IN (SELECT name FROM deleted) |
124
|
|
|
IF @old_name <> @new_name |
125
|
|
|
BEGIN |
126
|
|
|
ALTER TABLE {$authManager->itemChildTable} CHECK CONSTRAINT FK__auth_item__child; |
127
|
|
|
END |
128
|
|
|
END |
129
|
|
|
ELSE |
130
|
|
|
BEGIN |
131
|
|
|
DELETE FROM dbo.{$authManager->itemChildTable} WHERE parent IN (SELECT name FROM deleted) OR child IN (SELECT name FROM deleted); |
132
|
|
|
DELETE FROM dbo.{$authManager->itemTable} WHERE name IN (SELECT name FROM deleted); |
133
|
|
|
END |
134
|
|
|
END;"); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
|
|
public function down() |
142
|
|
|
{ |
143
|
|
|
$authManager = $this->getAuthManager(); |
144
|
|
|
$this->db = $authManager->db; |
145
|
|
|
|
146
|
|
|
if ($this->isMSSQL()) { |
147
|
|
|
$this->execute('DROP TRIGGER dbo.trigger_auth_item_child;'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$this->dropTable($authManager->assignmentTable); |
151
|
|
|
$this->dropTable($authManager->itemChildTable); |
152
|
|
|
$this->dropTable($authManager->itemTable); |
153
|
|
|
$this->dropTable($authManager->ruleTable); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
protected function buildFkClause() |
157
|
|
|
{ |
158
|
|
|
if ($this->isMSSQL()) { |
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ($this->isPgSQL()) { |
163
|
|
|
return ' ON DELETE SET NULL'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return ' ON DELETE SET NULL ON UPDATE CASCADE'; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|