LuminaWebsite/change_password.php
Maaxxs a96fdd1b16 Force a password change on first login for newly created users
New users no longer get a manually typed password: admins get a
random one-time password shown once after creation, and the account
is flagged must_change_password. require_login() redirects anyone
with that flag to the new change_password.php page (except that page
and logout) until they set their own password. Admins can also toggle
the flag when editing an existing user to force a re-change.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 06:45:44 +02:00

51 lines
1.9 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_login();
$user = current_user();
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
$newPassword = (string) ($_POST['new_password'] ?? '');
$newPasswordConfirm = (string) ($_POST['new_password_confirm'] ?? '');
if (strlen($newPassword) < 8) {
$error = 'Das neue Passwort muss mindestens 8 Zeichen lang sein.';
} elseif ($newPassword !== $newPasswordConfirm) {
$error = 'Die Passwoerter stimmen nicht ueberein.';
} else {
$stmt = db()->prepare('UPDATE users SET password_hash = ?, must_change_password = 0 WHERE id = ?');
$stmt->execute([password_hash($newPassword, PASSWORD_DEFAULT), $user['id']]);
set_flash('success', 'Passwort wurde geaendert.');
redirect('/dashboard.php');
}
}
$pageTitle = 'Passwort aendern';
include __DIR__ . '/includes/header.php';
?>
<div class="panel panel-narrow">
<h1>Passwort aendern</h1>
<p class="text-muted">
<?= $user['must_change_password'] ? 'Du musst dein Passwort aendern, bevor du fortfahren kannst.' : 'Passwort aendern' ?>
</p>
<?php if ($error): ?>
<div class="flash flash-error"><?= e($error) ?></div>
<?php endif; ?>
<form method="post" action="/change_password.php">
<?= csrf_field() ?>
<div>
<label for="new_password">Neues Passwort</label>
<input type="password" id="new_password" name="new_password" required autofocus>
<div class="field-hint">Mindestens 8 Zeichen.</div>
</div>
<div>
<label for="new_password_confirm">Neues Passwort wiederholen</label>
<input type="password" id="new_password_confirm" name="new_password_confirm" required>
</div>
<button type="submit">Passwort speichern</button>
</form>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>