1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Illuminate\Support\Facades\Schema; |
4
|
|
|
use Illuminate\Database\Schema\Blueprint; |
5
|
|
|
use Illuminate\Database\Migrations\Migration; |
6
|
|
|
use Itstructure\LaRbac\Helpers\Helper; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class CreateUserRoleTable |
10
|
|
|
* |
11
|
|
|
* @author Andrey Girnik <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class CreateUserRoleTable extends Migration |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $userModelClass; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* UserController constructor. |
22
|
|
|
*/ |
23
|
|
|
public function __construct() |
24
|
|
|
{ |
25
|
|
|
$this->userModelClass = config('rbac.userModelClass'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Run the migrations. |
30
|
|
|
* @throws Exception |
31
|
|
|
* @throws \Exception |
32
|
|
|
*/ |
33
|
|
|
public function up() |
34
|
|
|
{ |
35
|
|
|
Helper::checkUserModel($this->userModelClass); |
36
|
|
|
|
37
|
|
|
/* @var Illuminate\Foundation\Auth\User $userNewModel */ |
38
|
|
|
$userNewModel = new $this->userModelClass(); |
39
|
|
|
|
40
|
|
|
Helper::checkUserModelKeyType($userNewModel); |
41
|
|
|
|
42
|
|
|
$userModelTable = $userNewModel->getTable(); |
43
|
|
|
|
44
|
|
|
$userModelKeyName = $userNewModel->getAuthIdentifierName(); |
45
|
|
|
|
46
|
|
|
$userTablePrimaryType = Schema::getConnection()->getSchemaBuilder()->getColumnType($userModelTable, $userModelKeyName); |
47
|
|
|
|
48
|
|
|
Helper::checkUserTablePrimaryType($userTablePrimaryType, $userModelKeyName, $userModelTable); |
49
|
|
|
|
50
|
|
|
Schema::create('user_role', function (Blueprint $table) use ($userModelKeyName, $userModelTable, $userTablePrimaryType) { |
51
|
|
|
switch ($userTablePrimaryType) { |
52
|
|
|
case 'bigint': |
53
|
|
|
$table->unsignedBigInteger('user_id'); |
54
|
|
|
break; |
55
|
|
|
case 'integer': |
56
|
|
|
$table->unsignedInteger('user_id'); |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
$table->unsignedInteger('role_id'); |
60
|
|
|
$table->timestamps(); |
61
|
|
|
$table->unique(['user_id','role_id']); |
62
|
|
|
$table->foreign('user_id')->references($userModelKeyName)->on($userModelTable)->onDelete('cascade'); |
63
|
|
|
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); |
64
|
|
|
}); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Reverse the migrations. |
69
|
|
|
* @return void |
70
|
|
|
*/ |
71
|
|
|
public function down() |
72
|
|
|
{ |
73
|
|
|
Schema::dropIfExists('user_role'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|