1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
|
7
|
|
|
class CreateAuthorizationTables extends Migration |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Run the migrations. |
11
|
|
|
* |
12
|
|
|
* @return void |
13
|
|
|
*/ |
14
|
|
|
public function up() |
15
|
|
|
{ |
16
|
|
|
$tableNames = config('permission.table_names'); |
17
|
|
|
$columnNames = config('permission.column_names'); |
18
|
|
|
|
19
|
|
|
Schema::create($tableNames['permissions'], function (Blueprint $table) { |
20
|
|
|
$table->increments('id'); |
21
|
|
|
$table->string('name'); |
22
|
|
|
$table->string('guard_name'); |
23
|
|
|
$table->timestamps(); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) { |
27
|
|
|
$table->increments('id'); |
28
|
|
|
$table->string('name'); |
29
|
|
|
$table->string('guard_name'); |
30
|
|
|
$table->timestamps(); |
31
|
|
|
}); |
32
|
|
|
|
33
|
|
|
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) { |
34
|
|
|
$table->unsignedInteger('permission_id'); |
35
|
|
|
|
36
|
|
|
$table->string('model_type'); |
37
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
38
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type']); |
39
|
|
|
|
40
|
|
|
$table->foreign('permission_id') |
41
|
|
|
->references('id') |
42
|
|
|
->on($tableNames['permissions']) |
43
|
|
|
->onDelete('cascade'); |
44
|
|
|
|
45
|
|
|
$table->primary( |
46
|
|
|
['permission_id', $columnNames['model_morph_key'], 'model_type'], |
47
|
|
|
'model_has_permissions_permission_model_type_primary' |
48
|
|
|
); |
49
|
|
|
}); |
50
|
|
|
|
51
|
|
|
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) { |
52
|
|
|
$table->unsignedInteger('role_id'); |
53
|
|
|
|
54
|
|
|
$table->string('model_type'); |
55
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
56
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type']); |
57
|
|
|
|
58
|
|
|
$table->foreign('role_id') |
59
|
|
|
->references('id') |
60
|
|
|
->on($tableNames['roles']) |
61
|
|
|
->onDelete('cascade'); |
62
|
|
|
|
63
|
|
|
$table->primary( |
64
|
|
|
['role_id', $columnNames['model_morph_key'], 'model_type'], |
65
|
|
|
'model_has_roles_role_model_type_primary' |
66
|
|
|
); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
70
|
|
|
$table->unsignedInteger('permission_id'); |
71
|
|
|
$table->unsignedInteger('role_id'); |
72
|
|
|
|
73
|
|
|
$table->foreign('permission_id') |
74
|
|
|
->references('id') |
75
|
|
|
->on($tableNames['permissions']) |
76
|
|
|
->onDelete('cascade'); |
77
|
|
|
|
78
|
|
|
$table->foreign('role_id') |
79
|
|
|
->references('id') |
80
|
|
|
->on($tableNames['roles']) |
81
|
|
|
->onDelete('cascade'); |
82
|
|
|
|
83
|
|
|
$table->primary(['permission_id', 'role_id']); |
84
|
|
|
|
85
|
|
|
app('cache')->forget('spatie.permission.cache'); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Reverse the migrations. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function down() |
95
|
|
|
{ |
96
|
|
|
$tableNames = config('permission.table_names'); |
97
|
|
|
|
98
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
99
|
|
|
Schema::drop($tableNames['model_has_roles']); |
100
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
101
|
|
|
Schema::drop($tableNames['roles']); |
102
|
|
|
Schema::drop($tableNames['permissions']); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|