diff --git a/LMS.Repository/User/ResetPasswordModel.cs b/LMS.Repository/User/ResetPasswordModel.cs new file mode 100644 index 0000000..2c03828 --- /dev/null +++ b/LMS.Repository/User/ResetPasswordModel.cs @@ -0,0 +1,7 @@ +using System.ComponentModel.DataAnnotations; + +public class ResetPasswordModel +{ + [Required(ErrorMessage = "新密码是必填项")] + public required string NewPassword { get; set; } +} \ No newline at end of file diff --git a/LMS.service/Controllers/UserController.cs b/LMS.service/Controllers/UserController.cs index ad6dc1a..f9e3d18 100644 --- a/LMS.service/Controllers/UserController.cs +++ b/LMS.service/Controllers/UserController.cs @@ -180,7 +180,7 @@ namespace LMS.service.Controllers #endregion - #region 获取用户列表 + #region 获取用户列表 /// /// 获取用户列表,可以使用查询条件 userName, userId, nickName, phoneNumber, email, roleNames @@ -242,5 +242,17 @@ namespace LMS.service.Controllers } #endregion + + #region 管理员重置用户密码 + + [HttpPost("{id}")] + [Authorize] + public async Task>> ResetPassword(long id, [FromBody] ResetPasswordModel newPassword) + { + long requestUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0); + return await _loginService.ResetPassword(id, newPassword, requestUserId); + } + #endregion + } } diff --git a/LMS.service/Service/UserService/LoginService.cs b/LMS.service/Service/UserService/LoginService.cs index 09bf9b6..92c6303 100644 --- a/LMS.service/Service/UserService/LoginService.cs +++ b/LMS.service/Service/UserService/LoginService.cs @@ -1,6 +1,7 @@  using LMS.Common.RSAKey; using LMS.DAO; +using LMS.DAO.UserDAO; using LMS.Repository.DB; using LMS.Repository.Models.DB; using LMS.Repository.Models.User; @@ -18,18 +19,12 @@ using static LMS.Common.Enums.ResponseCodeEnum; namespace LMS.service.Service.UserService { - public class LoginService + public class LoginService(UserManager userManager, ApplicationDbContext context, SecurityService securityService, UserBasicDao userBasicDao) { - private readonly UserManager _userManager; - private readonly ApplicationDbContext _context; - private readonly SecurityService _securityService; - - public LoginService(UserManager userManager, ApplicationDbContext context, SecurityService securityService) - { - _userManager = userManager; - _context = context; - _securityService = securityService; - } + private readonly UserManager _userManager = userManager; + private readonly ApplicationDbContext _context = context; + private readonly SecurityService _securityService = securityService; + private readonly UserBasicDao _userBasicDao = userBasicDao; #region 生成JWT /// @@ -402,5 +397,66 @@ namespace LMS.service.Service.UserService } #endregion + + #region 管理员重置用户密码 + /// + /// 管理员重置用户密码 + /// + /// 被重置的用户ID + /// 新密码 + /// 请求的用户ID + /// + public async Task>> ResetPassword(long id, ResetPasswordModel newPassword, long requestUserId) + { + using var transaction = await _context.Database.BeginTransactionAsync(); + try + { + if (string.IsNullOrWhiteSpace(newPassword.NewPassword)) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.ParameterError, "新密码必填"); + } + // 检查当前用户是不是超级管理员 + bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId); + if (!isSuperAdmin) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.NotPermissionAction); + } + + // 查找需要重置密码的用户 + var user = await _userManager.FindByIdAsync(id.ToString()); + if (user == null) + { + return APIResponseModel.CreateErrorResponseModel(ResponseCode.FindUserByIdFail); + } + + // 移除用户当前密码(如果用户没有密码,则需要跳过此步骤) + var hasPassword = await _userManager.HasPasswordAsync(user); + if (hasPassword) + { + var removePasswordResult = await _userManager.RemovePasswordAsync(user); + if (!removePasswordResult.Succeeded) + { + var errors = string.Join("; ", removePasswordResult.Errors.Select(e => e.Description)); + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, $"移除旧密码失败:{errors}"); + } + } + + // 为用户设置新密码 + var addPasswordResult = await _userManager.AddPasswordAsync(user, newPassword.NewPassword); + if (!addPasswordResult.Succeeded) + { + var errors = string.Join("; ", addPasswordResult.Errors.Select(e => e.Description)); + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, $"重置密码失败:{errors}"); + } + await transaction.CommitAsync(); + return APIResponseModel.CreateSuccessResponseModel("密码已成功重置"); + } + catch (Exception e) + { + await transaction.RollbackAsync(); + return APIResponseModel.CreateErrorResponseModel(ResponseCode.SystemError, e.Message); + } + } + #endregion } }