шаг 5 завершен

This commit is contained in:
Alisa
2026-06-04 19:19:35 +03:00
parent 238831ad3a
commit 6216c1ee4f
34 changed files with 1859 additions and 36 deletions
+3
View File
@@ -3,7 +3,10 @@
declare(strict_types=1);
use App\Controllers\Admin\CategoryAdminController;
use App\Core\Auth;
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
Auth::requireAdmin();
(new CategoryAdminController())->index();
+50
View File
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);
use App\Core\Auth;
use App\Repositories\AdminRepository;
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
$repository = new AdminRepository();
$needsSetup = $repository->activeCount() === 0;
if ($needsSetup) {
header('Location: /admin/setup.php?token=' . rawurlencode((string) app_env('INSTALL_TOKEN', '')));
exit;
}
$error = null;
$next = admin_safe_next((string) ($_GET['next'] ?? '/admin/'));
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$next = admin_safe_next((string) ($_POST['next'] ?? '/admin/'));
if (Auth::attempt((string) ($_POST['email'] ?? ''), (string) ($_POST['password'] ?? ''))) {
header('Location: ' . ($next !== '' ? $next : '/admin/'));
exit;
}
$error = 'Неверный email или пароль.';
}
view('admin/login', [
'title' => 'Вход в админку - Рыбсток',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Админка', 'url' => '/admin/'],
['title' => 'Вход'],
],
'error' => $error,
'next' => $next,
]);
function admin_safe_next(string $next): string
{
if ($next === '' || !str_starts_with($next, '/') || str_starts_with($next, '//')) {
return '/admin/';
}
return $next;
}
+12
View File
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
use App\Core\Auth;
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
Auth::logout();
header('Location: /admin/login.php');
exit;
+3
View File
@@ -3,7 +3,10 @@
declare(strict_types=1);
use App\Controllers\Admin\ProductAdminController;
use App\Core\Auth;
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
Auth::requireAdmin();
(new ProductAdminController())->index();
+51
View File
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
use App\Repositories\AdminRepository;
require_once dirname(__DIR__, 2) . '/app/bootstrap.php';
$repository = new AdminRepository();
$isLocal = app_env('APP_ENV', 'production') === 'local';
$expectedToken = (string) app_env('INSTALL_TOKEN', '');
$actualToken = (string) ($_GET['token'] ?? $_POST['token'] ?? '');
$canRun = $isLocal && $expectedToken !== '' && hash_equals($expectedToken, $actualToken);
$hasAdmin = $repository->activeCount() > 0;
$message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!$canRun || $hasAdmin) {
http_response_code(403);
$error = 'Создание администратора недоступно.';
} else {
try {
$repository->createOwner(
(string) ($_POST['name'] ?? ''),
(string) ($_POST['email'] ?? ''),
(string) ($_POST['password'] ?? '')
);
$message = 'Администратор создан. Теперь можно войти.';
$hasAdmin = true;
} catch (Throwable $exception) {
$error = app_env('APP_DEBUG', 'false') === 'true'
? $exception->getMessage()
: 'Не удалось создать администратора.';
}
}
}
view('admin/setup', [
'title' => 'Создание администратора - Рыбсток',
'breadcrumbs' => [
['title' => 'Главная', 'url' => '/'],
['title' => 'Админка', 'url' => '/admin/'],
['title' => 'Создание администратора'],
],
'canRun' => $canRun,
'hasAdmin' => $hasAdmin,
'token' => $actualToken,
'message' => $message,
'error' => $error,
]);