TestSessionContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Security.Authentication;
  2. using System.Security.Claims;
  3. using Hotline.Authentications;
  4. using IdentityModel;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using XF.Domain.Authentications;
  8. using XF.Domain.Dependency;
  9. namespace Hotline.Tests.Controller;
  10. public class TestSessionContext : ISessionContext, IScopeDependency
  11. {
  12. private readonly IHttpContextAccessor _contextAccessor;
  13. private readonly ISessionContextProvider _changeSessionProvider;
  14. public TestSessionContext(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider)
  15. {
  16. _contextAccessor = httpContextAccessor;
  17. _changeSessionProvider = serviceProvider.GetService<ISessionContextProvider>();
  18. //_contextAccessor.HttpContext ??= new DefaultHttpContext();
  19. //if (httpContextAccessor.HttpContext == null) _contextAccessor.HttpContext = new DefaultHttpContext();
  20. }
  21. public void ChangeSession(string id)
  22. {
  23. _contextAccessor.HttpContext = _changeSessionProvider.ChangeSessionByUserId(id, _contextAccessor.HttpContext);
  24. }
  25. public async Task ChangeSessionAsync(string userId, CancellationToken cancellation)
  26. {
  27. throw new NotImplementedException();
  28. }
  29. public void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
  30. {
  31. throw new NotImplementedException();
  32. }
  33. public string? OpenId { get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.OpenId); } init { } }
  34. /// <summary>
  35. /// Id of current tenant or null for host
  36. /// </summary>
  37. public string? UserId
  38. {
  39. get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
  40. init { }
  41. }
  42. /// <summary>
  43. /// Id of current user or throw Exception for guest
  44. /// </summary>
  45. /// <exception cref="AuthenticationException"></exception>
  46. public string RequiredUserId => _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
  47. public string? UserName
  48. {
  49. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
  50. init { }
  51. }
  52. public string? Phone
  53. {
  54. get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.PhoneNumber); }
  55. init { }
  56. }
  57. /// <summary>
  58. /// Roles
  59. /// </summary>
  60. public string[] Roles
  61. {
  62. get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray(); }
  63. init { }
  64. }
  65. public string? OrgId
  66. {
  67. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
  68. init { }
  69. }
  70. public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
  71. public string? OrgName
  72. {
  73. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
  74. init { }
  75. }
  76. public int OrgLevel
  77. {
  78. get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
  79. init { }
  80. }
  81. public string? OrgAreaCode
  82. {
  83. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaCode); }
  84. init { }
  85. }
  86. public bool OrgIsCenter
  87. {
  88. get { return _contextAccessor.HttpContext?.User.FindBoolValue(AppClaimTypes.DepartmentIsCenter) ?? false; }
  89. init { }
  90. }
  91. /// <summary>
  92. /// 部门行政区划名称
  93. /// </summary>
  94. public string? OrgAreaName
  95. {
  96. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaName); }
  97. init { }
  98. }
  99. public string? AreaId
  100. {
  101. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
  102. init { }
  103. }
  104. public string? ClientId
  105. {
  106. get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.ClientId); }
  107. init { }
  108. }
  109. /// <summary>
  110. /// 工号
  111. /// </summary>
  112. public string? StaffNo
  113. {
  114. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.StaffNo); }
  115. init { }
  116. }
  117. }