From 4bba187034400b5a2b4bf351f4b3aec26513ab38 Mon Sep 17 00:00:00 2001 From: Keania Eric Date: Wed, 18 Oct 2023 18:29:09 +0100 Subject: [PATCH] added fix to configure twill permissions and role tables --- config/twill.php | 2 ++ .../2020_04_20_000001_support_permission.php | 26 ++++++++++++------- src/Models/Permission.php | 7 +++++ src/Models/Role.php | 7 +++++ 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/config/twill.php b/config/twill.php index 26644077e..14531b167 100644 --- a/config/twill.php +++ b/config/twill.php @@ -143,6 +143,8 @@ 'tags_table' => 'twill_tags', 'users_oauth_table' => 'twill_users_oauth', 'users_table' => 'twill_users', + 'permissions_table'=> 'permissions', + 'roles_table'=> 'roles', /* |-------------------------------------------------------------------------- diff --git a/migrations/optional/permissions-management/2020_04_20_000001_support_permission.php b/migrations/optional/permissions-management/2020_04_20_000001_support_permission.php index 8faf8e869..29e120bef 100644 --- a/migrations/optional/permissions-management/2020_04_20_000001_support_permission.php +++ b/migrations/optional/permissions-management/2020_04_20_000001_support_permission.php @@ -16,15 +16,18 @@ class SupportPermission extends Migration */ public function up() { - if (!Schema::hasTable('permissions') + $permissionTableName = config('twill.permissions_table', 'permissions'); + $roleTableName = config('twill.roles_table', 'roles'); + + if (!Schema::hasTable($permissionTableName) && !Schema::hasTable('groups') - && !Schema::hasTable('roles') + && !Schema::hasTable($roleTableName) && !Schema::hasTable('permission_twill_user') && !Schema::hasTable('group_twill_user') && !Schema::hasTable('group_permission') && !Schema::hasTable('permission_role') ) { - Schema::create('permissions', function (Blueprint $table) { + Schema::create($permissionTableName, function (Blueprint $table) { createDefaultTableFields($table); $table->string('name'); $table->string('display_name')->nullable(); @@ -40,7 +43,7 @@ public function up() $table->boolean('is_everyone_group')->default(false); }); - Schema::create('roles', function (Blueprint $table) { + Schema::create($roleTableName, function (Blueprint $table) { createDefaultTableFields($table); $table->string('name', 255)->nullable(); $table->boolean('in_everyone_group')->default(true); @@ -57,7 +60,7 @@ public function up() $table->bigInteger('permission_id')->unsigned()->nullable(); $table->foreign('permission_id') ->references('id') - ->on('permissions') + ->on($permissionTableName) ->onDelete('cascade'); }); @@ -81,7 +84,7 @@ public function up() $table->bigInteger('permission_id')->unsigned()->nullable(); $table->foreign('permission_id') ->references('id') - ->on('permissions') + ->on($permissionTableName) ->onDelete('cascade'); $table->bigInteger('group_id')->unsigned()->nullable(); @@ -95,13 +98,13 @@ public function up() $table->bigInteger('permission_id')->unsigned()->nullable(); $table->foreign('permission_id') ->references('id') - ->on('permissions') + ->on($permissionTableName) ->onDelete('cascade'); $table->bigInteger('role_id')->unsigned()->nullable(); $table->foreign('role_id') ->references('id') - ->on('roles') + ->on($roleTableName) ->onDelete('cascade'); }); @@ -118,13 +121,16 @@ public function up() */ public function down() { + $permissionTableName = config('twill.permissions_table', 'permissions'); + $roleTableName = config('twill.roles_table', 'roles'); + Schema::dropIfExists('permission_twill_user'); Schema::dropIfExists('group_twill_user'); Schema::dropIfExists('group_permission'); Schema::dropIfExists('permission_role'); - Schema::dropIfExists('permissions'); + Schema::dropIfExists($permissionTableName); Schema::dropIfExists('groups'); - Schema::dropIfExists('roles'); + Schema::dropIfExists($roleTableName); } private function seedBasicPermissions() diff --git a/src/Models/Permission.php b/src/Models/Permission.php index 9c34b6ea5..7bfded9f0 100644 --- a/src/Models/Permission.php +++ b/src/Models/Permission.php @@ -44,6 +44,13 @@ class Permission extends BaseModel protected $appends = ['permissionable_module']; + public function __construct(array $attributes = []) + { + $this->table = config('twill.permissions_table', 'permissions'); + + parent::__construct($attributes); + } + /** * Return an array of permission names that belongs to * a certain scope (global, module or item). diff --git a/src/Models/Role.php b/src/Models/Role.php index 343256eb4..36e3ed534 100644 --- a/src/Models/Role.php +++ b/src/Models/Role.php @@ -38,6 +38,13 @@ class Role extends BaseModel implements Sortable, TwillModelContract 'deleted_at' => 'datetime' ]; + public function __construct(array $attributes = []) + { + $this->table = config('twill.roles_table', 'roles'); + + parent::__construct($attributes); + } + public function scopeAccessible($query): Builder { $currentUser = auth('twill_users')->user();