last ...
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using xCommons.Extensions;
|
||||
using xExceptions.Constants;
|
||||
using xIdentityService.Interfaces;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Collections.Concurrent;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
|
||||
namespace xIdentityService.Models
|
||||
{
|
||||
public class XAuthorizationCache : IXAuthorizationCache
|
||||
{
|
||||
private readonly ILogger<XAuthorizationCache> logger;
|
||||
private readonly ConcurrentDictionary<string, AuthorizationPolicy> policies;
|
||||
|
||||
public XAuthorizationCache(
|
||||
ILogger<XAuthorizationCache> logger
|
||||
)
|
||||
{
|
||||
//
|
||||
this.logger = logger;
|
||||
policies = new ConcurrentDictionary<string, AuthorizationPolicy>(StringComparer.Ordinal);
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
policies.Clear();
|
||||
}
|
||||
|
||||
public void SetPolicy(
|
||||
string name,
|
||||
AuthorizationPolicy policy
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty() ||
|
||||
policy.IsNullOrDefault())
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
policies.AddOrUpdate(name, policy, (_, __) => policy);
|
||||
}
|
||||
|
||||
public bool TryGetPolicy(
|
||||
string name,
|
||||
out AuthorizationPolicy policy
|
||||
)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty())
|
||||
{
|
||||
//
|
||||
policy = null!;
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
var result = policies.TryGetValue(name, out policy!);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void RemovePolicy(string name)
|
||||
{
|
||||
//
|
||||
if (name.IsNullOrEmpty())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//
|
||||
if (policies.TryRemove(name, out _)) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user