Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure permissions and roles table names #2350

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
$permissionsTableName = config('twill.permissions_table', 'permissions');
$rolesTableName = config('twill.roles_table', 'roles');

if (!Schema::hasTable($permissionsTableName)
&& !Schema::hasTable('groups')
&& !Schema::hasTable('roles')
&& !Schema::hasTable($rolesTableName)
&& !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($permissionsTableName, function (Blueprint $table) {
createDefaultTableFields($table);
$table->string('name');
$table->string('display_name')->nullable();
Expand All @@ -40,14 +43,14 @@ public function up()
$table->boolean('is_everyone_group')->default(false);
});

Schema::create('roles', function (Blueprint $table) {
Schema::create($rolesTableName, function (Blueprint $table) {
createDefaultTableFields($table);
$table->string('name', 255)->nullable();
$table->boolean('in_everyone_group')->default(true);
$table->integer('position')->unsigned()->nullable();
});

Schema::create('permission_twill_user', function (Blueprint $table) {
Schema::create('permission_twill_user', function (Blueprint $table) use($permissionsTableName) {
$table->bigInteger('twill_user_id')->unsigned()->nullable();
$table->foreign('twill_user_id')
->references('id')
Expand All @@ -57,7 +60,7 @@ public function up()
$table->bigInteger('permission_id')->unsigned()->nullable();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->on($permissionsTableName)
->onDelete('cascade');
});

Expand All @@ -77,11 +80,11 @@ public function up()
$table->integer('position')->unsigned()->nullable();
});

Schema::create('group_permission', function (Blueprint $table) {
Schema::create('group_permission', function (Blueprint $table) use($permissionsTableName) {
$table->bigInteger('permission_id')->unsigned()->nullable();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->on($permissionsTableName)
->onDelete('cascade');

$table->bigInteger('group_id')->unsigned()->nullable();
Expand All @@ -91,17 +94,17 @@ public function up()
->onDelete('cascade');
});

Schema::create('permission_role', function (Blueprint $table) {
Schema::create('permission_role', function (Blueprint $table) use($permissionsTableName, $rolesTableName) {
$table->bigInteger('permission_id')->unsigned()->nullable();
$table->foreign('permission_id')
->references('id')
->on('permissions')
->on($permissionsTableName)
->onDelete('cascade');

$table->bigInteger('role_id')->unsigned()->nullable();
$table->foreign('role_id')
->references('id')
->on('roles')
->on($rolesTableName)
->onDelete('cascade');
});

Expand All @@ -118,13 +121,17 @@ public function up()
*/
public function down()
{
$permissionsTableName = config('twill.permissions_table', 'permissions');
$rolesTableName = 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($permissionsTableName);
Schema::dropIfExists('groups');
Schema::dropIfExists('roles');
Schema::dropIfExists($rolesTableName);
}

private function seedBasicPermissions()
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ class Permission extends BaseModel
'is_default',
];

public function __construct(array $attributes = [])
{
$this->table = config('twill.permissions_table', 'permissions');
parent::__construct($attributes);
}

protected $appends = ['permissionable_module'];

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ 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