新增管理员密码重置
This commit is contained in:
parent
16929a2ce0
commit
9203788e1d
7
LMS.Repository/User/ResetPasswordModel.cs
Normal file
7
LMS.Repository/User/ResetPasswordModel.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
|
||||||
|
public class ResetPasswordModel
|
||||||
|
{
|
||||||
|
[Required(ErrorMessage = "新密码是必填项")]
|
||||||
|
public required string NewPassword { get; set; }
|
||||||
|
}
|
||||||
@ -242,5 +242,17 @@ namespace LMS.service.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 管理员重置用户密码
|
||||||
|
|
||||||
|
[HttpPost("{id}")]
|
||||||
|
[Authorize]
|
||||||
|
public async Task<ActionResult<APIResponseModel<string>>> ResetPassword(long id, [FromBody] ResetPasswordModel newPassword)
|
||||||
|
{
|
||||||
|
long requestUserId = ConvertExtension.ObjectToLong(HttpContext.Items["UserId"] ?? 0);
|
||||||
|
return await _loginService.ResetPassword(id, newPassword, requestUserId);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
|
|
||||||
using LMS.Common.RSAKey;
|
using LMS.Common.RSAKey;
|
||||||
using LMS.DAO;
|
using LMS.DAO;
|
||||||
|
using LMS.DAO.UserDAO;
|
||||||
using LMS.Repository.DB;
|
using LMS.Repository.DB;
|
||||||
using LMS.Repository.Models.DB;
|
using LMS.Repository.Models.DB;
|
||||||
using LMS.Repository.Models.User;
|
using LMS.Repository.Models.User;
|
||||||
@ -18,18 +19,12 @@ using static LMS.Common.Enums.ResponseCodeEnum;
|
|||||||
|
|
||||||
namespace LMS.service.Service.UserService
|
namespace LMS.service.Service.UserService
|
||||||
{
|
{
|
||||||
public class LoginService
|
public class LoginService(UserManager<User> userManager, ApplicationDbContext context, SecurityService securityService, UserBasicDao userBasicDao)
|
||||||
{
|
{
|
||||||
private readonly UserManager<User> _userManager;
|
private readonly UserManager<User> _userManager = userManager;
|
||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context = context;
|
||||||
private readonly SecurityService _securityService;
|
private readonly SecurityService _securityService = securityService;
|
||||||
|
private readonly UserBasicDao _userBasicDao = userBasicDao;
|
||||||
public LoginService(UserManager<User> userManager, ApplicationDbContext context, SecurityService securityService)
|
|
||||||
{
|
|
||||||
_userManager = userManager;
|
|
||||||
_context = context;
|
|
||||||
_securityService = securityService;
|
|
||||||
}
|
|
||||||
|
|
||||||
#region 生成JWT
|
#region 生成JWT
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -402,5 +397,66 @@ namespace LMS.service.Service.UserService
|
|||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 管理员重置用户密码
|
||||||
|
/// <summary>
|
||||||
|
/// 管理员重置用户密码
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">被重置的用户ID</param>
|
||||||
|
/// <param name="newPassword">新密码</param>
|
||||||
|
/// <param name="requestUserId">请求的用户ID</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public async Task<ActionResult<APIResponseModel<string>>> ResetPassword(long id, ResetPasswordModel newPassword, long requestUserId)
|
||||||
|
{
|
||||||
|
using var transaction = await _context.Database.BeginTransactionAsync();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(newPassword.NewPassword))
|
||||||
|
{
|
||||||
|
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.ParameterError, "新密码必填");
|
||||||
|
}
|
||||||
|
// 检查当前用户是不是超级管理员
|
||||||
|
bool isSuperAdmin = await _userBasicDao.CheckUserIsSuperAdmin(requestUserId);
|
||||||
|
if (!isSuperAdmin)
|
||||||
|
{
|
||||||
|
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.NotPermissionAction);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查找需要重置密码的用户
|
||||||
|
var user = await _userManager.FindByIdAsync(id.ToString());
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
return APIResponseModel<string>.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<string>.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<string>.CreateErrorResponseModel(ResponseCode.SystemError, $"重置密码失败:{errors}");
|
||||||
|
}
|
||||||
|
await transaction.CommitAsync();
|
||||||
|
return APIResponseModel<string>.CreateSuccessResponseModel("密码已成功重置");
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync();
|
||||||
|
return APIResponseModel<string>.CreateErrorResponseModel(ResponseCode.SystemError, e.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user