newpasswd.php
2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
declare(strict_types=1);
require __DIR__ . '/../src/bootstrap.php';
Auth::requireLogin();
$user = trim((string) ($_GET['user'] ?? $_POST['user'] ?? ''));
$erro = null;
$ok = null;
if ($user === '') {
$erro = 'Usuário inválido.';
} elseif ($_SERVER['REQUEST_METHOD'] === 'POST') {
$pass1 = (string) ($_POST['passwd1'] ?? '');
$pass2 = (string) ($_POST['passwd2'] ?? '');
if ($pass1 === '' || $pass1 !== $pass2) {
$erro = 'As senhas não coincidem ou estão em branco.';
} elseif (strlen($pass1) < 8) {
$erro = 'A senha precisa ter pelo menos 8 caracteres.';
} else {
try {
$ldap = new Ldap();
$ldap->bindService();
$entry = $ldap->findUserByCn($user);
if ($entry === null) {
throw new RuntimeException('Aluno não encontrado no AD.');
}
$dn = $ldap->dnOf($entry);
$ldap->modify($dn, [
'unicodePwd' => Ldap::encodePassword($pass1),
]);
$quemFez = Auth::currentLogin() ?? 'desconhecido';
AuditLog::record($quemFez, 'alterou_senha_aluno', $user);
$ok = 'Senha alterada com sucesso!';
} catch (Throwable $e) {
$erro = 'Erro ao alterar a senha: ' . $e->getMessage();
}
}
}
View::header('Nova senha');
?>
<div class="card">
<h1>Nova senha para <?= View::escape($user) ?></h1>
<?php if ($erro): ?><div class="alert error"><?= View::escape($erro) ?></div><?php endif; ?>
<?php if ($ok): ?><div class="alert success"><?= View::escape($ok) ?></div><?php endif; ?>
<?php if (!$ok): ?>
<form method="post" action="newpasswd.php">
<input type="hidden" name="user" value="<?= View::escape($user) ?>">
<label for="passwd1">Nova senha</label>
<input type="password" id="passwd1" name="passwd1" required minlength="8">
<label for="passwd2">Confirmar senha</label>
<input type="password" id="passwd2" name="passwd2" required minlength="8">
<button type="submit">Alterar</button>
</form>
<?php endif; ?>
<p><a href="dashboard.php">← Voltar</a></p>
</div>
<?php
View::footer();