平安校园
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

165 lignes
6.0 KiB

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