You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

153 lines
9.2 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Caching;
using Volo.Abp.DependencyInjection;
using Volo.Abp.PermissionManagement;
using PermissionGrantCacheItem = WinIn.FasterZ.InterfaceDash.Permissions.PermissionGrantCacheItem;
namespace WinIn.FasterZ.AuthSiteCenter.Permissions
{
public class PermissionStoreAppService : ApplicationService, IPermissionStore, ITransientDependency
{
public ILogger<Volo.Abp.PermissionManagement.PermissionStore> Logger { get; set; }
protected IPermissionGrantRepository PermissionGrantRepository { get; }
protected IPermissionDefinitionManager PermissionDefinitionManager { get; }
protected IDistributedCache<PermissionGrantCacheItem> Cache { get; }
public PermissionStoreAppService(IPermissionGrantRepository permissionGrantRepository, IDistributedCache<PermissionGrantCacheItem> cache, IPermissionDefinitionManager permissionDefinitionManager)
{
PermissionGrantRepository = permissionGrantRepository;
Cache = cache;
PermissionDefinitionManager = permissionDefinitionManager;
Logger = NullLogger<Volo.Abp.PermissionManagement.PermissionStore>.Instance;
}
[HttpPost]
[Route("api/permissions/get-isGranted")]
public virtual async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey)
{
return (await GetCacheItemAsync(name, providerName, providerKey).ConfigureAwait(continueOnCapturedContext: false)).IsGranted;
}
protected virtual async Task<PermissionGrantCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
{
string cacheKey = CalculateCacheKey(name, providerName, providerKey);
Logger.LogDebug("PermissionStore.GetCacheItemAsync: " + cacheKey);
PermissionGrantCacheItem cacheItem2 = await Cache.GetAsync(cacheKey).ConfigureAwait(continueOnCapturedContext: false);
if (cacheItem2 != null)
{
Logger.LogDebug("Found in the cache: " + cacheKey);
return cacheItem2;
}
Logger.LogDebug("Not found in the cache: " + cacheKey);
cacheItem2 = new PermissionGrantCacheItem(isGranted: false);
await SetCacheItemsAsync(providerName, providerKey, name, cacheItem2).ConfigureAwait(continueOnCapturedContext: false);
return cacheItem2;
}
protected virtual async Task SetCacheItemsAsync(string providerName, string providerKey, string currentName, PermissionGrantCacheItem currentCacheItem)
{
IReadOnlyList<PermissionDefinition> permissions = PermissionDefinitionManager.GetPermissionsAsync().Result.ToList();
Logger.LogDebug("Getting all granted permissions from the repository for this provider name,key: " + providerName + "," + providerKey);
HashSet<string> hashSet = new HashSet<string>((await PermissionGrantRepository.GetListAsync(providerName, providerKey).ConfigureAwait(continueOnCapturedContext: false)).Select((PermissionGrant p) => p.Name));
Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}");
List<KeyValuePair<string, PermissionGrantCacheItem>> list = new List<KeyValuePair<string, PermissionGrantCacheItem>>();
foreach (PermissionDefinition item in permissions)
{
bool isGranted = hashSet.Contains(item.Name);
list.Add(new KeyValuePair<string, PermissionGrantCacheItem>(CalculateCacheKey(item.Name, providerName, providerKey), new PermissionGrantCacheItem(isGranted)));
if (item.Name == currentName)
{
currentCacheItem.IsGranted = isGranted;
}
}
await Cache.SetManyAsync(list).ConfigureAwait(continueOnCapturedContext: false);
Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}");
}
public virtual async Task<MultiplePermissionGrantResult> IsGrantedAsync(string[] names, string providerName, string providerKey)
{
Check.NotNullOrEmpty(names, "names");
MultiplePermissionGrantResult result = new MultiplePermissionGrantResult();
if (names.Length == 1)
{
string text = names.First();
Dictionary<string, PermissionGrantResult> result2 = result.Result;
string key = text;
result2.Add(key, (await IsGrantedAsync(names.First(), providerName, providerKey).ConfigureAwait(continueOnCapturedContext: false)) ? PermissionGrantResult.Granted : PermissionGrantResult.Undefined);
return result;
}
foreach (KeyValuePair<string, PermissionGrantCacheItem> item in await GetCacheItemsAsync(names, providerName, providerKey).ConfigureAwait(continueOnCapturedContext: false))
{
result.Result.Add(GetPermissionNameFormCacheKeyOrNull(item.Key), (item.Value != null && item.Value.IsGranted) ? PermissionGrantResult.Granted : PermissionGrantResult.Undefined);
}
return result;
}
protected virtual async Task<List<KeyValuePair<string, PermissionGrantCacheItem>>> GetCacheItemsAsync(string[] names, string providerName, string providerKey)
{
List<string> cacheKeys = names.Select((string x) => CalculateCacheKey(x, providerName, providerKey)).ToList();
Logger.LogDebug("PermissionStore.GetCacheItemAsync: " + string.Join(",", cacheKeys));
List<KeyValuePair<string, PermissionGrantCacheItem>> cacheItems = (await Cache.GetManyAsync(cacheKeys).ConfigureAwait(continueOnCapturedContext: false)).ToList();
if (cacheItems.All((KeyValuePair<string, PermissionGrantCacheItem> x) => x.Value != null))
{
Logger.LogDebug("Found in the cache: " + string.Join(",", cacheKeys));
return cacheItems;
}
List<string> list = (from x in cacheItems
where x.Value == null
select x.Key).ToList();
Logger.LogDebug("Not found in the cache: " + string.Join(",", list));
List<KeyValuePair<string, PermissionGrantCacheItem>> source = await SetCacheItemsAsync(providerName, providerKey, list).ConfigureAwait(continueOnCapturedContext: false);
List<KeyValuePair<string, PermissionGrantCacheItem>> list2 = new List<KeyValuePair<string, PermissionGrantCacheItem>>();
foreach (string key in cacheKeys)
{
KeyValuePair<string, PermissionGrantCacheItem> keyValuePair = source.FirstOrDefault((KeyValuePair<string, PermissionGrantCacheItem> x) => x.Key == key);
if (keyValuePair.Value == null)
{
keyValuePair = cacheItems.FirstOrDefault((KeyValuePair<string, PermissionGrantCacheItem> x) => x.Key == key);
}
list2.Add(new KeyValuePair<string, PermissionGrantCacheItem>(key, keyValuePair.Value));
}
return list2;
}
protected virtual async Task<List<KeyValuePair<string, PermissionGrantCacheItem>>> SetCacheItemsAsync(string providerName, string providerKey, List<string> notCacheKeys)
{
List<PermissionDefinition> permissions = PermissionDefinitionManager.GetPermissionsAsync().Result.ToList();
Logger.LogDebug("Getting not cache granted permissions from the repository for this provider name,key: " + providerName + "," + providerKey);
HashSet<string> hashSet = new HashSet<string>((await PermissionGrantRepository.GetListAsync(notCacheKeys.Select(new Func<string, string>(GetPermissionNameFormCacheKeyOrNull)).ToArray(), providerName, providerKey).ConfigureAwait(continueOnCapturedContext: false)).Select((PermissionGrant p) => p.Name));
Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}");
List<KeyValuePair<string, PermissionGrantCacheItem>> cacheItems = new List<KeyValuePair<string, PermissionGrantCacheItem>>();
foreach (PermissionDefinition item in permissions)
{
bool isGranted = hashSet.Contains(item.Name);
cacheItems.Add(new KeyValuePair<string, PermissionGrantCacheItem>(CalculateCacheKey(item.Name, providerName, providerKey), new PermissionGrantCacheItem(isGranted)));
}
await Cache.SetManyAsync(cacheItems).ConfigureAwait(continueOnCapturedContext: false);
Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}");
return cacheItems;
}
protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
{
return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey);
}
protected virtual string GetPermissionNameFormCacheKeyOrNull(string key)
{
return PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key);
}
}
}