平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuthEventSubscriber.cs 6.0 KiB

4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. 
  2. //
  3. using IP2Region.Net.Abstractions;
  4. namespace SafeCampus.System;
  5. /// <summary>
  6. /// 认证模块事件总线
  7. /// </summary>
  8. public class AuthEventSubscriber : IEventSubscriber, ISingleton
  9. {
  10. private readonly ISimpleCacheService _simpleCacheService;
  11. private readonly ISearcher _searcher;
  12. public IServiceProvider Services { get; }
  13. private readonly SqlSugarScope _db;
  14. public AuthEventSubscriber(ISimpleCacheService simpleCacheService, IServiceProvider services, ISearcher searcher)
  15. {
  16. _db = DbContext.DB;
  17. _simpleCacheService = simpleCacheService;
  18. _searcher = searcher;
  19. Services = services;
  20. }
  21. /// <summary>
  22. /// 登录事件
  23. /// </summary>
  24. /// <param name="context"></param>
  25. /// <returns></returns>
  26. [EventSubscribe(EventSubscriberConst.LOGIN_B)]
  27. public async Task Login(EventHandlerExecutingContext context)
  28. {
  29. var loginEvent = (LoginEvent)context.Source.Payload;//获取参数
  30. var loginAddress = GetLoginAddress(loginEvent.Ip);
  31. var sysUser = loginEvent.SysUser;
  32. #region 登录/密码策略
  33. var key = SystemConst.CACHE_LOGIN_ERROR_COUNT + sysUser.Account;//获取登录错误次数Key值
  34. _simpleCacheService.Remove(key);//移除登录错误次数
  35. // 创建新的作用域
  36. using var scope = Services.CreateScope();
  37. // 解析服务
  38. var configService = scope.ServiceProvider.GetRequiredService<IConfigService>();
  39. var messageService = scope.ServiceProvider.GetRequiredService<IMessageService>();
  40. var pwdRemindUpdateTime = sysUser.PwdRemindUpdateTime;//获取上次提醒修改密码时间
  41. var loginPolicy = await configService.GetConfigsByCategory(CateGoryConst.CONFIG_PWD_POLICY);//获取密码策略
  42. //获取用户token列表
  43. var tokenInfos = _simpleCacheService.HashGetOne<List<TokenInfo>>(CacheConst.CACHE_USER_TOKEN, sysUser.Id.ToString());
  44. var userToken = tokenInfos.Where(it => it.Token == loginEvent.Token).FirstOrDefault();
  45. if (userToken != null)
  46. {
  47. var subject = "请及时修改密码";
  48. //如果上次修改密码时间为空
  49. if (pwdRemindUpdateTime == null)
  50. {
  51. var pwdUpdateDefault = loginPolicy.First(x => x.ConfigKey == SysConfigConst.PWD_UPDATE_DEFAULT).ConfigValue.ToBoolean();//获取初始化提醒
  52. //如果密码初始化提醒为true
  53. if (pwdUpdateDefault)
  54. {
  55. await messageService.Send(new MessageSendInput
  56. {
  57. Subject = subject,
  58. Content = "请及时修改初始密码",
  59. Category = CateGoryConst.MESSAGE_INFORM,
  60. ReceiverIdList = new List<long> { sysUser.Id }
  61. });
  62. }
  63. sysUser.PwdRemindUpdateTime = DateTime.Now;//设置提醒时密码时间为当前时间
  64. }
  65. else
  66. {
  67. var pwdRemind = loginPolicy.First(x => x.ConfigKey == SysConfigConst.PWD_REMIND).ConfigValue.ToBoolean();//获取密码提醒天数
  68. if (pwdRemind)
  69. {
  70. var pwdRemindDay = loginPolicy.First(x => x.ConfigKey == SysConfigConst.PWD_REMIND_DAY).ConfigValue.ToInt();//获取密码提醒时间
  71. if (DateTime.Now - pwdRemindUpdateTime > TimeSpan.FromDays(pwdRemindDay))
  72. {
  73. await messageService.Send(new MessageSendInput
  74. {
  75. Subject = subject,
  76. Content = $"已超过{pwdRemindDay}天未修改密码,请及时修改密码",
  77. Category = CateGoryConst.MESSAGE_INFORM,
  78. ReceiverIdList = new List<long> { sysUser.Id }
  79. });
  80. }
  81. sysUser.PwdRemindUpdateTime = DateTime.Now;//设置提醒时密码时间为当前时间,避免重复提醒
  82. }
  83. }
  84. }
  85. #endregion 登录/密码策略
  86. #region 重新赋值属性,设置本次登录信息为最新的信息
  87. sysUser.LastLoginAddress = sysUser.LatestLoginAddress;
  88. sysUser.LastLoginDevice = sysUser.LatestLoginDevice;
  89. sysUser.LastLoginIp = sysUser.LatestLoginIp;
  90. sysUser.LastLoginTime = sysUser.LatestLoginTime;
  91. sysUser.LatestLoginAddress = loginAddress;
  92. sysUser.LatestLoginDevice = loginEvent.Device.ToString();
  93. sysUser.LatestLoginIp = loginEvent.Ip;
  94. sysUser.LatestLoginTime = loginEvent.DateTime;
  95. #endregion 重新赋值属性,设置本次登录信息为最新的信息
  96. //更新用户登录信息
  97. if (await _db.UpdateableWithAttr(sysUser).UpdateColumns(it => new
  98. {
  99. it.LastLoginAddress,
  100. it.LastLoginDevice,
  101. it.LastLoginIp,
  102. it.LastLoginTime,
  103. it.LatestLoginAddress,
  104. it.LatestLoginDevice,
  105. it.LatestLoginIp,
  106. it.LatestLoginTime,
  107. LastUpdatePwdTime = it.PwdRemindUpdateTime
  108. }).ExecuteCommandAsync() > 0)
  109. _simpleCacheService.HashAdd(SystemConst.CACHE_SYS_USER, sysUser.Id.ToString(), sysUser);//更新Redis信息
  110. await Task.CompletedTask;
  111. }
  112. /// <summary>
  113. /// 登出事件
  114. /// </summary>
  115. /// <param name="context"></param>
  116. /// <returns></returns>
  117. [EventSubscribe(EventSubscriberConst.LOGIN_OUT_B)]
  118. public async Task LoginOut(EventHandlerExecutingContext context)
  119. {
  120. _ = (LoginEvent)context.Source.Payload;//获取参数
  121. }
  122. /// <summary>
  123. /// 解析IP地址
  124. /// </summary>
  125. /// <param name="ip">ip地址</param>
  126. /// <returns></returns>
  127. private string GetLoginAddress(string ip)
  128. {
  129. try
  130. {
  131. var ipInfo = _searcher.Search(ip).Replace("0|", "");//解析ip并格式化
  132. return ipInfo;
  133. }
  134. catch
  135. {
  136. return "未知";
  137. }
  138. }
  139. }