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.
181 lines
5.4 KiB
181 lines
5.4 KiB
package com.example.myapp.service;
|
|
|
|
import com.example.myapp.model.User;
|
|
import java.util.Map;
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
public class UserService {
|
|
private Map<String, User> users;
|
|
private Map<String, String> usernameToEmail;
|
|
private Map<String, String> registrationCodes;
|
|
private Map<String, String> resetPasswordCodes;
|
|
|
|
private final FileStorageService fileStorageService;
|
|
private static final UserService instance = new UserService();
|
|
|
|
public static UserService getInstance() {
|
|
return instance;
|
|
}
|
|
|
|
private UserService() {
|
|
this.fileStorageService = new FileStorageService();
|
|
loadData();
|
|
}
|
|
|
|
private void loadData() {
|
|
users = fileStorageService.readUsersData();
|
|
registrationCodes = fileStorageService.readRegistrationCodesData();
|
|
resetPasswordCodes = fileStorageService.readResetPasswordCodesData();
|
|
|
|
usernameToEmail = new ConcurrentHashMap<>();
|
|
if (users != null) {
|
|
for (User user : users.values()) {
|
|
if (user.getUsername() != null && !user.getUsername().trim().isEmpty()) {
|
|
usernameToEmail.put(user.getUsername().toLowerCase(), user.getEmail());
|
|
}
|
|
}
|
|
}
|
|
|
|
if (users == null) users = new ConcurrentHashMap<>();
|
|
if (registrationCodes == null) registrationCodes = new ConcurrentHashMap<>();
|
|
if (resetPasswordCodes == null) resetPasswordCodes = new ConcurrentHashMap<>();
|
|
if (usernameToEmail == null) usernameToEmail = new ConcurrentHashMap<>();
|
|
}
|
|
|
|
private void saveUsers() {
|
|
fileStorageService.writeUsersData(users);
|
|
}
|
|
|
|
private void saveRegistrationCodes() {
|
|
fileStorageService.writeRegistrationCodesData(registrationCodes);
|
|
}
|
|
|
|
private void saveResetPasswordCodes() {
|
|
fileStorageService.writeResetPasswordCodesData(resetPasswordCodes);
|
|
}
|
|
|
|
public boolean emailExists(String email) {
|
|
return users.containsKey(email);
|
|
}
|
|
|
|
public boolean usernameExists(String username) {
|
|
return usernameToEmail.containsKey(username.toLowerCase());
|
|
}
|
|
|
|
public User getUserByIdentifier(String identifier) {
|
|
if (identifier.contains("@")) {
|
|
return users.get(identifier);
|
|
} else {
|
|
String email = usernameToEmail.get(identifier.toLowerCase());
|
|
return email != null ? users.get(email) : null;
|
|
}
|
|
}
|
|
|
|
public boolean userExists(String identifier) {
|
|
if (identifier.contains("@")) {
|
|
return emailExists(identifier);
|
|
} else {
|
|
return usernameExists(identifier);
|
|
}
|
|
}
|
|
|
|
public boolean isUserRegistered(String email) {
|
|
User user = users.get(email);
|
|
return user != null && user.getPasswordHash() != null && !user.getPasswordHash().isEmpty();
|
|
}
|
|
|
|
public void createPendingUser(String email, String code) {
|
|
if (users.containsKey(email)) {
|
|
User existingUser = users.get(email);
|
|
if (existingUser.getPasswordHash() == null || existingUser.getPasswordHash().isEmpty()) {
|
|
registrationCodes.put(email, code);
|
|
saveRegistrationCodes();
|
|
return;
|
|
}
|
|
}
|
|
|
|
users.putIfAbsent(email, new User(email, null));
|
|
registrationCodes.put(email, code);
|
|
saveUsers();
|
|
saveRegistrationCodes();
|
|
}
|
|
|
|
public boolean setUsername(String email, String username) {
|
|
if (username == null || username.trim().isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
String usernameLower = username.toLowerCase();
|
|
if (usernameToEmail.containsKey(usernameLower)) {
|
|
return false;
|
|
}
|
|
|
|
User user = users.get(email);
|
|
if (user != null) {
|
|
if (user.getUsername() != null) {
|
|
usernameToEmail.remove(user.getUsername().toLowerCase());
|
|
}
|
|
|
|
user.setUsername(username);
|
|
usernameToEmail.put(usernameLower, email);
|
|
saveUsers();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean verifyCode(String email, String code) {
|
|
String storedCode = registrationCodes.get(email);
|
|
if (storedCode != null && storedCode.equals(code)) {
|
|
User u = users.get(email);
|
|
if (u != null) {
|
|
u.setVerified(true);
|
|
registrationCodes.remove(email);
|
|
saveUsers();
|
|
saveRegistrationCodes();
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// 忘记密码相关方法
|
|
public void createResetPasswordCode(String email, String code) {
|
|
resetPasswordCodes.put(email, code);
|
|
saveResetPasswordCodes();
|
|
}
|
|
|
|
public boolean verifyResetPasswordCode(String email, String code) {
|
|
String storedCode = resetPasswordCodes.get(email);
|
|
if (storedCode != null && storedCode.equals(code)) {
|
|
resetPasswordCodes.remove(email);
|
|
saveResetPasswordCodes();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public void setPassword(String email, String passwordHash) {
|
|
User u = users.get(email);
|
|
if (u != null) {
|
|
u.setPasswordHash(passwordHash);
|
|
u.setVerified(true);
|
|
saveUsers();
|
|
}
|
|
}
|
|
|
|
public boolean checkPassword(String identifier, String plain) {
|
|
User user = getUserByIdentifier(identifier);
|
|
if (user == null || user.getPasswordHash() == null) {
|
|
return false;
|
|
}
|
|
|
|
return com.example.myapp.util.PasswordUtil.check(plain, user.getPasswordHash());
|
|
}
|
|
|
|
public String getDisplayName(String identifier) {
|
|
User user = getUserByIdentifier(identifier);
|
|
if (user == null) return identifier;
|
|
return user.getUsername() != null ? user.getUsername() : user.getEmail();
|
|
}
|
|
} |