Files
landing-api/app/Models/UserSession.php
Josep Piguillem 3897113313 Initial commit: Pireon Admin — headless admin panel and public API
Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer
collective, backed by the Pireon OAuth identity server. The public site is a
separate, server-rendered application consuming /api/public/v1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 17:55:27 +02:00

76 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Models;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* One browser's login, as Laravel's own database session driver already
* records it.
*
* Not a table this application created — `sessions` is the one Laravel's
* `DatabaseSessionHandler` writes to on every request when
* `SESSION_DRIVER=database`, which production always runs. This model exists
* only to make that table readable in the panel; nothing here changes how a
* session is written or expired.
*
* Deactivating somebody in this panel (`is_active = false`) already stops the
* *next* request the moment the guard re-checks it. What it does not do is
* touch a session already in progress, and there was never anywhere to see
* one, let alone end it before it next made a request. This closes that gap.
*/
#[Fillable(['id', 'user_id', 'ip_address', 'user_agent', 'payload', 'last_activity'])]
class UserSession extends Model
{
protected $table = 'sessions';
protected $primaryKey = 'id';
public $incrementing = false;
protected $keyType = 'string';
// Written once by the session handler and never touched again; there is
// no `created_at`/`updated_at` pair on this table to cast.
public $timestamps = false;
protected function casts(): array
{
return [
'last_activity' => 'integer',
];
}
/**
* @return BelongsTo<User, $this>
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
/**
* Whether this row is the request reading it right now — the one session
* this screen must never offer to end, because doing so would sign out
* the person clicking the button mid-click.
*/
public function isCurrent(): bool
{
return $this->id === session()->getId();
}
/**
* @param Builder<$this> $query
* @return Builder<$this>
*/
public function scopeRecent(Builder $query): Builder
{
return $query->orderByDesc('last_activity');
}
}