Completed
Push — master ( c7d5ad...0208a8 )
by Sherif
02:59
created

InitializeAcl::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
use Illuminate\Database\Schema\Blueprint;
4
use Illuminate\Database\Migrations\Migration;
5
6
class InitializeAcl extends Migration
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
	/**
9
	 * Run the migrations.
10
	 *
11
	 * @return void
12
	 */
13
	public function up()
14
	{
15
        /**
16
         * Delete previous permissions.
17
         */
18
		DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->delete();
19
20
		/**
21
         * Insert the permissions related to this module.
22
         */
23
        DB::table('permissions')->insert(
24
        	[
25
        		/**
26
        		 * Users model permissions.
27
        		 */
28
	        	[
29
	        	'name'       => 'save',
30
	        	'model'      => 'users',
31
	        	'created_at' => \DB::raw('NOW()'),
32
	        	'updated_at' => \DB::raw('NOW()')
33
	        	],
34
	        	[
35
	        	'name'       => 'delete',
36
	        	'model'      => 'users',
37
	        	'created_at' => \DB::raw('NOW()'),
38
	        	'updated_at' => \DB::raw('NOW()')
39
	        	],
40
	        	[
41
	        	'name'       => 'find',
42
	        	'model'      => 'users',
43
	        	'created_at' => \DB::raw('NOW()'),
44
	        	'updated_at' => \DB::raw('NOW()')
45
	        	],
46
	        	[
47
	        	'name'       => 'list',
48
	        	'model'      => 'users',
49
	        	'created_at' => \DB::raw('NOW()'),
50
	        	'updated_at' => \DB::raw('NOW()')
51
	        	],
52
	        	[
53
	        	'name'       => 'search',
54
	        	'model'      => 'users',
55
	        	'created_at' => \DB::raw('NOW()'),
56
	        	'updated_at' => \DB::raw('NOW()')
57
	        	],
58
	        	[
59
	        	'name'       => 'findby',
60
	        	'model'      => 'users',
61
	        	'created_at' => \DB::raw('NOW()'),
62
	        	'updated_at' => \DB::raw('NOW()')
63
	        	],
64
	        	[
65
	        	'name'       => 'first',
66
	        	'model'      => 'users',
67
	        	'created_at' => \DB::raw('NOW()'),
68
	        	'updated_at' => \DB::raw('NOW()')
69
	        	],
70
	        	[
71
	        	'name'       => 'paginate',
72
	        	'model'      => 'users',
73
	        	'created_at' => \DB::raw('NOW()'),
74
	        	'updated_at' => \DB::raw('NOW()')
75
	        	],
76
	        	[
77
	        	'name'       => 'paginateby',
78
	        	'model'      => 'users',
79
	        	'created_at' => \DB::raw('NOW()'),
80
	        	'updated_at' => \DB::raw('NOW()')
81
	        	],
82
	        	[
83
	        	'name'       => 'assigngroup',
84
	        	'model'      => 'users',
85
	        	'created_at' => \DB::raw('NOW()'),
86
	        	'updated_at' => \DB::raw('NOW()')
87
	        	],
88
89
	        	/**
90
        		 * Permissions model permissions.
91
        		 */
92
        		[
93
	        	'name'       => 'find',
94
	        	'model'      => 'permissions',
95
	        	'created_at' => \DB::raw('NOW()'),
96
	        	'updated_at' => \DB::raw('NOW()')
97
	        	],
98
	        	[
99
	        	'name'       => 'search',
100
	        	'model'      => 'permissions',
101
	        	'created_at' => \DB::raw('NOW()'),
102
	        	'updated_at' => \DB::raw('NOW()')
103
	        	],
104
	        	[
105
	        	'name'       => 'list',
106
	        	'model'      => 'permissions',
107
	        	'created_at' => \DB::raw('NOW()'),
108
	        	'updated_at' => \DB::raw('NOW()')
109
	        	],
110
	        	[
111
	        	'name'       => 'findby',
112
	        	'model'      => 'permissions',
113
	        	'created_at' => \DB::raw('NOW()'),
114
	        	'updated_at' => \DB::raw('NOW()')
115
	        	],
116
	        	[
117
	        	'name'       => 'first',
118
	        	'model'      => 'permissions',
119
	        	'created_at' => \DB::raw('NOW()'),
120
	        	'updated_at' => \DB::raw('NOW()')
121
	        	],
122
	        	[
123
	        	'name'       => 'paginate',
124
	        	'model'      => 'permissions',
125
	        	'created_at' => \DB::raw('NOW()'),
126
	        	'updated_at' => \DB::raw('NOW()')
127
	        	],
128
	        	[
129
	        	'name'       => 'paginateby',
130
	        	'model'      => 'permissions',
131
	        	'created_at' => \DB::raw('NOW()'),
132
	        	'updated_at' => \DB::raw('NOW()')
133
	        	],
134
135
	        	/**
136
        		 * Groups model permissions.
137
        		 */
138
	        	[
139
	        	'name'       => 'save',
140
	        	'model'      => 'groups',
141
	        	'created_at' => \DB::raw('NOW()'),
142
	        	'updated_at' => \DB::raw('NOW()')
143
	        	],
144
	        	[
145
	        	'name'       => 'delete',
146
	        	'model'      => 'groups',
147
	        	'created_at' => \DB::raw('NOW()'),
148
	        	'updated_at' => \DB::raw('NOW()')
149
	        	],
150
	        	[
151
	        	'name'       => 'find',
152
	        	'model'      => 'groups',
153
	        	'created_at' => \DB::raw('NOW()'),
154
	        	'updated_at' => \DB::raw('NOW()')
155
	        	],
156
	        	[
157
	        	'name'       => 'search',
158
	        	'model'      => 'groups',
159
	        	'created_at' => \DB::raw('NOW()'),
160
	        	'updated_at' => \DB::raw('NOW()')
161
	        	],
162
	        	[
163
	        	'name'       => 'list',
164
	        	'model'      => 'groups',
165
	        	'created_at' => \DB::raw('NOW()'),
166
	        	'updated_at' => \DB::raw('NOW()')
167
	        	],
168
	        	[
169
	        	'name'       => 'findby',
170
	        	'model'      => 'groups',
171
	        	'created_at' => \DB::raw('NOW()'),
172
	        	'updated_at' => \DB::raw('NOW()')
173
	        	],
174
	        	[
175
	        	'name'       => 'first',
176
	        	'model'      => 'groups',
177
	        	'created_at' => \DB::raw('NOW()'),
178
	        	'updated_at' => \DB::raw('NOW()')
179
	        	],
180
	        	[
181
	        	'name'       => 'paginate',
182
	        	'model'      => 'groups',
183
	        	'created_at' => \DB::raw('NOW()'),
184
	        	'updated_at' => \DB::raw('NOW()')
185
	        	],
186
	        	[
187
	        	'name'       => 'paginateby',
188
	        	'model'      => 'groups',
189
	        	'created_at' => \DB::raw('NOW()'),
190
	        	'updated_at' => \DB::raw('NOW()')
191
	        	],
192
	        	[
193
	        	'name'       => 'assignpermissions',
194
	        	'model'      => 'groups',
195
	        	'created_at' => \DB::raw('NOW()'),
196
	        	'updated_at' => \DB::raw('NOW()')
197
	        	],
198
        	]
199
        );
200
201
		/**
202
		 * Delete previous data.
203
		 */
204
		$adminGroup   = DB::table('groups')->where('name', 'Admin')->first();
205
		$adminUser    = DB::table('users')->where('email', '[email protected]')->first();
206
		$adminGroupId = $adminGroup ? DB::table('groups')->where('name', 'Admin')->first()->id : 0;
207
		$adminUserId  = $adminUser ? DB::table('users')->where('email', '[email protected]')->first()->id : 0;
208
		DB::table('users_groups')->where('user_id', $adminUserId)->where('group_id', $adminGroupId)->delete();
209
		DB::table('users')->where('email', '[email protected]')->delete();
210
		DB::table('groups')->where('name', 'admin')->delete();
211
212
		/**
213
		 * Create Default groups.
214
		 */
215
		$adminGroupId = DB::table('groups')->insertGetId(
216
			[
217
			'name'       => 'Admin',
218
			'created_at' => \DB::raw('NOW()'),
219
			'updated_at' => \DB::raw('NOW()')
220
			]
221
		);
222
223
		/**
224
		 * Create Default users.
225
		 */
226
		$adminUserId = DB::table('users')->insertGetId(
227
            [
228
			'email'      => '[email protected]',
229
			'password'   => bcrypt('123456'),
230
			'created_at' => \DB::raw('NOW()'),
231
			'updated_at' => \DB::raw('NOW()')
232
			]
233
        );
234
235
		/**
236
		 * Assign users to groups.
237
		 */
238
		DB::table('users_groups')->insert(
239
        	[
240
	            [
241
				'user_id'    => $adminUserId,
242
				'group_id'   => $adminGroupId,
243
				'created_at' => \DB::raw('NOW()'),
244
				'updated_at' => \DB::raw('NOW()')
245
	            ]
246
        	]
247
        );
248
249
        /**
250
		 * Assign the permissions to the admin group.
251
		 */
252
		$permissionIds = DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->select('id')->lists('id');
253
		$adminGroupId  = DB::table('groups')->where('name', 'Admin')->first()->id;
254
		foreach ($permissionIds as $permissionId) 
255
		{
256
			DB::table('groups_permissions')->insert(
257
				[
258
				'permission_id' => $permissionId,
259
				'group_id'      => $adminGroupId,
260
				'created_at'    => \DB::raw('NOW()'),
261
				'updated_at'    => \DB::raw('NOW()')
262
				]
263
			);
264
		}
265
	}
266
267
	/**
268
	 * Reverse the migrations.
269
	 *
270
	 * @return void
271
	 */
272
	public function down()
273
	{
274
		$adminGroupId = DB::table('groups')->where('name', 'Admin')->first()->id;
275
		$adminUserId  = DB::table('users')->where('email', '[email protected]')->first()->id;
276
277
		DB::table('permissions')->whereIn('model', ['users', 'permissions', 'groups'])->delete();
278
		DB::table('users_groups')->where('user_id', $adminUserId)->where('group_id', $adminGroupId)->delete();
279
		DB::table('users')->where('email', '[email protected]')->delete();
280
		DB::table('groups')->where('name', 'admin')->delete();
281
	}
282
}