feat: 添加管理员密码修改功能

This commit is contained in:
2026-04-27 23:16:31 +08:00
parent c161660b51
commit 9c4f22ff97
8 changed files with 135 additions and 0 deletions
@@ -112,4 +112,31 @@ public sealed class AuthService : IAuthService
ExpiresAt = session.ExpiresAt
};
}
public async Task ChangePasswordAsync(Guid userId, ChangePasswordRequest request, CancellationToken cancellationToken = default)
{
ArgumentNullException.ThrowIfNull(request);
if (string.IsNullOrWhiteSpace(request.CurrentPassword) || string.IsNullOrWhiteSpace(request.NewPassword))
{
throw new InvalidOperationException("当前密码和新密码不能为空。");
}
if (request.NewPassword.Length < 6)
{
throw new InvalidOperationException("新密码长度不能少于 6 位。");
}
var user = await _userAccountRepository.GetByIdAsync(userId, cancellationToken)
?? throw new InvalidOperationException("用户不存在。");
if (!PasswordHasher.Verify(request.CurrentPassword, user.PasswordHash))
{
throw new InvalidOperationException("当前密码不正确。");
}
user.UpdatePassword(PasswordHasher.Hash(request.NewPassword));
_userAccountRepository.Update(user);
await _unitOfWork.SaveChangesAsync(cancellationToken);
}
}