1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Migrations\Migration; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
|
7
|
|
|
class CreateCmsPermissionTables 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->string('name'); |
26
|
|
|
$table->string('guard_name'); |
27
|
|
|
$table->timestamps(); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) { |
31
|
|
|
$table->bigIncrements('id'); |
32
|
|
|
$table->string('name'); |
33
|
|
|
$table->string('guard_name'); |
34
|
|
|
$table->timestamps(); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
View Code Duplication |
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) { |
|
|
|
|
38
|
|
|
$table->unsignedBigInteger('permission_id'); |
39
|
|
|
|
40
|
|
|
$table->string('model_type'); |
41
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
42
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); |
43
|
|
|
|
44
|
|
|
$table->foreign('permission_id') |
45
|
|
|
->references('id') |
46
|
|
|
->on($tableNames['permissions']) |
47
|
|
|
->onDelete('cascade'); |
48
|
|
|
|
49
|
|
|
$table->primary(['permission_id', $columnNames['model_morph_key'], 'model_type'], |
50
|
|
|
'model_has_permissions_permission_model_type_primary'); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
View Code Duplication |
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) { |
|
|
|
|
54
|
|
|
$table->unsignedBigInteger('role_id'); |
55
|
|
|
|
56
|
|
|
$table->string('model_type'); |
57
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
58
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); |
59
|
|
|
|
60
|
|
|
$table->foreign('role_id') |
61
|
|
|
->references('id') |
62
|
|
|
->on($tableNames['roles']) |
63
|
|
|
->onDelete('cascade'); |
64
|
|
|
|
65
|
|
|
$table->primary(['role_id', $columnNames['model_morph_key'], 'model_type'], |
66
|
|
|
'model_has_roles_role_model_type_primary'); |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
70
|
|
|
$table->unsignedBigInteger('permission_id'); |
71
|
|
|
$table->unsignedBigInteger('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'], 'role_has_permissions_permission_id_role_id_primary'); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
app('cache') |
87
|
|
|
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) |
88
|
|
|
->forget(config('permission.cache.key')); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Reverse the migrations. |
93
|
|
|
* |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function down() |
97
|
|
|
{ |
98
|
|
|
$tableNames = config('permission.table_names'); |
99
|
|
|
|
100
|
|
|
if (empty($tableNames)) { |
101
|
|
|
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.'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
105
|
|
|
Schema::drop($tableNames['model_has_roles']); |
106
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
107
|
|
|
Schema::drop($tableNames['roles']); |
108
|
|
|
Schema::drop($tableNames['permissions']); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.