From 59d07f0d90889dec0c54f63f9fa4cbeefb1f9181 Mon Sep 17 00:00:00 2001 From: Leandro Date: Tue, 5 May 2020 12:01:07 -0300 Subject: [PATCH] Added timestamps to pivot tables --- ..._12_200000_create_authorization_tables.php | 86 +++++++++++++------ src/Traits/HasPermissions.php | 2 +- src/Traits/HasRoles.php | 2 +- 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/database/migrations/2014_10_12_200000_create_authorization_tables.php b/database/migrations/2014_10_12_200000_create_authorization_tables.php index 18c2bc6..310181a 100644 --- a/database/migrations/2014_10_12_200000_create_authorization_tables.php +++ b/database/migrations/2014_10_12_200000_create_authorization_tables.php @@ -4,49 +4,80 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateAuthorizationTables extends Migration -{ +class CreateAuthorizationTables extends Migration { /** * Run the migrations. * * @return void */ - public function up() - { - Schema::create('roles', function (Blueprint $table) { - $table->increments('id'); + public function up() { + Schema::create('roles', function(Blueprint $table) { + $table->bigIncrements('id'); $table->timestamps(); - $table->string('name'); - $table->string('label')->nullable(); + $table->string('name')->unique(); + $table->string('label'); + $table->boolean('protected')->default(false); }); - Schema::create('permissions', function (Blueprint $table) { - $table->increments('id'); + Schema::create('permissions', function(Blueprint $table) { + $table->bigIncrements('id'); $table->timestamps(); - $table->string('name'); - $table->string('label')->nullable(); + $table->string('name')->unique(); + $table->string('label'); + $table->string('group'); }); - Schema::create('permission_role', function (Blueprint $table) { - $table->unsignedInteger('permission_id'); - $table->unsignedInteger('role_id'); + Schema::create('permission_role', function(Blueprint $table) { + $table->unsignedBigInteger('permission_id'); + $table->unsignedBigInteger('role_id'); + $table->timestamps(); $table->primary(['permission_id', 'role_id']); + $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); + $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade'); }); - Schema::create('permission_user', function (Blueprint $table) { - $table->unsignedInteger('permission_id'); - $table->unsignedInteger('user_id'); + Schema::create('user_role', function(Blueprint $table) { + $table->unsignedBigInteger('role_id'); + $table->unsignedBigInteger('user_id'); + $table->timestamps(); + + $table->primary(['role_id', 'user_id']); + $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users'); + }); + + Schema::create('user_permission', function(Blueprint $table) { + $table->unsignedBigInteger('permission_id'); + $table->unsignedBigInteger('user_id'); + $table->timestamps(); $table->primary(['permission_id', 'user_id']); + $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade'); + $table->foreign('user_id')->references('id')->on('users'); }); - Schema::create('role_user', function (Blueprint $table) { - $table->unsignedInteger('role_id'); - $table->unsignedInteger('user_id'); +/* + Schema::create('admin_user_role', function(Blueprint $table) { + $table->unsignedBigInteger('role_id'); + $table->unsignedBigInteger('admin_user_id'); + $table->timestamps(); - $table->primary(['role_id', 'user_id']); + $table->primary(['role_id', 'admin_user_id']); + $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); + $table->foreign('admin_user_id')->references('id')->on('admin_users'); }); + + Schema::create('admin_user_permission', function(Blueprint $table) { + $table->unsignedBigInteger('permission_id'); + $table->unsignedBigInteger('admin_user_id'); + $table->timestamps(); + + $table->primary(['permission_id', 'admin_user_id']); + $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade'); + $table->foreign('admin_user_id')->references('id')->on('admin_users'); + });*/ + } /** @@ -54,12 +85,13 @@ public function up() * * @return void */ - public function down() - { - Schema::dropIfExists('role_user'); - Schema::dropIfExists('permission_role'); - Schema::dropIfExists('permission_user'); + public function down() { +// Schema::dropIfExists('admin_user_permission'); +// Schema::dropIfExists('admin_user_role'); + Schema::dropIfExists('user_permission'); + Schema::dropIfExists('user_role'); Schema::dropIfExists('permissions'); Schema::dropIfExists('roles'); + } } diff --git a/src/Traits/HasPermissions.php b/src/Traits/HasPermissions.php index 8810609..6997668 100644 --- a/src/Traits/HasPermissions.php +++ b/src/Traits/HasPermissions.php @@ -13,6 +13,6 @@ trait HasPermissions */ public function permissions() { - return $this->belongsToMany(Authorization::permissionModel()); + return $this->belongsToMany(Authorization::permissionModel())->withTimestamps(); } } diff --git a/src/Traits/HasRoles.php b/src/Traits/HasRoles.php index fbeba46..61d62cf 100644 --- a/src/Traits/HasRoles.php +++ b/src/Traits/HasRoles.php @@ -13,6 +13,6 @@ trait HasRoles */ public function roles() { - return $this->belongsToMany(Authorization::roleModel()); + return $this->belongsToMany(Authorization::roleModel())->withTimestamps(); } }