Quellcode durchsuchen

增加根据UserId切换Session方法

qinchaoyue vor 2 Monaten
Ursprung
Commit
c0eec5f744

+ 0 - 12
src/Hotline.Api/Controllers/CallController.cs

@@ -52,18 +52,6 @@ namespace Hotline.Api.Controllers
             _telOperationXthxRepository = telOperationXthxRepository;
         }
 
-        /// <summary>
-        /// 分机状态通知
-        /// </summary>
-        /// <param name="dto"></param>
-        /// <returns></returns>
-        [HttpPost("notify/status")]
-        [LogFilterAlpha("分机状态通知")]
-        [AllowAnonymous]
-        public async Task NotifyStatus([FromBody] NotifyStatusInDto dto)
-        {
-        }
-
         /// <summary>
         /// 查询分机
         /// </summary>

+ 5 - 0
src/Hotline.Api/Controllers/IPPbxController.cs

@@ -676,6 +676,11 @@ namespace Hotline.Api.Controllers
         [LogFilterAlpha("话机状态通知")]
         public async Task NotifyStatus(NotifyInDto dto)
         {
+            _sessionContext.ChangeSession("08dbf001-0a5c-48f0-8a54-850098119a86");
+            if (_sessionContext.UserId == "08dbf001-0a5c-48f0-8a54-850098119a86")
+            {
+                return;
+            }
             if (dto.Status == 0) // 签出
             {
                 await _iPPbxApplication.ResetTelStatus(null, dto.TelNo, CancellationToken.None);

+ 52 - 0
src/Hotline/Authentications/ChangeSessionProvider.cs

@@ -0,0 +1,52 @@
+using Hotline.Identity.Accounts;
+using Hotline.Settings;
+using Hotline.Share.Dtos.CallCenter;
+using Hotline.Users;
+using IdentityModel;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.DependencyInjection;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Claims;
+using System.Text;
+using System.Threading.Tasks;
+using XF.Domain.Authentications;
+using XF.Domain.Dependency;
+using XF.Domain.Repository;
+
+namespace Hotline.Authentications;
+public class ChangeSessionProvider : IChangeSessionProvider, IScopeDependency
+{
+    private readonly IHttpContextAccessor _contextAccessor;
+
+    public ChangeSessionProvider(IHttpContextAccessor contextAccessor)
+    {
+        _contextAccessor = contextAccessor;
+    }
+
+    public void ChangeSessionByUserId(string id, HttpContext httpContext)
+    {
+        var userRepository = _contextAccessor.HttpContext.RequestServices.GetService<IRepository<User>>();
+        var accountRepository = _contextAccessor.HttpContext.RequestServices.GetService<IAccountRepository>();
+        var user = userRepository.Queryable().Where(m => m.Id == id).First();
+        if (user == null) return;
+        var account = accountRepository.GetExtAsync(m => m.Id == user.Id, m => m.Includes(x => x.Roles)).GetAwaiter().GetResult();
+
+        List<Claim> userClaims = [
+            new(JwtClaimTypes.Subject, account.Id),
+                new(JwtClaimTypes.PhoneNumber, account.PhoneNo ?? string.Empty),
+                new(ClaimTypes.NameIdentifier, user.Id),
+                new(AppClaimTypes.UserDisplayName, account.Name),
+                new(AppClaimTypes.DepartmentId, user.OrgId ?? string.Empty),
+                new(AppClaimTypes.DepartmentIsCenter, user.Organization?.IsCenter.ToString() ?? string.Empty),
+                new(AppClaimTypes.DepartmentName, user.Organization?.Name ?? string.Empty),
+                new(AppClaimTypes.DepartmentAreaCode, user.Organization?.AreaCode ?? string.Empty),
+                new(AppClaimTypes.DepartmentAreaName, user.Organization?.AreaName ?? string.Empty),
+                new(AppClaimTypes.DepartmentLevel, user.Organization?.Level.ToString() ?? string.Empty),
+                new(AppClaimTypes.AreaId, user.OrgId?.GetHigherOrgId() ?? string.Empty),
+            ];
+        userClaims.AddRange(account.Roles.Select(d => new Claim(JwtClaimTypes.Role, d.Name)));
+        httpContext.User = new ClaimsPrincipal(new ClaimsIdentity(userClaims));
+    }
+}

+ 5 - 0
src/Hotline/Authentications/FakeSessionContext.cs

@@ -50,5 +50,10 @@ namespace Hotline.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get; init; }
+
+        public void ChangeSession(string id)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 5 - 0
src/Hotline/Authentications/Police110SessionContext.cs

@@ -62,5 +62,10 @@ namespace Hotline.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get; init; }
+
+        public void ChangeSession(string id)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 5 - 0
src/Hotline/Authentications/ProvinceSessionContext.cs

@@ -67,5 +67,10 @@ namespace Hotline.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get; init; }
+
+        public void ChangeSession(string id)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 5 - 0
src/Hotline/Authentications/YbEnterpriseSessionContext.cs

@@ -63,5 +63,10 @@ namespace Hotline.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get; init; }
+
+        public void ChangeSession(string id)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 5 - 0
src/Hotline/Authentications/ZzptSessionContext.cs

@@ -62,5 +62,10 @@ namespace Hotline.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get; init; }
+
+        public void ChangeSession(string id)
+        {
+            throw new NotImplementedException();
+        }
     }
 }

+ 10 - 0
src/XF.Domain/Authentications/DefaultSessionContext.cs

@@ -2,6 +2,7 @@
 using System.Security.Claims;
 using IdentityModel;
 using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.DependencyInjection;
 using XF.Domain.Dependency;
 using XF.Domain.Extensions;
 
@@ -10,6 +11,7 @@ namespace XF.Domain.Authentications
     public class DefaultSessionContext : ISessionContext, IScopeDependency
     {
         private readonly HttpContext _context;
+        private readonly IChangeSessionProvider _changeSessionProvider;
         public DefaultSessionContext(IHttpContextAccessor httpContextAccessor)
         {
             var httpContext = httpContextAccessor.HttpContext;
@@ -18,6 +20,9 @@ namespace XF.Domain.Authentications
                 return;
 
             _context = httpContext;
+            if (_context.Items.Any(m => m.Key == nameof(DefaultSessionContext))) return;
+            _context.Items.Add(nameof(DefaultSessionContext), nameof(IChangeSessionProvider));
+            _changeSessionProvider = httpContextAccessor.HttpContext.RequestServices.GetService<IChangeSessionProvider>();
         }
 
         /// <summary>
@@ -68,6 +73,11 @@ namespace XF.Domain.Authentications
         /// 例如: 微信的OpenId
         /// </summary>
         public string? OpenId { get => _context.User.FindFirstValue(AppClaimTypes.OpenId); init { } }
+
+        public void ChangeSession(string id)
+        {
+            _changeSessionProvider.ChangeSessionByUserId(id, _context);
+        }
     }
 
     public static class ClaimsPrincipalExtensions

+ 12 - 0
src/XF.Domain/Authentications/IChangeSessionProvider.cs

@@ -0,0 +1,12 @@
+using Microsoft.AspNetCore.Http;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace XF.Domain.Authentications;
+public interface IChangeSessionProvider
+{
+    void ChangeSessionByUserId(string id, HttpContext httpContext);
+}

+ 2 - 0
src/XF.Domain/Authentications/ISessionContext.cs

@@ -51,4 +51,6 @@ public interface ISessionContext
     /// 例如: 微信的OpenId
     /// </summary>
     string? OpenId { get; init; }
+
+    void ChangeSession(string id);
 }