Files
Toiminnanohjaus/app/Modules/Users/Presentation/Http/Middleware/UseConsoleSession.php
T
petriandClaude Sonnet 5.5 32c33e2a64 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]>
2026-11-29 09:58:11 +02:00

79 lines
2.5 KiB
PHP

<?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.'/');
}
}