|
@@ -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));
|
|
|
|
+ }
|
|
|
|
+}
|