mirror of https://github.com/abpframework/abp.git
9 changed files with 137 additions and 36 deletions
@ -1,36 +1,53 @@ |
|||
using System.Globalization; |
|||
using System.Threading.Tasks; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.Extensions.Caching.Memory; |
|||
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations; |
|||
using Volo.Abp.AspNetCore.Mvc.Client; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Http.Client.DynamicProxying; |
|||
using Volo.Abp.Users; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly |
|||
{ |
|||
public class WebAssemblyCachedApplicationConfigurationClient : ICachedApplicationConfigurationClient, ITransientDependency |
|||
{ |
|||
protected IHttpClientProxy<IAbpApplicationConfigurationAppService> Proxy { get; } |
|||
protected ICurrentUser CurrentUser { get; } |
|||
|
|||
protected IMemoryCache Cache { get; } |
|||
|
|||
public WebAssemblyCachedApplicationConfigurationClient( |
|||
IHttpClientProxy<IAbpApplicationConfigurationAppService> proxy, |
|||
ICurrentUser currentUser) |
|||
IMemoryCache cache) |
|||
{ |
|||
Proxy = proxy; |
|||
CurrentUser = currentUser; |
|||
Cache = cache; |
|||
} |
|||
|
|||
public async Task InitializeAsync() |
|||
{ |
|||
await GetAsync(); |
|||
} |
|||
|
|||
public async Task<ApplicationConfigurationDto> GetAsync() |
|||
{ |
|||
//TODO: Cache
|
|||
return await Cache.GetOrCreateAsync( |
|||
CreateCacheKey(), |
|||
e => Proxy.Service.GetAsync() |
|||
); |
|||
} |
|||
|
|||
public ApplicationConfigurationDto Get() |
|||
{ |
|||
var cacheKey = CreateCacheKey(); |
|||
if(Cache.TryGetValue(cacheKey, out ApplicationConfigurationDto value)) |
|||
{ |
|||
return value; |
|||
} |
|||
|
|||
return await Proxy.Service.GetAsync(); |
|||
throw new AbpException($"Should initialize the {nameof(ICachedApplicationConfigurationClient)} before getting the value!"); |
|||
} |
|||
|
|||
protected virtual string CreateCacheKey() |
|||
{ |
|||
return $"ApplicationConfiguration_{CurrentUser.Id?.ToString("N") ?? "Anonymous"}_{CultureInfo.CurrentUICulture.Name}"; |
|||
return $"ApplicationConfiguration"; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,39 @@ |
|||
using System.Collections.Generic; |
|||
using System.Security.Claims; |
|||
using Volo.Abp.AspNetCore.Mvc.Client; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Security.Claims; |
|||
|
|||
namespace Volo.Abp.AspNetCore.Components.WebAssembly |
|||
{ |
|||
public class WebAssemblyCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ITransientDependency |
|||
{ |
|||
protected ICachedApplicationConfigurationClient ConfigurationClient { get; } |
|||
|
|||
public WebAssemblyCurrentPrincipalAccessor(ICachedApplicationConfigurationClient configurationClient) |
|||
{ |
|||
ConfigurationClient = configurationClient; |
|||
} |
|||
|
|||
protected override ClaimsPrincipal GetClaimsPrincipal() |
|||
{ |
|||
//TODO: Should be optimized! Or should be replaced?
|
|||
|
|||
var configuration = ConfigurationClient.Get(); |
|||
|
|||
var claims = new List<Claim>(); |
|||
|
|||
claims.Add(new Claim(AbpClaimTypes.UserName,configuration.CurrentUser.UserName)); |
|||
claims.Add(new Claim(AbpClaimTypes.Email,configuration.CurrentUser.Email)); |
|||
claims.Add(new Claim(AbpClaimTypes.UserId,configuration.CurrentUser.Id.ToString())); |
|||
claims.Add(new Claim(AbpClaimTypes.TenantId,configuration.CurrentUser.TenantId.ToString())); |
|||
|
|||
foreach (var role in configuration.CurrentUser.Roles) |
|||
{ |
|||
claims.Add(new Claim(AbpClaimTypes.Role, role)); |
|||
} |
|||
|
|||
return new ClaimsPrincipal(new ClaimsIdentity(claims)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,30 @@ |
|||
using System; |
|||
using System.Security.Claims; |
|||
using System.Threading; |
|||
|
|||
namespace Volo.Abp.Security.Claims |
|||
{ |
|||
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(() => |
|||
{ |
|||
_currentPrincipal.Value = parent; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -1,34 +1,14 @@ |
|||
using System; |
|||
using System.Security.Claims; |
|||
using System.Security.Claims; |
|||
using System.Threading; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.Abp.Security.Claims |
|||
{ |
|||
public class ThreadCurrentPrincipalAccessor : ICurrentPrincipalAccessor, ISingletonDependency |
|||
public class ThreadCurrentPrincipalAccessor : CurrentPrincipalAccessorBase, ISingletonDependency |
|||
{ |
|||
public ClaimsPrincipal Principal => _currentPrincipal.Value ?? GetClaimsPrincipal(); |
|||
|
|||
private readonly AsyncLocal<ClaimsPrincipal> _currentPrincipal = new AsyncLocal<ClaimsPrincipal>(); |
|||
|
|||
protected virtual ClaimsPrincipal GetClaimsPrincipal() |
|||
protected override ClaimsPrincipal GetClaimsPrincipal() |
|||
{ |
|||
return Thread.CurrentPrincipal as ClaimsPrincipal; |
|||
} |
|||
|
|||
public virtual IDisposable Change(ClaimsPrincipal principal) |
|||
{ |
|||
return SetCurrent(principal); |
|||
} |
|||
|
|||
private IDisposable SetCurrent(ClaimsPrincipal principal) |
|||
{ |
|||
var parent = Principal; |
|||
_currentPrincipal.Value = principal; |
|||
return new DisposeAction(() => |
|||
{ |
|||
_currentPrincipal.Value = parent; |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue