|
|
|
|
@ -53,9 +53,9 @@ namespace Volo.Abp.PermissionManagement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"Not found in the cache: {cacheKey}");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cacheItem = new PermissionGrantCacheItem(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await SetCacheItemsAsync(providerName, providerKey, name, cacheItem);
|
|
|
|
|
|
|
|
|
|
return cacheItem;
|
|
|
|
|
@ -68,7 +68,7 @@ namespace Volo.Abp.PermissionManagement
|
|
|
|
|
PermissionGrantCacheItem currentCacheItem)
|
|
|
|
|
{
|
|
|
|
|
var permissions = PermissionDefinitionManager.GetPermissions();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"Getting all granted permissions from the repository for this provider name,key: {providerName},{providerKey}");
|
|
|
|
|
|
|
|
|
|
var grantedPermissionsHashSet = new HashSet<string>(
|
|
|
|
|
@ -87,7 +87,7 @@ namespace Volo.Abp.PermissionManagement
|
|
|
|
|
CalculateCacheKey(permission.Name, providerName, providerKey),
|
|
|
|
|
new PermissionGrantCacheItem(isGranted))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (permission.Name == currentName)
|
|
|
|
|
{
|
|
|
|
|
currentCacheItem.IsGranted = isGranted;
|
|
|
|
|
@ -95,13 +95,118 @@ namespace Volo.Abp.PermissionManagement
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Cache.SetManyAsync(cacheItems);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, nameof(names));
|
|
|
|
|
|
|
|
|
|
var result = new MultiplePermissionGrantResult();
|
|
|
|
|
|
|
|
|
|
if (names.Length == 1)
|
|
|
|
|
{
|
|
|
|
|
var name = names.First();
|
|
|
|
|
result.Result.Add(name,
|
|
|
|
|
await IsGrantedAsync(names.First(), providerName, providerKey)
|
|
|
|
|
? PermissionGrantResult.Granted
|
|
|
|
|
: PermissionGrantResult.Undefined);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cacheItems = await GetCacheItemsAsync(names, providerName, providerKey);
|
|
|
|
|
foreach (var item in cacheItems)
|
|
|
|
|
{
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
var cacheKeys = names.Select(x => CalculateCacheKey(x, providerName, providerKey)).ToList();
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"PermissionStore.GetCacheItemAsync: {string.Join(",", cacheKeys)}");
|
|
|
|
|
|
|
|
|
|
var cacheItems = (await Cache.GetManyAsync(cacheKeys)).ToList();
|
|
|
|
|
if (cacheItems.All(x => x.Value != null))
|
|
|
|
|
{
|
|
|
|
|
Logger.LogDebug($"Found in the cache: {string.Join(",", cacheKeys)}");
|
|
|
|
|
return cacheItems;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var notCacheKeys = cacheItems.Where(x => x.Value == null).Select(x => x.Key).ToList();
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"Not found in the cache: {string.Join(",", notCacheKeys)}");
|
|
|
|
|
|
|
|
|
|
var newCacheItems = await SetCacheItemsAsync(providerName, providerKey, notCacheKeys);
|
|
|
|
|
|
|
|
|
|
var result = new List<KeyValuePair<string, PermissionGrantCacheItem>>();
|
|
|
|
|
foreach (var key in cacheKeys)
|
|
|
|
|
{
|
|
|
|
|
var item = newCacheItems.FirstOrDefault(x => x.Key == key);
|
|
|
|
|
if (item.Value == null)
|
|
|
|
|
{
|
|
|
|
|
item = cacheItems.FirstOrDefault(x => x.Key == key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result.Add(new KeyValuePair<string, PermissionGrantCacheItem>(key, item.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual async Task<List<KeyValuePair<string, PermissionGrantCacheItem>>> SetCacheItemsAsync(
|
|
|
|
|
string providerName,
|
|
|
|
|
string providerKey,
|
|
|
|
|
List<string> notCacheKeys)
|
|
|
|
|
{
|
|
|
|
|
var permissions = PermissionDefinitionManager.GetPermissions().Where(x => notCacheKeys.Any(k => GetPermissionNameFormCacheKeyOrNull(k) == x.Name)).ToList();
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"Getting not cache granted permissions from the repository for this provider name,key: {providerName},{providerKey}");
|
|
|
|
|
|
|
|
|
|
var grantedPermissionsHashSet = new HashSet<string>(
|
|
|
|
|
(await PermissionGrantRepository.GetListAsync(notCacheKeys.Select(GetPermissionNameFormCacheKeyOrNull).ToArray(), providerName, providerKey)).Select(p => p.Name)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}");
|
|
|
|
|
|
|
|
|
|
var cacheItems = new List<KeyValuePair<string, PermissionGrantCacheItem>>();
|
|
|
|
|
|
|
|
|
|
foreach (var permission in permissions)
|
|
|
|
|
{
|
|
|
|
|
var isGranted = grantedPermissionsHashSet.Contains(permission.Name);
|
|
|
|
|
|
|
|
|
|
cacheItems.Add(new KeyValuePair<string, PermissionGrantCacheItem>(
|
|
|
|
|
CalculateCacheKey(permission.Name, providerName, providerKey),
|
|
|
|
|
new PermissionGrantCacheItem(isGranted))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Cache.SetManyAsync(cacheItems);
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
//TODO: throw ex when name is null?
|
|
|
|
|
return PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|