SysUserService.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Microsoft.Extensions.Options;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 系统用户服务
  7. /// </summary>
  8. public class SysUserService : ISysUserService, ITransient
  9. {
  10. private readonly IRepository<SysUser> _userRep;
  11. private readonly AuthOptions _authOptions;
  12. public SysUserService(IRepository<SysUser> userRep, IOptions<AuthOptions> options)
  13. {
  14. _userRep = userRep;
  15. _authOptions = options.Value;
  16. }
  17. /// <summary>
  18. /// 获取所有用户列表
  19. /// </summary>
  20. /// <returns></returns>
  21. public async Task<List<SysUserLiteOutput>> GetAllList()
  22. {
  23. var items = await _userRep.DetachedEntities.Where(u => u.Status == CommonStatus.ENABLE)
  24. .ProjectToType<SysUserLiteOutput>()
  25. .ToListAsync();
  26. return items;
  27. }
  28. /// <summary>
  29. /// 修改密码
  30. /// </summary>
  31. /// <param name="input"></param>
  32. /// <returns></returns>
  33. public async Task ChangePassword(ChangeSysUserPasswordInput input)
  34. {
  35. string oldPwd = RSAEncryption.Decrypt(input.OldPassword, _authOptions.RsaPrivateKey);
  36. string newPwd = RSAEncryption.Decrypt(input.NewPassword, _authOptions.RsaPrivateKey);
  37. if (newPwd.Length < 6 || newPwd.Length > 32)
  38. {
  39. throw Oops.Oh(ErrorCode.E1006);
  40. }
  41. if ((await _userRep.DetachedEntities.AsQueryable().ToListAsync()).Any(t => t.Id == CurrentSysUserInfo.SysUserId && AESEncryption.Decrypt(t.Password, _authOptions.AesPassword).Equals(oldPwd)))
  42. {
  43. var user = await _userRep.FirstOrDefaultAsync(t => t.Id == CurrentSysUserInfo.SysUserId);
  44. user.Password = AESEncryption.Encrypt(newPwd, _authOptions.AesPassword);
  45. await user.UpdateIncludeNowAsync(new[] { "Password" });
  46. }
  47. else
  48. {
  49. throw Oops.Oh(ErrorCode.E1005);
  50. }
  51. }
  52. }