mirror of https://github.com/abpframework/abp
parent
10ea0e61d3
commit
e407754280
@ -1,13 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.AspNetCore.Mvc.Client;
|
||||
|
||||
public class RemoteDynamicClaimsPrincipalContributorCacheOptions
|
||||
{
|
||||
public TimeSpan CacheAbsoluteExpiration { get; set; }
|
||||
|
||||
public RemoteDynamicClaimsPrincipalContributorCacheOptions()
|
||||
{
|
||||
CacheAbsoluteExpiration = TimeSpan.FromSeconds(60);
|
||||
}
|
||||
}
|
||||
@ -1,22 +0,0 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.Security.Claims;
|
||||
|
||||
[Serializable]
|
||||
public class AbpClaimCacheItem
|
||||
{
|
||||
public string Type { get; set; }
|
||||
|
||||
public string? Value { get; set; }
|
||||
|
||||
public AbpClaimCacheItem(string type, string? value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public static string CalculateCacheKey(Guid userId, Guid? tenantId)
|
||||
{
|
||||
return $"{tenantId}-{userId}";
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
|
||||
namespace Volo.Abp.Security.Claims;
|
||||
|
||||
[Serializable]
|
||||
public class AbpDynamicClaim
|
||||
{
|
||||
public string Type { get; set; }
|
||||
|
||||
public string? Value { get; set; }
|
||||
|
||||
public AbpDynamicClaim(string type, string? value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Volo.Abp.Security.Claims;
|
||||
|
||||
[Serializable]
|
||||
public class AbpDynamicClaimCacheItem
|
||||
{
|
||||
public List<AbpDynamicClaim> Claims { get; set; }
|
||||
|
||||
public AbpDynamicClaimCacheItem()
|
||||
{
|
||||
Claims = new List<AbpDynamicClaim>();
|
||||
}
|
||||
|
||||
public AbpDynamicClaimCacheItem(List<AbpDynamicClaim> claims)
|
||||
{
|
||||
Claims = claims;
|
||||
}
|
||||
|
||||
public static string CalculateCacheKey(Guid userId, Guid? tenantId)
|
||||
{
|
||||
return $"{tenantId}-{userId}";
|
||||
}
|
||||
}
|
||||
@ -1,48 +1,31 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Volo.Abp.Identity;
|
||||
using Volo.Abp.Security.Claims;
|
||||
using Volo.Abp.Users;
|
||||
|
||||
namespace Volo.Abp.Account;
|
||||
|
||||
[Authorize]
|
||||
public class DynamicClaimsAppService : IdentityAppServiceBase, IDynamicClaimsAppService
|
||||
{
|
||||
protected IdentityDynamicClaimsPrincipalContributorCache IdentityDynamicClaimsPrincipalContributorCache { get; }
|
||||
protected IAbpClaimsPrincipalFactory AbpClaimsPrincipalFactory { get; }
|
||||
protected ICurrentPrincipalAccessor PrincipalAccessor { get; }
|
||||
protected IOptions<AbpClaimsPrincipalFactoryOptions> AbpClaimsPrincipalFactoryOptions { get; }
|
||||
|
||||
public DynamicClaimsAppService(
|
||||
IdentityDynamicClaimsPrincipalContributorCache identityDynamicClaimsPrincipalContributorCache,
|
||||
IAbpClaimsPrincipalFactory abpClaimsPrincipalFactory,
|
||||
ICurrentPrincipalAccessor principalAccessor,
|
||||
IOptions<AbpClaimsPrincipalFactoryOptions> abpClaimsPrincipalFactoryOptions)
|
||||
ICurrentPrincipalAccessor principalAccessor)
|
||||
{
|
||||
IdentityDynamicClaimsPrincipalContributorCache = identityDynamicClaimsPrincipalContributorCache;
|
||||
AbpClaimsPrincipalFactory = abpClaimsPrincipalFactory;
|
||||
PrincipalAccessor = principalAccessor;
|
||||
AbpClaimsPrincipalFactoryOptions = abpClaimsPrincipalFactoryOptions;
|
||||
}
|
||||
|
||||
public virtual async Task<List<DynamicClaimDto>> GetAsync()
|
||||
public virtual async Task RefreshAsync()
|
||||
{
|
||||
var principal = await AbpClaimsPrincipalFactory.CreateAsync(PrincipalAccessor.Principal);
|
||||
|
||||
var dynamicClaims = new List<DynamicClaimDto>();
|
||||
foreach (var claimType in AbpClaimsPrincipalFactoryOptions.Value.DynamicClaims)
|
||||
{
|
||||
var claims = principal.Claims.Where(x => x.Type == claimType).ToList();
|
||||
if (claims.Any())
|
||||
{
|
||||
dynamicClaims.AddRange(claims.Select(claim => new DynamicClaimDto(claimType, claim.Value)));
|
||||
}
|
||||
else
|
||||
{
|
||||
dynamicClaims.Add(new DynamicClaimDto(claimType, null));
|
||||
}
|
||||
}
|
||||
|
||||
return dynamicClaims;
|
||||
await IdentityDynamicClaimsPrincipalContributorCache.ClearAsync(CurrentUser.GetId(), CurrentUser.TenantId);
|
||||
await AbpClaimsPrincipalFactory.CreateDynamicAsync(PrincipalAccessor.Principal);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue