1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Schema; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Database\Migrations\Migration; |
6
|
|
|
use Spatie\Permission\PermissionRegistrar; |
7
|
|
|
|
8
|
|
|
class CreatePermissionTables extends Migration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Run the migrations. |
12
|
|
|
* |
13
|
|
|
* @return void |
14
|
|
|
*/ |
15
|
|
|
public function up() |
16
|
|
|
{ |
17
|
|
|
$tableNames = config('permission.table_names'); |
18
|
|
|
$columnNames = config('permission.column_names'); |
19
|
|
|
$teams = config('permission.teams'); |
20
|
|
|
|
21
|
|
|
if (empty($tableNames)) { |
22
|
|
|
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.'); |
23
|
|
|
} |
24
|
|
|
if ($teams && empty($columnNames['team_foreign_key'] ?? null)) { |
25
|
|
|
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
Schema::create($tableNames['permissions'], function (Blueprint $table) { |
29
|
|
|
$table->bigIncrements('id'); |
30
|
|
|
$table->string('name'); // For MySQL 8.0 use string('name', 125); |
31
|
|
|
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); |
32
|
|
|
$table->timestamps(); |
33
|
|
|
|
34
|
|
|
$table->unique(['name', 'guard_name']); |
35
|
|
|
}); |
36
|
|
|
|
37
|
|
|
Schema::create($tableNames['roles'], function (Blueprint $table) use ($teams, $columnNames) { |
38
|
|
|
$table->bigIncrements('id'); |
39
|
|
|
if ($teams || config('permission.testing')) { // permission.testing is a fix for sqlite testing |
40
|
|
|
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable(); |
41
|
|
|
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index'); |
42
|
|
|
} |
43
|
|
|
$table->string('name'); // For MySQL 8.0 use string('name', 125); |
44
|
|
|
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125); |
45
|
|
|
$table->timestamps(); |
46
|
|
|
if ($teams || config('permission.testing')) { |
47
|
|
|
$table->unique([$columnNames['team_foreign_key'], 'name', 'guard_name']); |
48
|
|
|
} else { |
49
|
|
|
$table->unique(['name', 'guard_name']); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
Schema::create($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) { |
54
|
|
|
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); |
55
|
|
|
|
56
|
|
|
$table->string('model_type'); |
57
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
58
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_permissions_model_id_model_type_index'); |
59
|
|
|
|
60
|
|
|
$table->foreign(PermissionRegistrar::$pivotPermission) |
61
|
|
|
->references('id') |
62
|
|
|
->on($tableNames['permissions']) |
63
|
|
|
->onDelete('cascade'); |
64
|
|
|
if ($teams) { |
65
|
|
|
$table->unsignedBigInteger($columnNames['team_foreign_key']); |
66
|
|
|
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index'); |
67
|
|
|
|
68
|
|
|
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], |
69
|
|
|
'model_has_permissions_permission_model_type_primary'); |
70
|
|
|
} else { |
71
|
|
|
$table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'], |
72
|
|
|
'model_has_permissions_permission_model_type_primary'); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) { |
78
|
|
|
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole); |
79
|
|
|
|
80
|
|
|
$table->string('model_type'); |
81
|
|
|
$table->unsignedBigInteger($columnNames['model_morph_key']); |
82
|
|
|
$table->index([$columnNames['model_morph_key'], 'model_type'], 'model_has_roles_model_id_model_type_index'); |
83
|
|
|
|
84
|
|
|
$table->foreign(PermissionRegistrar::$pivotRole) |
85
|
|
|
->references('id') |
86
|
|
|
->on($tableNames['roles']) |
87
|
|
|
->onDelete('cascade'); |
88
|
|
|
if ($teams) { |
89
|
|
|
$table->unsignedBigInteger($columnNames['team_foreign_key']); |
90
|
|
|
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index'); |
91
|
|
|
|
92
|
|
|
$table->primary([$columnNames['team_foreign_key'], PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], |
93
|
|
|
'model_has_roles_role_model_type_primary'); |
94
|
|
|
} else { |
95
|
|
|
$table->primary([PermissionRegistrar::$pivotRole, $columnNames['model_morph_key'], 'model_type'], |
96
|
|
|
'model_has_roles_role_model_type_primary'); |
97
|
|
|
} |
98
|
|
|
}); |
99
|
|
|
|
100
|
|
|
Schema::create($tableNames['role_has_permissions'], function (Blueprint $table) use ($tableNames) { |
101
|
|
|
$table->unsignedBigInteger(PermissionRegistrar::$pivotPermission); |
102
|
|
|
$table->unsignedBigInteger(PermissionRegistrar::$pivotRole); |
103
|
|
|
|
104
|
|
|
$table->foreign(PermissionRegistrar::$pivotPermission) |
105
|
|
|
->references('id') |
106
|
|
|
->on($tableNames['permissions']) |
107
|
|
|
->onDelete('cascade'); |
108
|
|
|
|
109
|
|
|
$table->foreign(PermissionRegistrar::$pivotRole) |
110
|
|
|
->references('id') |
111
|
|
|
->on($tableNames['roles']) |
112
|
|
|
->onDelete('cascade'); |
113
|
|
|
|
114
|
|
|
$table->primary([PermissionRegistrar::$pivotPermission, PermissionRegistrar::$pivotRole], 'role_has_permissions_permission_id_role_id_primary'); |
115
|
|
|
}); |
116
|
|
|
|
117
|
|
|
app('cache') |
118
|
|
|
->store(config('permission.cache.store') != 'default' ? config('permission.cache.store') : null) |
119
|
|
|
->forget(config('permission.cache.key')); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Reverse the migrations. |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function down() |
128
|
|
|
{ |
129
|
|
|
$tableNames = config('permission.table_names'); |
130
|
|
|
|
131
|
|
|
if (empty($tableNames)) { |
132
|
|
|
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.'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
Schema::drop($tableNames['role_has_permissions']); |
136
|
|
|
Schema::drop($tableNames['model_has_roles']); |
137
|
|
|
Schema::drop($tableNames['model_has_permissions']); |
138
|
|
|
Schema::drop($tableNames['roles']); |
139
|
|
|
Schema::drop($tableNames['permissions']); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|