singleloginsystem/app/Models/ConnectedApp.php
= 495c0ea2d8 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.
2026-05-19 07:16:17 +00:00

77 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Fillable;
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;
/**
* @property int $id
* @property string $name
* @property AppRole $pivot
*/
#[Fillable([
'name',
'slug',
'connection_protocol_id',
'connection_provider_id',
'connection_status_id',
])]
class ConnectedApp extends Model
{
use LogsActivity, SoftDeletes;
/**
* @return HasOne<ConnectionProtocol, $this>
*/
public function protocol(): HasOne
{
return $this->hasOne(ConnectionProtocol::class, 'id', 'connection_protocol_id');
}
/**
* @return BelongsTo<ConnectionProvider, $this>
*/
public function provider(): BelongsTo
{
return $this->belongsTo(ConnectionProvider::class, 'connection_provider_id');
}
/**
* @return HasOne<ConnectionStatus, $this>
*/
public function status(): HasOne
{
return $this->hasOne(ConnectionStatus::class, 'id', 'connection_status_id');
}
public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults()
->logFillable() // Log all fillable attributes
->logOnlyDirty() // Log only attributes that actually changed
->dontLogIfAttributesChangedOnly(['updated_at']) // Skip if only updated_at changed
->dontLogEmptyChanges() // Prevent saving logs with no changed attributes
->setDescriptionForEvent(fn (
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');
}
}