chore:目录重构
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
using System.Security.Claims;
|
||||
using Yi.Framework.Infrastructure.Utils;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers.Accessor
|
||||
{
|
||||
public abstract class CurrentPrincipalAccessorBase : ICurrentPrincipalAccessor
|
||||
{
|
||||
public ClaimsPrincipal Principal => _currentPrincipal.Value ?? GetClaimsPrincipal();
|
||||
|
||||
private readonly AsyncLocal<ClaimsPrincipal> _currentPrincipal = new AsyncLocal<ClaimsPrincipal>();
|
||||
|
||||
protected abstract ClaimsPrincipal GetClaimsPrincipal();
|
||||
|
||||
public virtual IDisposable Change(ClaimsPrincipal principal)
|
||||
{
|
||||
return SetCurrent(principal);
|
||||
}
|
||||
|
||||
private IDisposable SetCurrent(ClaimsPrincipal principal)
|
||||
{
|
||||
var parent = Principal;
|
||||
_currentPrincipal.Value = principal;
|
||||
return new DisposeAction<ValueTuple<AsyncLocal<ClaimsPrincipal>, ClaimsPrincipal>>(static (state) =>
|
||||
{
|
||||
var (currentPrincipal, parent) = state;
|
||||
currentPrincipal.Value = parent;
|
||||
}, (_currentPrincipal, parent));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers.Accessor
|
||||
{
|
||||
public class HttpContextCurrentPrincipalAccessor : ThreadCurrentPrincipalAccessor
|
||||
{
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public HttpContextCurrentPrincipalAccessor(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
protected override ClaimsPrincipal GetClaimsPrincipal()
|
||||
{
|
||||
return _httpContextAccessor.HttpContext?.User ?? base.GetClaimsPrincipal();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers.Accessor
|
||||
{
|
||||
public interface ICurrentPrincipalAccessor
|
||||
{
|
||||
ClaimsPrincipal Principal { get; }
|
||||
IDisposable Change(ClaimsPrincipal principal);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers.Accessor
|
||||
{
|
||||
public class StaticPrincipalAccessor : CurrentPrincipalAccessorBase
|
||||
{
|
||||
public static ClaimsPrincipal ClaimsPrincipal { get; set; }
|
||||
protected override ClaimsPrincipal GetClaimsPrincipal()
|
||||
{
|
||||
return ClaimsPrincipal;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Security.Claims;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers.Accessor
|
||||
{
|
||||
public class ThreadCurrentPrincipalAccessor : CurrentPrincipalAccessorBase
|
||||
{
|
||||
protected override ClaimsPrincipal GetClaimsPrincipal()
|
||||
{
|
||||
return Thread.CurrentPrincipal as ClaimsPrincipal;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Infrastructure.Const;
|
||||
using Yi.Framework.Infrastructure.CurrentUsers.Accessor;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers
|
||||
{
|
||||
public class CurrentUser : ICurrentUser
|
||||
{
|
||||
private readonly ICurrentPrincipalAccessor _principalAccessor;
|
||||
public CurrentUser(ICurrentPrincipalAccessor principalAccessor)
|
||||
{
|
||||
_principalAccessor = principalAccessor;
|
||||
}
|
||||
public bool IsAuthenticated => Id != 0;
|
||||
|
||||
public long Id => FindUserId();
|
||||
|
||||
public string UserName => this.FindClaimValue(TokenTypeConst.UserName);
|
||||
|
||||
/// <summary>
|
||||
/// 暂时为默认值
|
||||
/// </summary>
|
||||
public Guid TenantId { get; set; } = Guid.Empty;
|
||||
|
||||
public string Email => FindClaimValue(TokenTypeConst.Email);
|
||||
|
||||
public bool EmailVerified => false;
|
||||
|
||||
public string PhoneNumber => FindClaimValue(TokenTypeConst.PhoneNumber);
|
||||
|
||||
public bool PhoneNumberVerified => false;
|
||||
|
||||
public string[]? Roles => this.FindClaims(TokenTypeConst.Roles).Select(c => c.Value).Distinct().ToArray();
|
||||
|
||||
public string[]? Permission => this.FindClaims(TokenTypeConst.Permission).Select(c => c.Value).Distinct().ToArray();
|
||||
|
||||
public virtual Claim FindClaim(string claimType)
|
||||
{
|
||||
return _principalAccessor.Principal?.Claims.FirstOrDefault(c => c.Type == claimType);
|
||||
}
|
||||
|
||||
public virtual Claim[] FindClaims(string claimType)
|
||||
{
|
||||
return _principalAccessor.Principal?.Claims.Where(c => c.Type == claimType).ToArray() ?? new Claim[0];
|
||||
}
|
||||
|
||||
public virtual Claim[] GetAllClaims()
|
||||
{
|
||||
return _principalAccessor.Principal?.Claims.ToArray() ?? new Claim[0];
|
||||
}
|
||||
|
||||
public string FindClaimValue(string claimType)
|
||||
{
|
||||
return FindClaim(claimType)?.Value;
|
||||
}
|
||||
|
||||
|
||||
public long FindUserId()
|
||||
{
|
||||
var userIdOrNull = _principalAccessor.Principal?.Claims?.FirstOrDefault(c => c.Type == TokenTypeConst.Id);
|
||||
if (userIdOrNull == null || string.IsNullOrWhiteSpace(userIdOrNull.Value))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (long.TryParse(userIdOrNull.Value, out long userId))
|
||||
{
|
||||
return userId;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.CurrentUsers
|
||||
{
|
||||
public interface ICurrentUser
|
||||
{
|
||||
public bool IsAuthenticated { get; }
|
||||
public long Id { get; }
|
||||
|
||||
public string UserName { get; }
|
||||
|
||||
public Guid TenantId { get; }
|
||||
|
||||
public string Email { get; }
|
||||
|
||||
public bool EmailVerified { get; }
|
||||
|
||||
public string PhoneNumber { get; }
|
||||
|
||||
public bool PhoneNumberVerified { get; }
|
||||
|
||||
public string[]? Roles { get; }
|
||||
|
||||
public string[]? Permission { get; }
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user