47 lines
964 B
PHP
47 lines
964 B
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||
|
|
|
||
|
|
class PersonalAccessToken extends Model
|
||
|
|
{
|
||
|
|
protected $fillable = [
|
||
|
|
'name',
|
||
|
|
'token',
|
||
|
|
'abilities',
|
||
|
|
'last_used_at',
|
||
|
|
'expires_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected function casts(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'abilities' => 'array',
|
||
|
|
'last_used_at' => 'datetime',
|
||
|
|
'expires_at' => 'datetime',
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
public function tokenable(): MorphTo
|
||
|
|
{
|
||
|
|
return $this->morphTo();
|
||
|
|
}
|
||
|
|
|
||
|
|
public function can(string $ability): bool
|
||
|
|
{
|
||
|
|
return in_array('*', $this->abilities ?? []) || in_array($ability, $this->abilities ?? []);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function markAsUsed(): void
|
||
|
|
{
|
||
|
|
$this->update(['last_used_at' => now()]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function isExpired(): bool
|
||
|
|
{
|
||
|
|
return $this->expires_at && $this->expires_at->isPast();
|
||
|
|
}
|
||
|
|
}
|