TestSessionContextManager.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Hotline.Authentications;
  2. using Hotline.Caching.Interfaces;
  3. using Hotline.Configurations;
  4. using Hotline.Identity.Accounts;
  5. using Hotline.Settings;
  6. using Hotline.Share.Tools;
  7. using IdentityModel;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.Extensions.DependencyInjection;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Security.Claims;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using XF.Domain.Authentications;
  17. using XF.Domain.Dependency;
  18. using XF.Domain.Exceptions;
  19. using XF.Domain.Repository;
  20. namespace Hotline.Tests.Controller;
  21. public class TestSessionContextManager : ISessionContextManager, IScopeDependency
  22. {
  23. private readonly IServiceProvider _serviceProvider;
  24. private readonly ISystemSettingCacheManager _systemSettingCacheManager;
  25. public TestSessionContextManager(
  26. IServiceProvider serviceProvider,
  27. ISystemSettingCacheManager systemSettingCacheManager)
  28. {
  29. _serviceProvider = serviceProvider;
  30. _systemSettingCacheManager = systemSettingCacheManager;
  31. }
  32. public async Task ChangeSessionContextByUserIdAsync(string userId, CancellationToken cancellation)
  33. {
  34. var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
  35. if (httpContextAccessor.HttpContext == null)
  36. httpContextAccessor.HttpContext = new DefaultHttpContext()
  37. {
  38. RequestServices = _serviceProvider
  39. };
  40. var accountRepository = _serviceProvider.GetService<IRepository<Account>>();
  41. var account = await accountRepository.Queryable()
  42. .Includes(d => d.User, x => x.Organization)
  43. .Includes(d => d.Roles)
  44. .FirstAsync(d => d.Id == userId, cancellation);
  45. List<Claim> userClaims =
  46. [
  47. new(JwtClaimTypes.Subject, account.Id),
  48. new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
  49. new(ClaimTypes.NameIdentifier, account.Id),
  50. new(AppClaimTypes.UserDisplayName, account.Name),
  51. new(AppClaimTypes.DepartmentId, account.User?.OrgId ?? string.Empty),
  52. new(AppClaimTypes.DepartmentIsCenter, account.User?.Organization?.IsCenter.ToString() ?? string.Empty),
  53. new(AppClaimTypes.DepartmentName, account.User?.Organization?.Name ?? string.Empty),
  54. new(AppClaimTypes.DepartmentAreaCode, account.User?.Organization?.AreaCode ?? string.Empty),
  55. new(AppClaimTypes.DepartmentAreaName, account.User?.Organization?.AreaName ?? string.Empty),
  56. new(AppClaimTypes.DepartmentLevel, account.User?.Organization?.Level.ToString() ?? string.Empty),
  57. new(AppClaimTypes.AreaId, account.User?.OrgId?.GetHigherOrgId() ?? string.Empty),
  58. ];
  59. userClaims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
  60. httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
  61. }
  62. /// <summary>
  63. /// 依据source创建对应Session
  64. /// </summary>
  65. public void ChangeSessionContext(string source)
  66. {
  67. var setting = _systemSettingCacheManager.GetSetting(SettingConstants.CityBaseConfiguration)?.SettingValue[0];
  68. var cityBase = setting.FromJson<CityBaseConfiguration>();
  69. DefaultCityBaseConfiguration config = source switch
  70. {
  71. "province" => cityBase.CityProvinceAssign,
  72. "110" => cityBase.PublicSecurity,
  73. "yb-enterprise" => cityBase.CityEnterprise,
  74. "zzpt" => cityBase.ComprehensiveTreatment,
  75. _ => throw new ArgumentOutOfRangeException(nameof(source), source, null)
  76. };
  77. ChangeSession(config.UserId, config.UserName, config.OrgId, config.OrgName, config.OrgId.CalcOrgLevel());
  78. }
  79. #region private
  80. private void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
  81. {
  82. var httpContextAccessor = _serviceProvider.GetRequiredService<IHttpContextAccessor>();
  83. if (httpContextAccessor.HttpContext == null)
  84. throw new UserFriendlyException("current httpContext is null");
  85. List<Claim> userClaims =
  86. [
  87. new(JwtClaimTypes.Subject, userId),
  88. new(ClaimTypes.NameIdentifier, userId),
  89. new(AppClaimTypes.UserDisplayName, username),
  90. new(AppClaimTypes.DepartmentId, orgId ?? string.Empty),
  91. new(AppClaimTypes.DepartmentIsCenter, orgId?.IsCenter().ToString() ?? string.Empty),
  92. new(AppClaimTypes.DepartmentName, orgname ?? string.Empty),
  93. new(AppClaimTypes.DepartmentLevel, orgLevel.ToString() ?? string.Empty),
  94. new(AppClaimTypes.AreaId, orgId?.GetHigherOrgId() ?? string.Empty),
  95. ];
  96. httpContextAccessor.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
  97. }
  98. #endregion
  99. }