Skip to content

Commit

Permalink
added fix to configure twill permissions and role tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Keania Eric authored and Keania Eric committed Oct 18, 2023
1 parent 35d0bbc commit 4bba187
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/twill.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@
'tags_table' => 'twill_tags',
'users_oauth_table' => 'twill_users_oauth',
'users_table' => 'twill_users',
'permissions_table'=> 'permissions',
'roles_table'=> 'roles',

/*
|--------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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');
});

Expand All @@ -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();
Expand All @@ -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');
});

Expand All @@ -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()
Expand Down
7 changes: 7 additions & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
7 changes: 7 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 4bba187

Please sign in to comment.