new funcionality

This commit is contained in:
2026-07-27 17:20:02 +02:00
parent c2b5291a74
commit c5ce8a7a36
101 changed files with 6889 additions and 176 deletions

View File

@@ -0,0 +1,58 @@
<?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();
}
}