Give the machine console its own session with PIN sign-in, make counters cumulative

The console under console/ has its own session cookie and console guard
(UseConsoleSession), so signing out or changing user there leaves the
normal interface alone. Links open it in a new window through a one-time
handoff key; a signed-out console asks for a PIN code or password on its
own login page. Users set their 4-6 digit PIN on the profile's PIN page,
and the console's change user dialog takes it on a keypad; five wrong
PINs lock PIN sign-in for 15 minutes.

Machine counters are cumulative: users record how much a job used and it
is added to the latest reading. A maintenance record restarts the whole
counter interval from the reading at the record.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-29 09:58:11 +02:00
co-authored by Claude Sonnet 5.5
parent 87e38260dc
commit 32c33e2a64
55 changed files with 1660 additions and 194 deletions
@@ -0,0 +1,65 @@
<?php
namespace App\Modules\Users\Presentation\Http\Controllers;
use App\Modules\Users\Application\UseCases\CheckLoginAllowed;
use App\Modules\Users\Presentation\Http\Middleware\UseConsoleSession;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
/**
* Opens the machine console in its own window signed in as the same user
* (spec 19.6). The normal user interface makes a one-time key valid for a
* minute; the console's own session (UseConsoleSession) takes it and signs
* the user in there. After that the two sessions live apart.
*/
class ConsoleHandoff
{
public const int SECONDS = 60;
/**
* In the normal user interface: a key for the console page in "to".
*/
public function start(Request $request): RedirectResponse
{
$to = (string) $request->query('to', '');
abort_unless(UseConsoleSession::isConsoleUrl($to), 404);
$token = Str::random(40);
Cache::put(self::cacheKey($token), ['user' => (int) $request->user()?->getAuthIdentifier(), 'to' => $to], self::SECONDS);
return redirect()->route('console.handoff.accept', $token);
}
/**
* In the console's own session: signs in with the key once.
*/
public function accept(Request $request, string $token, CheckLoginAllowed $checkLoginAllowed): RedirectResponse
{
/** @var array{user: int, to: string}|null $handoff */
$handoff = Cache::pull(self::cacheKey($token));
if ($handoff === null || ! $checkLoginAllowed->execute($handoff['user'])) {
return redirect()->route('console.login');
}
if (Auth::check()) {
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
}
Auth::loginUsingId($handoff['user']);
$request->session()->regenerate();
return redirect()->to($handoff['to']);
}
private static function cacheKey(string $token): string
{
return 'console-handoff|'.hash('sha256', $token);
}
}
@@ -0,0 +1,34 @@
<?php
namespace App\Modules\Users\Presentation\Http\Controllers;
use App\Modules\Users\Presentation\Http\Middleware\SetLocale;
use App\Modules\Users\Presentation\Http\Middleware\UseConsoleSession;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
/**
* Signs out of the machine console's own session only (spec 19.6), e.g. to
* sign in there with email and password; the normal user interface stays
* signed in. The console's sign-in page returns to the console page.
*/
class ConsoleLogoutController
{
public function __invoke(Request $request): RedirectResponse
{
$locale = app()->getLocale();
$returnTo = (string) $request->input('return_to', '');
Auth::logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
$request->session()->put(SetLocale::SESSION_KEY, $locale);
if (UseConsoleSession::isConsoleUrl($returnTo)) {
$request->session()->put('url.intended', $returnTo);
}
return redirect()->route('console.login');
}
}
@@ -36,7 +36,7 @@ class EnsureAccountIsUsable
// The reason goes in the URL: Livewire follows this redirect with
// fetch first, which would consume a flashed message.
return redirect()->route('login', ['access' => 'ended']);
return redirect()->route(UseConsoleSession::isConsole($request) ? 'console.login' : 'login', ['access' => 'ended']);
}
return $next($request);
@@ -0,0 +1,78 @@
<?php
namespace App\Modules\Users\Presentation\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Response;
/**
* Gives the machine console its own session (spec 19.6), so the console can
* run in its own window or on a tablet next to the normal user interface
* in the same browser: pages under console/ and the Livewire requests of
* components rendered there use a session cookie of their own and the
* "console" guard. Signing in or out on one side leaves the other alone,
* and /login always signs in to the normal user interface.
*
* Runs before StartSession. A Livewire request tells where its component
* was rendered by the path Livewire keeps in the snapshot (memo.path); it
* can only choose between the browser's own sessions, and the CSRF token
* of that session must still match.
*/
class UseConsoleSession
{
public const string PREFIX = 'console';
public const string GUARD = 'console';
public const string DEFAULT_GUARD = 'web';
public function __construct(private SessionManager $sessions) {}
public function handle(Request $request, Closure $next): Response
{
$console = self::isConsole($request);
$cookie = (string) config('session.cookie');
$this->sessions->driver()->setName($console ? $cookie.'_console' : $cookie);
Auth::shouldUse($console ? self::GUARD : self::DEFAULT_GUARD);
return $next($request);
}
public static function isConsole(Request $request): bool
{
if (self::isConsolePath($request->path())) {
return true;
}
if (! $request->hasHeader('X-Livewire')) {
return false;
}
$snapshot = json_decode((string) $request->input('components.0.snapshot', ''), true);
$path = is_array($snapshot) ? ($snapshot['memo']['path'] ?? null) : null;
return is_string($path) && self::isConsolePath($path);
}
/**
* Whether a URL of this application is on the console.
*/
public static function isConsoleUrl(string $url): bool
{
$base = rtrim(url('/'), '/').'/';
return Str::startsWith($url, $base.self::PREFIX.'/') && ! Str::contains($url, ['\\', '@']);
}
private static function isConsolePath(string $path): bool
{
$path = trim($path, '/');
return $path === self::PREFIX || str_starts_with($path, self::PREFIX.'/');
}
}