平安校园
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

UserEventSubscriber.cs 4.2 KiB

4 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 
  2. //
  3. using NetTaste;
  4. using SimpleRedis;
  5. namespace SafeCampus.System;
  6. /// <summary>
  7. /// 用户模块事件总线
  8. /// </summary>
  9. public class UserEventSubscriber : IEventSubscriber, ISingleton
  10. {
  11. private readonly IServiceProvider _services;
  12. private readonly ISimpleCacheService _simpleCacheService;
  13. public UserEventSubscriber(IServiceProvider services, ISimpleCacheService simpleCacheService)
  14. {
  15. _services = services;
  16. _simpleCacheService = simpleCacheService;
  17. }
  18. /// <summary>
  19. /// 根据角色ID列表清除用户缓存
  20. /// </summary>
  21. /// <param name="context"></param>
  22. /// <returns></returns>
  23. [EventSubscribe(EventSubscriberConst.CLEAR_USER_CACHE)]
  24. public async Task DeleteUserCacheByRoleIds(EventHandlerExecutingContext context)
  25. {
  26. var roleIds = (List<long>)context.Source.Payload;//获取角色ID
  27. // 创建新的作用域
  28. using var scope = _services.CreateScope();
  29. // 解析角色服务
  30. var relationService = scope.ServiceProvider.GetRequiredService<IRelationService>();
  31. //获取用户和角色关系
  32. var relations = await relationService.GetRelationListByTargetIdListAndCategory(roleIds.Select(it => it.ToString()).ToList(),
  33. CateGoryConst.RELATION_SYS_USER_HAS_ROLE);
  34. if (relations.Count > 0)
  35. {
  36. var userIds = relations.Select(it => it.ObjectId).ToList();//用户ID列表
  37. // 解析用户服务
  38. var userService = scope.ServiceProvider.GetRequiredService<ISysUserService>();
  39. //从redis中删除
  40. userService.DeleteUserFromRedis(userIds);
  41. }
  42. }
  43. /// <summary>
  44. /// 根据机构ID列表清除用户token
  45. /// </summary>
  46. /// <param name="context"></param>
  47. [EventSubscribe(EventSubscriberConst.CLEAR_ORG_TOKEN)]
  48. public async Task DeleteUserTokenByOrgIds(EventHandlerExecutingContext context)
  49. {
  50. var orgIds = (List<long>)context.Source.Payload;//获取机构ID
  51. // 创建新的作用域
  52. using var scope = _services.CreateScope();
  53. // 获取用户ID列表
  54. var userIds = await DbContext.DB.QueryableWithAttr<SysUser>().Where(it => orgIds.Contains(it.OrgId)).Select(it => it.Id).ToListAsync();
  55. //从redis中删除所属机构的用户token
  56. _simpleCacheService.HashDel<List<Token>>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
  57. }
  58. /// <summary>
  59. /// 根据模块ID列表清除用户token
  60. /// </summary>
  61. /// <param name="context"></param>
  62. [EventSubscribe(EventSubscriberConst.CLEAR_MODULE_TOKEN)]
  63. public async Task DeleteUserTokenByModuleId(EventHandlerExecutingContext context)
  64. {
  65. var moduleId = (long)context.Source.Payload;//获取机构ID
  66. // 创建新的作用域
  67. using var scope = _services.CreateScope();
  68. // 解析关系服务
  69. var relationService = scope.ServiceProvider.GetRequiredService<IRelationService>();
  70. var roleModuleRelations =
  71. await relationService.GetRelationListByTargetIdAndCategory(moduleId.ToString(), CateGoryConst.RELATION_SYS_ROLE_HAS_MODULE);//角色模块关系
  72. var userModuleRelations =
  73. await relationService.GetRelationListByTargetIdAndCategory(moduleId.ToString(), CateGoryConst.RELATION_SYS_USER_HAS_MODULE);//用户模块关系
  74. var userIds = userModuleRelations.Select(it => it.ObjectId).ToList();//用户ID列表
  75. var roleIds = roleModuleRelations.Select(it => it.ObjectId).ToList();//角色ID列表
  76. var userRoleRelations = await relationService.GetRelationListByTargetIdListAndCategory(roleIds.Select(it => it.ToString()).ToList(),
  77. CateGoryConst.RELATION_SYS_USER_HAS_ROLE);//用户角色关系
  78. userIds.AddRange(userRoleRelations.Select(it => it.ObjectId).ToList());//添加用户ID列表
  79. // 解析用户服务
  80. var userService = scope.ServiceProvider.GetRequiredService<ISysUserService>();
  81. //从redis中删除用户信息
  82. userService.DeleteUserFromRedis(userIds);
  83. //从redis中删除用户token
  84. _simpleCacheService.HashDel<List<Token>>(CacheConst.CACHE_USER_TOKEN, userIds.Select(it => it.ToString()).ToArray());
  85. }
  86. }