1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Database\Schema\Blueprint; |
4
|
|
|
use Illuminate\Database\Migrations\Migration; |
5
|
|
|
|
6
|
|
|
class RbacSetupTables extends Migration |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
private $userTable; |
9
|
|
|
private $userForeignKey; |
10
|
|
|
|
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$provider = config('auth.guards.' . config('admin.guard') . '.provider'); |
14
|
|
|
$userModelName = config("auth.providers.{$provider}.model"); |
15
|
|
|
$userModel = new $userModelName(); |
16
|
|
|
$this->userTable = $userModel->getTable(); |
17
|
|
|
$this->userForeignKey = $userModel->getForeignKey(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Run the migrations. |
22
|
|
|
* |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function up() |
26
|
|
|
{ |
27
|
|
|
// Create table for admin user |
28
|
|
|
if ($this->userTable != 'users') { |
29
|
|
View Code Duplication |
Schema::create($this->userTable, function (Blueprint $table) { |
|
|
|
|
30
|
|
|
$table->engine = "InnoDB COMMENT='管理员表'"; |
31
|
|
|
$table->increments('id'); |
32
|
|
|
$table->string('name')->unique()->comment('名称'); |
33
|
|
|
$table->string('email')->nullable()->unique()->comment('邮箱'); |
34
|
|
|
$table->string('password')->nullable()->comment('密码'); |
35
|
|
|
$table->rememberToken(); |
36
|
|
|
$table->timestamps(); |
37
|
|
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Create table for storing roles |
41
|
|
View Code Duplication |
Schema::create('roles', function (Blueprint $table) { |
|
|
|
|
42
|
|
|
$table->engine = "InnoDB COMMENT='角色表'"; |
43
|
|
|
$table->increments('id'); |
44
|
|
|
$table->string('name')->unique()->comment('名称'); |
45
|
|
|
$table->string('display_name')->nullable()->comment('显示名'); |
46
|
|
|
$table->string('description')->nullable()->comment('备注'); |
47
|
|
|
$table->timestamps(); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
// Create table for associating roles to users (Many-to-Many) |
51
|
|
|
Schema::create('role_user', function (Blueprint $table) { |
52
|
|
|
$table->engine = "InnoDB COMMENT='角色与用户对应表'"; |
53
|
|
|
$table->integer($this->userForeignKey)->unsigned()->comment('管理员ID'); |
54
|
|
|
$table->integer('role_id')->unsigned()->comment('角色ID'); |
55
|
|
|
|
56
|
|
|
/*$table->foreign('uid')->references('id')->on('users') |
|
|
|
|
57
|
|
|
->onUpdate('cascade')->onDelete('cascade'); |
58
|
|
|
$table->foreign('role_id')->references('id')->on('roles') |
59
|
|
|
->onUpdate('cascade')->onDelete('cascade');*/ |
60
|
|
|
|
61
|
|
|
$table->primary([$this->userForeignKey, 'role_id']); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
// Create table for storing permissions |
65
|
|
|
Schema::create('permissions', function (Blueprint $table) { |
66
|
|
|
$table->engine = "InnoDB COMMENT='权限(即菜单)表'"; |
67
|
|
|
$table->increments('id')->comment('主键'); |
68
|
|
|
$table->integer('pid')->comment('父ID'); |
69
|
|
|
$table->string('icon', 100)->comment('图标class'); |
70
|
|
|
$table->string('display_name', 200)->comment('显示名称'); |
71
|
|
|
$table->string('name', 100)->comment('名称'); |
72
|
|
|
$table->tinyInteger('is_menu')->comment('是否作为菜单')->default('1'); |
73
|
|
|
$table->tinyInteger('sort')->unsigned()->comment('排序'); |
74
|
|
|
$table->string('description')->nullable()->comment('描述'); |
75
|
|
|
$table->timestamps(); |
76
|
|
|
$table->index('pid'); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
// Create table for associating permissions to roles (Many-to-Many) |
80
|
|
|
Schema::create('permission_role', function (Blueprint $table) { |
81
|
|
|
$table->engine = "InnoDB COMMENT='权限与角色对应表'"; |
82
|
|
|
$table->integer('permission_id')->unsigned()->comment('权限ID'); |
83
|
|
|
$table->integer('role_id')->unsigned()->comment('角色ID'); |
84
|
|
|
|
85
|
|
|
$table->primary(['permission_id', 'role_id']); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Reverse the migrations. |
91
|
|
|
* |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
public function down() |
95
|
|
|
{ |
96
|
|
|
Schema::drop('permission_role'); |
97
|
|
|
Schema::drop('permissions'); |
98
|
|
|
Schema::drop('role_user'); |
99
|
|
|
Schema::drop('roles'); |
100
|
|
|
if ($this->userTable != 'users') { |
101
|
|
|
Schema::drop($this->userTable); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.