|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Phinx\Migration\AbstractMigration; |
|
4
|
|
|
|
|
5
|
|
|
class Acl extends AbstractMigration |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* Change Method. |
|
9
|
|
|
* |
|
10
|
|
|
* Write your reversible migrations using this method. |
|
11
|
|
|
* |
|
12
|
|
|
* More information on writing migrations is available here: |
|
13
|
|
|
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class |
|
14
|
|
|
* |
|
15
|
|
|
* The following commands can be used in this method and Phinx will |
|
16
|
|
|
* automatically reverse them when rolling back: |
|
17
|
|
|
* |
|
18
|
|
|
* createTable |
|
19
|
|
|
* renameTable |
|
20
|
|
|
* addColumn |
|
21
|
|
|
* renameColumn |
|
22
|
|
|
* addIndex |
|
23
|
|
|
* addForeignKey |
|
24
|
|
|
* |
|
25
|
|
|
* Remember to call "create()" or "update()" and NOT "save()" when working |
|
26
|
|
|
* with the Table class. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function change() |
|
29
|
|
|
{ |
|
30
|
|
|
$roles = $this->table('acl_roles', ['primary_key' => ['id', 'code']]); |
|
31
|
|
|
$roles |
|
32
|
|
|
->addColumn('name', 'string', ['length'=>255]) |
|
33
|
|
|
->addColumn('created', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => '']) |
|
34
|
|
|
->addIndex(['name'], ['unique' => true]) |
|
35
|
|
|
->create(); |
|
36
|
|
|
|
|
37
|
|
|
$privileges = $this->table('acl_privileges', ['primary_key' => ['roleId', 'module', 'privilege']]); |
|
38
|
|
|
$privileges |
|
39
|
|
|
->addColumn('roleId', 'integer') |
|
40
|
|
|
->addColumn('module', 'string', ['length'=>32]) |
|
41
|
|
|
->addColumn('privilege', 'string', ['length'=>32]) |
|
42
|
|
|
->addForeignKey('roleId', $roles, 'id', [ |
|
43
|
|
|
'delete' => 'CASCADE', |
|
44
|
|
|
'update' => 'CASCADE' |
|
45
|
|
|
]) |
|
46
|
|
|
->create(); |
|
47
|
|
|
|
|
48
|
|
|
$usersRoles = $this->table('acl_users_roles', ['id' => false, 'primary_key' => ['userId', 'roleId']]); |
|
49
|
|
|
$usersRoles |
|
50
|
|
|
->addColumn('userId', 'integer') |
|
51
|
|
|
->addColumn('roleId', 'integer') |
|
52
|
|
|
->addForeignKey('userId', 'users', 'id', [ |
|
53
|
|
|
'delete' => 'CASCADE', |
|
54
|
|
|
'update' => 'CASCADE' |
|
55
|
|
|
]) |
|
56
|
|
|
->addForeignKey('roleId', $roles, 'id', [ |
|
57
|
|
|
'delete' => 'CASCADE', |
|
58
|
|
|
'update' => 'CASCADE' |
|
59
|
|
|
]) |
|
60
|
|
|
->create(); |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$roles = [ |
|
64
|
|
|
[ |
|
65
|
|
|
'id' => 1, |
|
66
|
|
|
'name' => 'system' |
|
67
|
|
|
], |
|
68
|
|
|
[ |
|
69
|
|
|
'id' => 2, |
|
70
|
|
|
'name' => 'admin' |
|
71
|
|
|
], |
|
72
|
|
|
[ |
|
73
|
|
|
'id' => 3, |
|
74
|
|
|
'name' => 'member' |
|
75
|
|
|
], |
|
76
|
|
|
[ |
|
77
|
|
|
'id' => 4, |
|
78
|
|
|
'name' => 'guest' |
|
79
|
|
|
], |
|
80
|
|
|
]; |
|
81
|
|
|
$this->table('acl_roles')->insert($roles)->save(); |
|
82
|
|
|
|
|
83
|
|
|
$privileges = [ |
|
84
|
|
|
[ |
|
85
|
|
|
'roleId' => 2, |
|
86
|
|
|
'module' => 'acl', |
|
87
|
|
|
'privilege' => 'Management' |
|
88
|
|
|
], |
|
89
|
|
|
[ |
|
90
|
|
|
'roleId' => 2, |
|
91
|
|
|
'module' => 'acl', |
|
92
|
|
|
'privilege' => 'View' |
|
93
|
|
|
], |
|
94
|
|
|
[ |
|
95
|
|
|
'roleId' => 2, |
|
96
|
|
|
'module' => 'cache', |
|
97
|
|
|
'privilege' => 'Management' |
|
98
|
|
|
], |
|
99
|
|
|
[ |
|
100
|
|
|
'roleId' => 2, |
|
101
|
|
|
'module' => 'dashboard', |
|
102
|
|
|
'privilege' => 'Dashboard' |
|
103
|
|
|
], |
|
104
|
|
|
[ |
|
105
|
|
|
'roleId' => 2, |
|
106
|
|
|
'module' => 'pages', |
|
107
|
|
|
'privilege' => 'Management' |
|
108
|
|
|
], |
|
109
|
|
|
[ |
|
110
|
|
|
'roleId' => 2, |
|
111
|
|
|
'module' => 'system', |
|
112
|
|
|
'privilege' => 'Info' |
|
113
|
|
|
], |
|
114
|
|
|
[ |
|
115
|
|
|
'roleId' => 2, |
|
116
|
|
|
'module' => 'users', |
|
117
|
|
|
'privilege' => 'EditEmail' |
|
118
|
|
|
], |
|
119
|
|
|
[ |
|
120
|
|
|
'roleId' => 2, |
|
121
|
|
|
'module' => 'users', |
|
122
|
|
|
'privilege' => 'EditPassword' |
|
123
|
|
|
], |
|
124
|
|
|
[ |
|
125
|
|
|
'roleId' => 2, |
|
126
|
|
|
'module' => 'users', |
|
127
|
|
|
'privilege' => 'Management' |
|
128
|
|
|
], |
|
129
|
|
|
[ |
|
130
|
|
|
'roleId' => 2, |
|
131
|
|
|
'module' => 'users', |
|
132
|
|
|
'privilege' => 'ViewProfile' |
|
133
|
|
|
], |
|
134
|
|
|
[ |
|
135
|
|
|
'roleId' => 3, |
|
136
|
|
|
'module' => 'users', |
|
137
|
|
|
'privilege' => 'EditEmail' |
|
138
|
|
|
], |
|
139
|
|
|
[ |
|
140
|
|
|
'roleId' => 3, |
|
141
|
|
|
'module' => 'users', |
|
142
|
|
|
'privilege' => 'EditPassword' |
|
143
|
|
|
], |
|
144
|
|
|
[ |
|
145
|
|
|
'roleId' => 3, |
|
146
|
|
|
'module' => 'users', |
|
147
|
|
|
'privilege' => 'Management' |
|
148
|
|
|
], |
|
149
|
|
|
[ |
|
150
|
|
|
'roleId' => 3, |
|
151
|
|
|
'module' => 'users', |
|
152
|
|
|
'privilege' => 'ViewProfile' |
|
153
|
|
|
], |
|
154
|
|
|
]; |
|
155
|
|
|
$this->table('acl_privileges')->insert($privileges)->save(); |
|
156
|
|
|
|
|
157
|
|
|
$usersRoles = [ |
|
158
|
|
|
[ |
|
159
|
|
|
'userId' => 1, |
|
160
|
|
|
'roleId' => 1 |
|
161
|
|
|
], |
|
162
|
|
|
[ |
|
163
|
|
|
'userId' => 2, |
|
164
|
|
|
'roleId' => 2 |
|
165
|
|
|
], |
|
166
|
|
|
]; |
|
167
|
|
|
$this->table('acl_users_roles')->insert($usersRoles)->save(); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.