- Created `admin-head.php` for consistent <head> markup across admin pages. - Created `admin-nav.php` for shared navigation bar, reducing duplication. - Updated all admin pages (account, hours, index, login, menu, users) to utilize the new components. - Added security headers to `auth.php` and `index.php` for improved security. - Introduced `security-headers.php` to manage security headers across the application. - Moved inline styles from admin pages to SCSS files to comply with Content Security Policy (CSP). - Normalized file permissions during build process to prevent permission issues on deployment.
156 lines
4.9 KiB
PHP
156 lines
4.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once __DIR__ . '/../inc/auth.php';
|
|
require_once __DIR__ . '/../inc/assets.php';
|
|
require_once __DIR__ . '/../inc/admin-head.php';
|
|
require_once __DIR__ . '/../inc/admin-nav.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']);
|
|
// 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">
|
|
|
|
<?php render_admin_head('My Account'); ?>
|
|
|
|
<body>
|
|
|
|
<?php render_admin_nav($user, 'account'); ?>
|
|
|
|
<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>
|