Итерация 1: каркас Slim 4 приложения
- 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
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Domovoy\Controllers;
|
||||
|
||||
use Domovoy\Services\AuthService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class SetupController
|
||||
{
|
||||
private AuthService $authService;
|
||||
|
||||
public function __construct(AuthService $authService)
|
||||
{
|
||||
$this->authService = $authService;
|
||||
}
|
||||
|
||||
public function form(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($this->authService->getUserCount() > 0) {
|
||||
return $response
|
||||
->withHeader('Location', '/login')
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
ob_start();
|
||||
$error = null;
|
||||
require dirname(__DIR__, 2) . '/templates/auth/setup.php';
|
||||
$body = ob_get_clean();
|
||||
$response->getBody()->write($body);
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
if ($this->authService->getUserCount() > 0) {
|
||||
return $response
|
||||
->withHeader('Location', '/login')
|
||||
->withStatus(302);
|
||||
}
|
||||
|
||||
$data = $request->getParsedBody();
|
||||
$username = trim($data['username'] ?? '');
|
||||
$password = $data['password'] ?? '';
|
||||
|
||||
if (strlen($username) < 2 || strlen($password) < 8) {
|
||||
ob_start();
|
||||
$error = 'Username must be at least 2 chars, password at least 8';
|
||||
require dirname(__DIR__, 2) . '/templates/auth/setup.php';
|
||||
$body = ob_get_clean();
|
||||
$response->getBody()->write($body);
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
try {
|
||||
$user = $this->authService->createUser($username, $password);
|
||||
} catch (\RuntimeException $e) {
|
||||
ob_start();
|
||||
$error = $e->getMessage();
|
||||
require dirname(__DIR__, 2) . '/templates/auth/setup.php';
|
||||
$body = ob_get_clean();
|
||||
$response->getBody()->write($body);
|
||||
return $response->withStatus(400);
|
||||
}
|
||||
|
||||
$_SESSION['user_id'] = $user->id;
|
||||
$_SESSION['username'] = $user->username;
|
||||
|
||||
return $response
|
||||
->withHeader('Location', '/dashboard')
|
||||
->withStatus(302);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user