feat: 添加管理员密码修改功能
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user