- Slim 4 + php-di контейнер с маршрутизацией - Docker Compose: app (PHP 8.3-FPM) + web (nginx) + db (MariaDB) - Phinx миграция: таблица users (uuid, username, password_hash) - Auth: login/logout с сессиями - Setup: создание первого пользователя - Dashboard с layout (Bootstrap 5.3) - CLI: bin/console, bin/run-scan-worker.php - Smoke test: scripts/check.sh - README.md, PLAN.md
40 lines
1.1 KiB
PHP
40 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Domovoy\Middleware;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use Slim\Routing\RouteContext;
|
|
|
|
class AuthMiddleware
|
|
{
|
|
public function __invoke(
|
|
ServerRequestInterface $request,
|
|
\Psr\Http\Server\RequestHandlerInterface $handler
|
|
): ResponseInterface {
|
|
$path = $request->getUri()->getPath();
|
|
|
|
$publicPaths = ['/login', '/setup'];
|
|
$isPublic = in_array($path, $publicPaths, true);
|
|
|
|
if (!isset($_SESSION['user_id']) && !$isPublic) {
|
|
$response = new \Slim\Psr7\Response();
|
|
return $response
|
|
->withHeader('Location', '/login')
|
|
->withStatus(302);
|
|
}
|
|
|
|
// If logged in and trying to access login/setup, redirect to dashboard
|
|
if (isset($_SESSION['user_id']) && $isPublic) {
|
|
$response = new \Slim\Psr7\Response();
|
|
return $response
|
|
->withHeader('Location', '/dashboard')
|
|
->withStatus(302);
|
|
}
|
|
|
|
return $handler->handle($request);
|
|
}
|
|
}
|