1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
|
7
|
|
|
class CreatePermissionTables 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
|
|
|
if (empty($tableNames)) { |
20
|
|
|
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding.'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
Schema::create($tableNames['permissions'], function (Blueprint $table) { |
24
|
|
|
$table->bigIncrements('id'); |
25
|
|
|
$table->uuid('uuid')->default('-'); |
26
|
|
|
$table->string('name'); |
27
|
|
|
$table->string('guard_name'); |
28
|
|
|
$table->timestamps(); |
29
|
|
|
}); |
30
|
|
|
|
31
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) { |
32
|
|
|
$table->bigIncrements('id'); |
33
|
|
|
$table->uuid('uuid')->default('-'); |
34
|
|
|
$table->string('name'); |
35
|
|
|
$table->string('guard_name'); |
36
|
|
|
$table->timestamps(); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) { |
40
|
|
|
$table->unsignedBigInteger('permission_id'); |
41
|
|
|
|
42
|
|
|
$table->string('model_type'); |
43
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
44
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); |
45
|
|
|
|
46
|
|
|
$table->foreign('permission_id') |
47
|
|
|
->references('id') |
48
|
|
|
->on($tableNames['permissions']) |
49
|
|
|
->onDelete('cascade'); |
50
|
|
|
|
51
|
|
|
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'], |
52
|
|
|
'model_has_permissions_permission_model_type_primary'); |
53
|
|
|
}); |
54
|
|
|
|
55
|
|
|
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) { |
56
|
|
|
$table->unsignedBigInteger('role_id'); |
57
|
|
|
|
58
|
|
|
$table->string('model_type'); |
59
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
60
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); |
61
|
|
|
|
62
|
|
|
$table->foreign('role_id') |
63
|
|
|
->references('id') |
64
|
|
|
->on($tableNames['roles']) |
65
|
|
|
->onDelete('cascade'); |
66
|
|
|
|
67
|
|
|
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'], |
68
|
|
|
'model_has_roles_role_model_type_primary'); |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
72
|
|
|
$table->unsignedBigInteger('permission_id'); |
73
|
|
|
$table->unsignedBigInteger('role_id'); |
74
|
|
|
|
75
|
|
|
$table->foreign('permission_id') |
76
|
|
|
->references('id') |
77
|
|
|
->on($tableNames['permissions']) |
78
|
|
|
->onDelete('cascade'); |
79
|
|
|
|
80
|
|
|
$table->foreign('role_id') |
81
|
|
|
->references('id') |
82
|
|
|
->on($tableNames['roles']) |
83
|
|
|
->onDelete('cascade'); |
84
|
|
|
|
85
|
|
|
$table->primary(['permission_id', 'role_id'], 'role_has_permissions_permission_id_role_id_primary'); |
86
|
|
|
}); |
87
|
|
|
|
88
|
|
|
app('cache') |
89
|
|
|
->store('default' != config('permission.cache.store') ? config('permission.cache.store') : null) |
90
|
|
|
->forget(config('permission.cache.key')); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Reverse the migrations. |
95
|
|
|
* |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
|
|
public function down() |
99
|
|
|
{ |
100
|
|
|
$tableNames = config('permission.table_names'); |
101
|
|
|
|
102
|
|
|
if (empty($tableNames)) { |
103
|
|
|
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
107
|
|
|
Schema::drop($tableNames['model_has_roles']); |
108
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
109
|
|
|
Schema::drop($tableNames['roles']); |
110
|
|
|
Schema::drop($tableNames['permissions']); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|