forked from laysense/laysense.cn
Fix CMS login and restore PAGEPAN feature icons
This commit is contained in:
@@ -4,11 +4,13 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Database;
|
||||
use App\Content\Html;
|
||||
use App\Content\Passwords;
|
||||
use App\Content\Repository;
|
||||
use App\Content\View;
|
||||
use App\Http\Request;
|
||||
use RuntimeException;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
final class AdminController
|
||||
@@ -18,41 +20,41 @@ final class AdminController
|
||||
public function login(Request $request): Response
|
||||
{
|
||||
if ($this->signedIn($request)) {
|
||||
return View::redirect('/admin');
|
||||
}
|
||||
$error = '';
|
||||
$email = '';
|
||||
$status = 200;
|
||||
if ($request->method() === 'POST') {
|
||||
if (!$this->validToken($request)) {
|
||||
return $this->error($request, '表单已过期,请刷新后重试。', 403);
|
||||
}
|
||||
$email = strtolower($this->input($request, 'email'));
|
||||
$password = $this->input($request, 'password', trimValue: false);
|
||||
$repo = new Repository();
|
||||
$address = $this->address($request);
|
||||
if (!$repo->recordLoginAttempt($address)) {
|
||||
$error = '尝试次数过多,请在 15 分钟后重试。';
|
||||
$status = 429;
|
||||
} else {
|
||||
$user = strlen($email) <= 254 ? $repo->administrator($email) : null;
|
||||
$hash = $user['password_hash'] ?? '$2y$12$XGNKW.dBbfpUrZT8MYvWquCW1dBN3fIlVb29tlaoFIge3YDWG0AzG';
|
||||
if (strlen($password) <= 72 && password_verify($password, $hash) && $user) {
|
||||
$request->sessionRegenerateId(true);
|
||||
$request->session()->flush();
|
||||
$request->session()->set('admin_id', $user['id']);
|
||||
$request->session()->set('auth_version', hash('sha256', $user['password_hash']));
|
||||
$request->session()->set('admin_since', time());
|
||||
$repo->clearLoginAttempts($address);
|
||||
|
||||
return View::redirect('/admin');
|
||||
}
|
||||
$error = '邮箱或密码有误。';
|
||||
$status = 422;
|
||||
}
|
||||
return $this->success($request, '/admin');
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-login', ['title' => '登录内容管理', 'error' => $error, 'email' => $email], $status);
|
||||
$data = ['title' => '登录内容管理', 'error' => '', 'email' => ''];
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->render($request, 'admin-login', $data);
|
||||
}
|
||||
|
||||
$data['email'] = strtolower($this->input($request, 'email'));
|
||||
if (!$this->validToken($request)) {
|
||||
$data['error'] = '登录表单已更新,请重新提交。若反复出现此提示,请确认浏览器允许本站 Cookie。';
|
||||
|
||||
return $this->render($request, 'admin-login', $data, 403);
|
||||
}
|
||||
|
||||
$password = $this->input($request, 'password', trimValue: false);
|
||||
$repo = new Repository();
|
||||
$user = strlen($data['email']) <= 254 ? $repo->administrator($data['email']) : null;
|
||||
$hash = $user['password_hash'] ?? '$2y$12$XGNKW.dBbfpUrZT8MYvWquCW1dBN3fIlVb29tlaoFIge3YDWG0AzG';
|
||||
if (Passwords::isSupported($password) && password_verify($password, $hash) && $user) {
|
||||
$this->openSession($request, $user);
|
||||
|
||||
return $this->success($request, '/admin');
|
||||
}
|
||||
|
||||
$retryAfter = $repo->recordFailedLogin($request->clientAddress());
|
||||
if ($retryAfter > 0) {
|
||||
$data['error'] = '当前来源的密码验证失败次数过多,请核对密码或稍后再试。';
|
||||
|
||||
return $this->render($request, 'admin-login', $data, 429)->withHeader('Retry-After', (string) $retryAfter);
|
||||
}
|
||||
|
||||
$data['error'] = '邮箱或密码有误。';
|
||||
|
||||
return $this->render($request, 'admin-login', $data, 422);
|
||||
}
|
||||
|
||||
public function logout(Request $request): Response
|
||||
@@ -61,9 +63,47 @@ final class AdminController
|
||||
return $failure;
|
||||
}
|
||||
$request->session()->flush();
|
||||
$request->session()->save();
|
||||
$request->sessionRegenerateId(true);
|
||||
|
||||
return View::redirect('/admin/login');
|
||||
return $this->success($request, '/admin/login');
|
||||
}
|
||||
|
||||
public function password(Request $request): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$current = $this->input($request, 'current_password', trimValue: false);
|
||||
$password = $this->input($request, 'new_password', trimValue: false);
|
||||
$confirmation = $this->input($request, 'confirm_password', trimValue: false);
|
||||
if (!Passwords::isSupported($current) || !password_verify($current, $this->user['password_hash'])) {
|
||||
$error = '当前密码有误。';
|
||||
} elseif (!Passwords::isValidNew($password)) {
|
||||
$error = '新密码至少 8 个字符,最多 72 字节。';
|
||||
} elseif ($password !== $confirmation) {
|
||||
$error = '两次输入的新密码不一致。';
|
||||
} else {
|
||||
try {
|
||||
$newHash = password_hash($password, PASSWORD_DEFAULT);
|
||||
if (!(new Repository())->changePassword($this->user, $newHash)) {
|
||||
throw new RuntimeException('密码已在其他会话中更新,请重新登录。');
|
||||
}
|
||||
$this->openSession($request, array_replace($this->user, ['password_hash' => $newHash]));
|
||||
|
||||
return $this->success($request, '/admin/password?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-password', [
|
||||
'title' => '修改密码', 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
public function dashboard(Request $request): Response
|
||||
@@ -121,7 +161,7 @@ final class AdminController
|
||||
}
|
||||
$saved = $repo->saveArticle($id === null ? null : (int) $id, $data, $this->version($request));
|
||||
|
||||
return View::redirect('/admin/articles/' . $saved . '?saved=1');
|
||||
return $this->success($request, '/admin/articles/' . $saved . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
@@ -171,7 +211,7 @@ final class AdminController
|
||||
}
|
||||
$repo->savePage($slug, $data, $this->version($request));
|
||||
|
||||
return View::redirect('/admin/pages/' . $slug . '?saved=1');
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
@@ -206,7 +246,7 @@ final class AdminController
|
||||
$data['body'] = Html::sanitize($data['body']);
|
||||
$repo->saveSection($slug, $key, $data, $this->version($request));
|
||||
|
||||
return View::redirect('/admin/pages/' . $slug . '/' . $key . '?saved=1');
|
||||
return $this->success($request, '/admin/pages/' . $slug . '/' . $key . '?saved=1');
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
@@ -224,7 +264,7 @@ final class AdminController
|
||||
}
|
||||
$body = $this->input($request, 'body');
|
||||
if (strlen($body) > 300000) {
|
||||
return new Response(413, ['Cache-Control' => 'no-store'], '正文太长。');
|
||||
return $this->error($request, '正文太长。', 413);
|
||||
}
|
||||
$html = $this->input($request, 'format') === 'html' ? Html::sanitize($body) : Html::markdown($body);
|
||||
|
||||
@@ -237,10 +277,12 @@ final class AdminController
|
||||
private function guard(Request $request): ?Response
|
||||
{
|
||||
if (!$this->signedIn($request)) {
|
||||
return View::redirect('/admin/login');
|
||||
return $request->isAjax()
|
||||
? $this->json(['message' => '登录已过期。请在新标签页重新登录,再返回继续编辑。'], 401)
|
||||
: View::redirect('/admin/login');
|
||||
}
|
||||
if ($request->method() === 'POST' && !$this->validToken($request)) {
|
||||
return $this->error($request, '表单已过期,请刷新后重试。', 403);
|
||||
return $this->error($request, '表单凭证已更新,请重新提交。', 403);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -256,6 +298,7 @@ final class AdminController
|
||||
$user = (new Repository())->administratorById($id);
|
||||
if (!$user || !hash_equals(hash('sha256', $user['password_hash']), (string) $session->get('auth_version'))) {
|
||||
$session->flush();
|
||||
$session->save();
|
||||
|
||||
return false;
|
||||
}
|
||||
@@ -264,6 +307,19 @@ final class AdminController
|
||||
return true;
|
||||
}
|
||||
|
||||
private function openSession(Request $request, array $user): void
|
||||
{
|
||||
$request->sessionRegenerateId(true);
|
||||
$session = $request->session();
|
||||
$session->flush();
|
||||
$session->put([
|
||||
'admin_id' => $user['id'],
|
||||
'auth_version' => hash('sha256', $user['password_hash']),
|
||||
'admin_since' => time(),
|
||||
]);
|
||||
$this->token($request);
|
||||
}
|
||||
|
||||
private function token(Request $request): string
|
||||
{
|
||||
$session = $request->session();
|
||||
@@ -271,6 +327,7 @@ final class AdminController
|
||||
if (!is_string($token)) {
|
||||
$token = bin2hex(random_bytes(32));
|
||||
$session->set('csrf', $token);
|
||||
$session->save();
|
||||
}
|
||||
|
||||
return $token;
|
||||
@@ -278,7 +335,9 @@ final class AdminController
|
||||
|
||||
private function validToken(Request $request): bool
|
||||
{
|
||||
return hash_equals($this->token($request), $this->input($request, 'csrf'));
|
||||
$token = $request->session()->get('csrf');
|
||||
|
||||
return is_string($token) && hash_equals($token, $this->input($request, 'csrf'));
|
||||
}
|
||||
|
||||
private function input(Request $request, string $key, bool $trimValue = true): string
|
||||
@@ -313,15 +372,6 @@ final class AdminController
|
||||
return $data;
|
||||
}
|
||||
|
||||
private function address(Request $request): string
|
||||
{
|
||||
$peer = $request->getRemoteIp();
|
||||
$trusted = explode(',', getenv('LAYSENSE_TRUSTED_PROXIES') ?: '');
|
||||
$candidate = $request->header('x-real-ip', '');
|
||||
|
||||
return in_array($peer, $trusted, true) && filter_var($candidate, FILTER_VALIDATE_IP) ? $candidate : $peer;
|
||||
}
|
||||
|
||||
private function validationMessage(RuntimeException $exception): string
|
||||
{
|
||||
if ($exception instanceof \PDOException) {
|
||||
@@ -333,9 +383,32 @@ final class AdminController
|
||||
return $exception->getMessage();
|
||||
}
|
||||
|
||||
private function success(Request $request, string $path): Response
|
||||
{
|
||||
return $request->isAjax() ? $this->json(['redirect' => $path]) : View::redirect($path);
|
||||
}
|
||||
|
||||
private function json(array $payload, int $status = 200): Response
|
||||
{
|
||||
return new Response($status, [
|
||||
'Content-Type' => 'application/json; charset=utf-8',
|
||||
'Cache-Control' => 'no-store',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'X-Robots-Tag' => 'noindex, nofollow',
|
||||
], Database::json($payload));
|
||||
}
|
||||
|
||||
private function render(Request $request, string $view, array $data, int $status = 200): Response
|
||||
{
|
||||
return View::render($view, $data + ['token' => $this->token($request), 'user' => $this->user], $status, true);
|
||||
$token = $this->token($request);
|
||||
if ($request->isAjax() && $status >= 400) {
|
||||
return $this->json([
|
||||
'message' => $data['error'] ?? $data['title'],
|
||||
...($status === 403 ? ['csrf' => $token] : []),
|
||||
], $status);
|
||||
}
|
||||
|
||||
return View::render($view, $data + ['token' => $token, 'user' => $this->user], $status, true);
|
||||
}
|
||||
|
||||
private function error(Request $request, string $message, int $status): Response
|
||||
|
||||
Reference in New Issue
Block a user