Refactor admin panel: enhance user management and security features

- Added role-based access control for user management links in admin navigation.
- Improved user account display with a dedicated account link.
- Implemented form submission confirmation for delete actions in the admin interface.
- Introduced a password visibility toggle on the login page.
- Added login throttling to prevent brute-force attacks, recording failed attempts.
- Created a CLI script for creating an admin user, replacing the previous hardcoded admin credentials.
- Updated database schema to support login attempt tracking.
- Enhanced user editing functionality, allowing role changes with restrictions.
- Improved modal handling for user actions, ensuring better focus management.
- Added styles for new account management page and improved modal transitions.
This commit is contained in:
David Dyer committed 2026-10-25 12:58:19 -04:00
1 parent 91ec95a4a9
commit 242c5165e1
21 files changed
+669 -77

No files matched your search

+182
View File
@@ -0,0 +1,182 @@
<?php
declare(strict_types=1);
require_once __DIR__ . '/../inc/auth.php';
require_once __DIR__ . '/../inc/assets.php';
require_once __DIR__ . '/../inc/users.php';
// Any signed-in user can change their own username and/or password here,
// whatever their role. Any change requires the current password.
require_auth();
$user = get_logged_in_user();
$userId = (int) $user['id'];
$errorMsg = '';
function account_post_str(string $key): string {
return is_string($_POST[$key] ?? null) ? $_POST[$key] : '';
}
/**
* Saves whichever of username / password changed, after checking the
* current password once. Returns an error message, or null on success.
*/
function handle_update_account(int $userId, array $user): ?string {
$username = trim(account_post_str('username'));
$newPassword = account_post_str('new_password');
$usernameChanged = $username !== $user['username'];
$passwordChanged = $newPassword !== '';
if (!verify_user_password($userId, account_post_str('current_password'))) {
return 'Your current password is incorrect.';
}
if (!$usernameChanged && !$passwordChanged) {
return 'Nothing to change — edit your username or enter a new password.';
}
// Validate everything before saving anything, so a bad password
// doesn't leave a half-applied username change behind.
if ($usernameChanged) {
if ($error = validate_username($username)) {
return $error;
}
if (username_exists($username, $userId)) {
return 'That username is already taken.';
}
}
if ($passwordChanged) {
if (strlen($newPassword) < MIN_PASSWORD_LENGTH) {
return 'New password must be at least ' . MIN_PASSWORD_LENGTH . ' characters.';
}
if ($newPassword !== account_post_str('confirm_password')) {
return 'New passwords do not match.';
}
}
if ($usernameChanged) {
update_user($userId, $username, $user['role']);
$_SESSION['admin_username'] = $username;
}
if ($passwordChanged) {
set_user_password($userId, $newPassword);
// New session ID after a credential change, so any copy of the old
// session cookie stops working.
session_regenerate_id(true);
}
return null;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verify_csrf_token(account_post_str('csrf_token'))) {
$errorMsg = 'Session expired or invalid security token. Please try again.';
} else {
$errorMsg = handle_update_account($userId, $user) ?? '';
}
if ($errorMsg === '') {
header('Location: account.php?saved=1');
exit;
}
}
$justSaved = !$_POST && isset($_GET['saved']);
$isAdmin = ($user['role'] ?? '') === 'admin';
// After a failed save, keep the username they typed in the field.
$usernameValue = $_POST ? trim(account_post_str('username')) : $user['username'];
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Account | The Old Fitz Admin</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,400;0,9..144,600;1,9..144,400&family=Karla:wght@400;500;600;700&family=IBM+Plex+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="../styles.css<?= asset_version('styles.css') ?>">
</head>
<body>
<nav class="admin-nav">
<div class="container admin-nav-inner">
<a href="index.php" class="admin-brand">
<img src="../assets/knot.png" alt="" class="knot">
<span>The Old Fitz Admin</span>
</a>
<div class="admin-nav-links">
<a href="index.php">Announcements</a>
<a href="menu.php">Menu</a>
<?php if ($isAdmin): ?>
<a href="users.php">Users</a>
<?php endif; ?>
<a href="hours.php">Hours</a>
</div>
<div class="admin-user">
<a href="../" target="_blank" rel="noopener">View Public Site ↗</a>
<a href="account.php" class="admin-account-link active" title="My Account"><?= htmlspecialchars($user['username']) ?></a>
<a href="logout.php">Sign Out</a>
</div>
</div>
</nav>
<main class="dashboard-layout">
<div class="container">
<div class="dash-header">
<p class="eyebrow">Account</p>
<h1>My Account</h1>
</div>
<?php if ($errorMsg !== ''): ?>
<div class="alert-error" role="alert"><?= htmlspecialchars($errorMsg) ?></div>
<?php elseif ($justSaved): ?>
<div class="alert-success" role="status">Account updated.</div>
<?php endif; ?>
<div class="panel account-panel">
<div class="panel-head">
<h2>
<span>👤 Account Details</span>
</h2>
</div>
<p class="muted">Role: <?= htmlspecialchars(ucfirst((string) $user['role'])) ?></p>
<form method="POST" action="account.php">
<input type="hidden" name="csrf_token" value="<?= htmlspecialchars(get_csrf_token()) ?>">
<div class="form-grid">
<div class="form-group">
<label for="account-username">Username</label>
<input type="text" id="account-username" name="username" class="form-control" autocomplete="username" value="<?= htmlspecialchars($usernameValue) ?>" required>
</div>
<div class="form-group">
<label for="account-current">Current Password</label>
<input type="password" id="account-current" name="current_password" class="form-control" autocomplete="current-password" required>
</div>
<div class="form-group">
<label for="account-new">New Password</label>
<input type="password" id="account-new" name="new_password" class="form-control" autocomplete="new-password" minlength="<?= MIN_PASSWORD_LENGTH ?>">
<p class="muted field-hint">Leave blank to keep your current password.</p>
</div>
<div class="form-group">
<label for="account-confirm">Confirm New Password</label>
<input type="password" id="account-confirm" name="confirm_password" class="form-control" autocomplete="new-password" minlength="<?= MIN_PASSWORD_LENGTH ?>">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
</form>
</div>
</div>
</main>
</body>
</html>