You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
math-learing/src/com/personalproject/auth/AccountRepository.java

141 lines
4.2 KiB

package com.personalproject.auth;
import com.personalproject.model.DifficultyLevel;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
* 存放用户账号并提供认证和注册查询能力.
*/
public final class AccountRepository {
private final Map<String, UserAccount> accounts = new HashMap<>();
private final Map<String, String> registrationCodes = new HashMap<>();
/**
* 根据用户名与密码查找匹配的账号.
*
* @param username 用户名.
* @param password 密码.
* @return 匹配成功时返回账号信息,否则返回空结果.
*/
public Optional<UserAccount> authenticate(String username, String password) {
if (username == null || password == null) {
return Optional.empty();
}
UserAccount account = accounts.get(username.trim());
if (account == null || !account.isRegistered()) {
return Optional.empty();
}
if (!account.password().equals(password.trim())) {
return Optional.empty();
}
return Optional.of(account);
}
/**
* Registers a new user account with email.
*
* @param username The username
* @param email The email address
* @param difficultyLevel The selected difficulty level
* @return true if registration was successful, false if username already exists
*/
public boolean registerUser(String username, String email, DifficultyLevel difficultyLevel) {
String trimmedUsername = username.trim();
String trimmedEmail = email.trim();
if (accounts.containsKey(trimmedUsername)) {
return false; // Username already exists
}
// Check if email is already used by another account
for (UserAccount account : accounts.values()) {
if (account.email().equals(trimmedEmail) && account.isRegistered()) {
return false; // Email already registered
}
}
UserAccount newAccount = new UserAccount(
trimmedUsername,
trimmedEmail,
"", // Empty password initially
difficultyLevel,
LocalDateTime.now(),
false); // Not registered until password is set
accounts.put(trimmedUsername, newAccount);
return true;
}
/**
* Sets the password for a user after registration.
*
* @param username The username
* @param password The password to set
* @return true if successful, false if user doesn't exist
*/
public boolean setPassword(String username, String password) {
UserAccount account = accounts.get(username.trim());
if (account == null) {
return false;
}
UserAccount updatedAccount = new UserAccount(
account.username(),
account.email(),
password,
account.difficultyLevel(),
account.registrationDate(),
true); // Now registered
accounts.put(username.trim(), updatedAccount);
return true;
}
/**
* Changes the password for an existing user.
*
* @param username The username
* @param oldPassword The current password
* @param newPassword The new password
* @return true if successful, false if old password is incorrect or user doesn't exist
*/
public boolean changePassword(String username, String oldPassword, String newPassword) {
UserAccount account = accounts.get(username.trim());
if (account == null || !account.password().equals(oldPassword) || !account.isRegistered()) {
return false;
}
UserAccount updatedAccount = new UserAccount(
account.username(),
account.email(),
newPassword,
account.difficultyLevel(),
account.registrationDate(),
true);
accounts.put(username.trim(), updatedAccount);
return true;
}
/**
* Checks if a user exists in the system.
*
* @param username The username to check
* @return true if user exists, false otherwise
*/
public boolean userExists(String username) {
return accounts.containsKey(username.trim());
}
/**
* Gets a user account by username.
*
* @param username The username
* @return Optional containing the user account if found
*/
public Optional<UserAccount> getUser(String username) {
return Optional.ofNullable(accounts.get(username.trim()));
}
}