feat: admin-set passwords for staff, and a secure temporary-password generator

Creating a user takes the first password from the admin, and a new
POST /api/admin/users/{id}/password lets an admin set a temporary password
for another active user. Both store the bcrypt hash with login = N, so the
user must choose their own password at the next sign-in. No email is sent.
The password must match its confirmation and pass the admin password rules.
The emailed-password generator now draws from SecureRandom.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 11:19:10 +08:00
1 parent b49c9f1bc3
commit 2c0a847b68
12 files changed
+386 -15

No files matched your search

@@ -0,0 +1,40 @@
package com.solvway.services.system.util;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashSet;
import java.util.Set;
import org.junit.jupiter.api.Test;
import com.solvway.core.utils.ValidateUtil;
/** The shared temporary-password generator (lives in solvway-core-libs, which has no test setup). */
class ValidateUtilPasswordTest {
private static final String ALPHABET = "ABCDEFGHJKLMNPRSTUVWXYZabcdefghjkmnpqrstuvwyz23456789";
private final ValidateUtil util = new ValidateUtil();
@Test
void lengthMatchesArgumentAndCharactersComeFromTheAlphabet() {
for (int len : new int[] {1, 8, 12, 32}) {
String p = util.getRandomPasswordString(len);
assertThat(p).hasSize(len);
assertThat(p.chars()).allMatch(c -> ALPHABET.indexOf(c) >= 0);
}
}
@Test
void thousandPasswordsOfTwelveAreDistinct() {
Set<String> seen = new HashSet<>();
for (int i = 0; i < 1000; i++) seen.add(util.getRandomPasswordString(12));
assertThat(seen).hasSize(1000);
}
@Test
void generatorUsesSecureRandom() throws Exception {
// java.util.Random would also pass the tests above; this pins the secure source the spec requires.
var field = ValidateUtil.class.getDeclaredField("PASSWORD_RANDOM");
assertThat(field.getType()).isEqualTo(java.security.SecureRandom.class);
}
}