AuthService.cs 4.5 KB

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