59 lines
1.5 KiB
PHP
59 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\User;
|
|
|
|
/**
|
|
* Tokens OAuth activos de un usuario ("sesiones") — compartido por
|
|
* Admin\UserSessionController (sesión) y Api\UserSessionController (token),
|
|
* igual que AnalyticsSummary/AuditLog::paginatedFor.
|
|
*/
|
|
class UserSessions
|
|
{
|
|
public static function active(User $user)
|
|
{
|
|
return $user->tokens()
|
|
->where('revoked', false)
|
|
->where(function ($q) {
|
|
$q->whereNull('expires_at')->orWhere('expires_at', '>', now());
|
|
})
|
|
->with('client:id,name,revoked')
|
|
->latest('created_at')
|
|
->get()
|
|
->map(fn ($token) => [
|
|
'id' => $token->id,
|
|
'client_name' => $token->client?->name,
|
|
'client_revoked' => (bool) $token->client?->revoked,
|
|
'name' => $token->name,
|
|
'scopes' => $token->scopes,
|
|
'created_at' => $token->created_at,
|
|
'expires_at' => $token->expires_at,
|
|
]);
|
|
}
|
|
|
|
public static function revoke(User $user, string $tokenId): bool
|
|
{
|
|
$token = $user->tokens()->where('id', $tokenId)->first();
|
|
|
|
if (! $token) {
|
|
return false;
|
|
}
|
|
|
|
TokenRevocation::revoke($token);
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function revokeAll(User $user): int
|
|
{
|
|
$tokens = $user->tokens()->where('revoked', false)->get();
|
|
|
|
foreach ($tokens as $token) {
|
|
TokenRevocation::revoke($token);
|
|
}
|
|
|
|
return $tokens->count();
|
|
}
|
|
}
|