TestSessionContext.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //if (httpContextAccessor.HttpContext == null) _contextAccessor.HttpContext = new DefaultHttpContext();
  19. }
  20. public void ChangeSession(string id)
  21. {
  22. _contextAccessor.HttpContext = _changeSessionProvider.ChangeSessionByUserId(id, _contextAccessor.HttpContext);
  23. }
  24. public async Task ChangeSessionAsync(string userId, CancellationToken cancellation)
  25. {
  26. throw new NotImplementedException();
  27. }
  28. public void ChangeSession(string userId, string username, string orgId, string orgname, int orgLevel)
  29. {
  30. throw new NotImplementedException();
  31. }
  32. public string? OpenId { get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.OpenId); } init { } }
  33. /// <summary>
  34. /// Id of current tenant or null for host
  35. /// </summary>
  36. public string? UserId
  37. {
  38. get { return _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); }
  39. init { }
  40. }
  41. /// <summary>
  42. /// Id of current user or throw Exception for guest
  43. /// </summary>
  44. /// <exception cref="AuthenticationException"></exception>
  45. public string RequiredUserId => _contextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
  46. public string? UserName
  47. {
  48. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.UserDisplayName); }
  49. init { }
  50. }
  51. public string? Phone
  52. {
  53. get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.PhoneNumber); }
  54. init { }
  55. }
  56. /// <summary>
  57. /// Roles
  58. /// </summary>
  59. public string[] Roles
  60. {
  61. get { return _contextAccessor.HttpContext?.User.Claims.Where(d => d.Type == JwtClaimTypes.Role).Select(d => d.Value).ToArray(); }
  62. init { }
  63. }
  64. public string? OrgId
  65. {
  66. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId); }
  67. init { }
  68. }
  69. public string RequiredOrgId => _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentId);
  70. public string? OrgName
  71. {
  72. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentName); }
  73. init { }
  74. }
  75. public int OrgLevel
  76. {
  77. get { return _contextAccessor.HttpContext?.User.FindIntValue(AppClaimTypes.DepartmentLevel) ?? 0; }
  78. init { }
  79. }
  80. public string? OrgAreaCode
  81. {
  82. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaCode); }
  83. init { }
  84. }
  85. public bool OrgIsCenter
  86. {
  87. get { return _contextAccessor.HttpContext?.User.FindBoolValue(AppClaimTypes.DepartmentIsCenter) ?? false; }
  88. init { }
  89. }
  90. /// <summary>
  91. /// 部门行政区划名称
  92. /// </summary>
  93. public string? OrgAreaName
  94. {
  95. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.DepartmentAreaName); }
  96. init { }
  97. }
  98. public string? AreaId
  99. {
  100. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.AreaId); }
  101. init { }
  102. }
  103. public string? ClientId
  104. {
  105. get { return _contextAccessor.HttpContext?.User.FindFirstValue(JwtClaimTypes.ClientId); }
  106. init { }
  107. }
  108. /// <summary>
  109. /// 工号
  110. /// </summary>
  111. public string? StaffNo
  112. {
  113. get { return _contextAccessor.HttpContext?.User.FindFirstValue(AppClaimTypes.StaffNo); }
  114. init { }
  115. }
  116. }