AuthService.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using ERP.Core.Entities;
  2. using ERP.Core.Interface;
  3. using ERP.Core.Repositorys;
  4. using ERP.Core.Resources;
  5. using ERP.Framework.Cache;
  6. using ERP.Framework.Config;
  7. using ERP.Framework.Constants;
  8. using ERP.Framework.Enum;
  9. using ERP.Framework.Exceptions;
  10. using ERP.Framework.Security.Core;
  11. using ERP.Framework.Utils;
  12. using Microsoft.Extensions.Localization;
  13. namespace ERP.Core.Services
  14. {
  15. public class AuthService : IAuthService
  16. {
  17. private readonly IStringLocalizer<Core18N> _localizer;
  18. private readonly SysUserRepository _sysUserRepository;
  19. private readonly SysRoleRepository _sysRoleRepository;
  20. private readonly SysMenuRepository _sysMenuRepository;
  21. public AuthService(IStringLocalizer<Core18N> localizer, SysUserRepository sysUserRepository, SysRoleRepository sysRoleRepository, SysMenuRepository sysMenuRepository)
  22. {
  23. _localizer = localizer;
  24. this._sysUserRepository = sysUserRepository;
  25. this._sysRoleRepository = sysRoleRepository;
  26. this._sysMenuRepository = sysMenuRepository;
  27. }
  28. /// <summary>
  29. /// 获取登录用户
  30. /// </summary>
  31. /// <param name="securityConfig">设置</param>r
  32. /// <param name="loginName">用户名</param>
  33. /// <param name="password">密码</param>
  34. /// <param name="deviceKey">设备唯一值</param>
  35. /// <returns></returns>
  36. /// <exception cref="Exception"></exception>
  37. public LoginUser GetLoginUser(
  38. SecurityConfig securityConfig
  39. , string loginName
  40. , string password)
  41. {
  42. var user = _sysUserRepository.FirstOrDefault(t => t.UserName == loginName);
  43. if (user == null)
  44. {
  45. throw new CodeException(message: string.Format(_localizer["User.NotExist"], loginName), code: ErrorCode.USER_NOT_EXIST);
  46. }
  47. CheckUserStatus(user);
  48. CheckLogin(securityConfig, password, user);
  49. var roleList = _sysRoleRepository.SelectRoleListByUserId(user.Id);
  50. var roleKeyList = roleList.Select(t => t.RoleKey).ToList();
  51. var permissions = new List<string>();
  52. if (roleList.Any(t => t.RoleKey == FrameworkConstant.ADMIN))
  53. {
  54. permissions.Add(FrameworkConstant.ADMIN_PERMISSION);
  55. }
  56. else
  57. {
  58. var roleIds = roleList.Select(t => t.Id).ToList();
  59. permissions = _sysMenuRepository.SelectPermissionsByRoleIds(roleIds);
  60. }
  61. return new LoginUser()
  62. {
  63. UserId = user.Id,
  64. UserName = user.UserName,
  65. NickName = user.NickName,
  66. Avatar = user.Avatar,
  67. Email = user.Email,
  68. UserStatus = user.UserStatus,
  69. RoleKeys = roleKeyList,
  70. Permission = permissions
  71. };
  72. }
  73. private void CheckUserStatus(SysUser user)
  74. {
  75. if (user.UserStatus == StatusEnum.Enable)
  76. {
  77. return;
  78. }
  79. else if (user.UserStatus == StatusEnum.Disable)
  80. {
  81. throw new CodeException(message: string.Format(_localizer["User.Bloked"], user.UserName), code: ErrorCode.USER_BLOCK);
  82. }
  83. }
  84. private void CheckLogin(SecurityConfig securityConfig, string password, SysUser loginUser)
  85. {
  86. var passwordErrorRedisKey = AuthConstant.PASSWORD_ERROR + IPUtil.GetClientIp();
  87. var errorNumber = RedisHelper.Get(passwordErrorRedisKey);
  88. var hasErrorRedis = !errorNumber.IsNullOrEmpty();
  89. if (hasErrorRedis && errorNumber.ToInt() >= securityConfig.MaxRetryCount)
  90. {
  91. // Todo 记录登录信息(超出限制期间登录)
  92. throw new CodeException(message: string.Format(_localizer["User.Password.RetryLimitExceed"], errorNumber, securityConfig.LockTime), code: ErrorCode.USER_PASSWORD_RETRYLIMITEXCEED);
  93. }
  94. var isLogin = CryptoUtil.BCValify(password, loginUser.Password);
  95. if (!isLogin)
  96. {
  97. if (!hasErrorRedis)
  98. {
  99. RedisHelper.SaveExpire(passwordErrorRedisKey, AuthConstant.FIRST_PASSWORD_ERROR, securityConfig.LockTime);
  100. }
  101. else
  102. {
  103. var number = int.Parse(errorNumber) + 1;
  104. RedisHelper.Save(passwordErrorRedisKey, number.ToString());
  105. }
  106. throw new CodeException(message: _localizer["User.Password.Error"], code: ErrorCode.PASSWORD_ERROR);
  107. }
  108. if (hasErrorRedis)
  109. {
  110. RedisHelper.Delete(passwordErrorRedisKey);
  111. }
  112. }
  113. }
  114. }