TestSessionContext.cs 3.8 KB

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