Feat: add app roles management and relationships
- Added `app_roles` migration to manage role assignments for connected apps, supporting role duration and indexing. - Introduced `AppRole` pivot model for managing `ConnectedApp` and `Role` relationships. - Updated `ConnectedApp` and `Role` models with `BelongsToMany` relationships for role associations. - Adjusted `permission` config to use custom `Role` model.
This commit is contained in:
parent
a2dbd0713c
commit
495c0ea2d8
39
app/Models/AppRole.php
Normal file
39
app/Models/AppRole.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\{Relations\BelongsTo, Relations\Pivot};
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $app_id
|
||||
* @property int $role_id
|
||||
* @property int $duration Duration in days.
|
||||
*/
|
||||
#[Fillable(['app_id', 'role_id', 'duration'])]
|
||||
class AppRole extends Pivot
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $table = 'app_roles';
|
||||
|
||||
/**
|
||||
* @return BelongsTo<ConnectedApp, $this>
|
||||
*/
|
||||
public function app(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(ConnectedApp::class, 'app_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Role, $this>
|
||||
*/
|
||||
public function role(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Role::class, 'role_id');
|
||||
}
|
||||
}
|
||||
@ -5,7 +5,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\{Model, Relations\BelongsTo, SoftDeletes};
|
||||
use Illuminate\Database\Eloquent\{Model, Relations\BelongsTo, Relations\BelongsToMany, SoftDeletes};
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Spatie\Activitylog\Models\Concerns\LogsActivity;
|
||||
use Spatie\Activitylog\Support\LogOptions;
|
||||
@ -13,6 +13,7 @@
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property AppRole $pivot
|
||||
*/
|
||||
#[Fillable([
|
||||
'name',
|
||||
@ -60,4 +61,16 @@ public function getActivitylogOptions(): LogOptions
|
||||
string $eventName
|
||||
) => ":causer.name {$eventName} connected app ':subject.name'");
|
||||
}
|
||||
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Role::class,
|
||||
'app_roles',
|
||||
'app_id',
|
||||
'role_id',
|
||||
)
|
||||
->using(AppRole::class)
|
||||
->withPivot('duration');
|
||||
}
|
||||
}
|
||||
|
||||
23
app/Models/Role.php
Normal file
23
app/Models/Role.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Spatie\Permission\Models\Role as SpatieRole;
|
||||
|
||||
class Role extends SpatieRole
|
||||
{
|
||||
public function apps(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
ConnectedApp::class,
|
||||
'app_roles',
|
||||
'role_id',
|
||||
'app_id'
|
||||
)
|
||||
->using(AppRole::class)
|
||||
->withPivot('id', 'duration');
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use app\Models\Role;
|
||||
use Spatie\Permission\DefaultTeamResolver;
|
||||
use Spatie\Permission\Models\{Permission, Role};
|
||||
use Spatie\Permission\Models\Permission;
|
||||
|
||||
return [
|
||||
|
||||
|
||||
33
database/migrations/2026_05_18_113635_app_roles.php
Normal file
33
database/migrations/2026_05_18_113635_app_roles.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\ConnectedApp;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class() extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('app_roles', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignIdFor(ConnectedApp::class, 'app_id');
|
||||
$table->foreignId('role_id')->comment('role_id from spatie roles table');
|
||||
$table->integer('duration')->comment('in days');
|
||||
$table->index(['app_id', 'role_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('app_roles');
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user