|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Enea\Authorization\Support\Config; |
|
4
|
|
|
use Illuminate\Database\Migrations\Migration; |
|
5
|
|
|
use Illuminate\Database\Schema\Blueprint; |
|
6
|
|
|
use Illuminate\Support\Facades\Schema; |
|
7
|
|
|
|
|
8
|
|
|
class CreateLaravelAuthorizationTables extends Migration |
|
9
|
|
|
{ |
|
10
|
|
|
public function up(): void |
|
11
|
|
|
{ |
|
12
|
|
|
Schema::create(Config::roleTableName(), $this->getGrantableStructure()); |
|
13
|
|
|
Schema::create(Config::permissionTableName(), $this->getGrantableStructure()); |
|
14
|
|
|
Schema::create(Config::userRoleTableName(), $this->getAuthorizationsStructure(Config::roleTableName())); |
|
15
|
|
|
Schema::create(Config::userPermissionTableName(), $this->getAuthorizationsStructure(Config::permissionTableName())); |
|
16
|
|
|
|
|
17
|
|
|
Schema::create(Config::rolePermissionTableName(), function (Blueprint $table): void { |
|
18
|
|
|
$table->unsignedInteger('role_id'); |
|
19
|
|
|
$table->foreign('role_id')->references('id')->on(Config::roleTableName()); |
|
20
|
|
|
|
|
21
|
|
|
$table->unsignedInteger('permission_id'); |
|
22
|
|
|
$table->foreign('permission_id')->references('id')->on(Config::permissionTableName()); |
|
23
|
|
|
|
|
24
|
|
|
$table->primary(['role_id', 'permission_id']); |
|
25
|
|
|
|
|
26
|
|
|
$table->timestamps(); |
|
27
|
|
|
}); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function down(): void |
|
31
|
|
|
{ |
|
32
|
|
|
Schema::dropIfExists(Config::rolePermissionTableName()); |
|
33
|
|
|
Schema::dropIfExists(Config::userPermissionTableName()); |
|
34
|
|
|
Schema::dropIfExists(Config::userRoleTableName()); |
|
35
|
|
|
Schema::dropIfExists(Config::permissionTableName()); |
|
36
|
|
|
Schema::dropIfExists(Config::roleTableName()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
private function getGrantableStructure(): Closure |
|
40
|
|
|
{ |
|
41
|
|
|
return function (Blueprint $table): void { |
|
42
|
|
|
$table->increments('id'); |
|
43
|
|
|
$table->string('secret_name', 60)->unique(); |
|
44
|
|
|
$table->string('display_name', 60); |
|
45
|
|
|
$table->string('description')->nullable(); |
|
46
|
|
|
$table->timestamps(); |
|
47
|
|
|
}; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
private function getAuthorizationsStructure($tableName): Closure |
|
51
|
|
|
{ |
|
52
|
|
|
return function (Blueprint $table) use ($tableName): void { |
|
53
|
|
|
$table->increments('id'); |
|
54
|
|
|
|
|
55
|
|
|
$table->unsignedInteger('authorizable_id'); |
|
56
|
|
|
$table->string('authorizable_type', 100); |
|
57
|
|
|
$table->index(['authorizable_id', 'authorizable_type']); |
|
58
|
|
|
|
|
59
|
|
|
$name = str_singular($tableName); |
|
60
|
|
|
$table->unsignedInteger("{$name}_id"); |
|
61
|
|
|
$table->foreign("{$name}_id")->references('id')->on($tableName); |
|
62
|
|
|
|
|
63
|
|
|
$table->timestamps(); |
|
64
|
|
|
}; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|