平安校园
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.
 
 
 
 
 
 

103 lignes
4.2 KiB

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