diff --git a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs index 2ffd6f80e1..bd8fe7abc2 100644 --- a/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs +++ b/modules/permission-management/src/Volo.Abp.PermissionManagement.Domain/Volo/Abp/PermissionManagement/PermissionStore.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System.Linq; +using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.Authorization.Permissions; @@ -10,17 +11,21 @@ namespace Volo.Abp.PermissionManagement public class PermissionStore : IPermissionStore, ITransientDependency { public ILogger Logger { get; set; } - + protected IPermissionGrantRepository PermissionGrantRepository { get; } + protected IPermissionDefinitionManager PermissionDefinitionManager { get; } + protected IDistributedCache Cache { get; } - + public PermissionStore( IPermissionGrantRepository permissionGrantRepository, - IDistributedCache cache) + IDistributedCache cache, + IPermissionDefinitionManager permissionDefinitionManager) { PermissionGrantRepository = permissionGrantRepository; Cache = cache; + PermissionDefinitionManager = permissionDefinitionManager; Logger = NullLogger.Instance; } @@ -29,7 +34,10 @@ namespace Volo.Abp.PermissionManagement return (await GetCacheItemAsync(name, providerName, providerKey)).IsGranted; } - protected virtual async Task GetCacheItemAsync(string name, string providerName, string providerKey) + protected virtual async Task GetCacheItemAsync( + string name, + string providerName, + string providerKey) { var cacheKey = CalculateCacheKey(name, providerName, providerKey); @@ -43,23 +51,45 @@ namespace Volo.Abp.PermissionManagement return cacheItem; } - Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}"); + Logger.LogDebug($"Not found in the cache: {cacheKey}"); + + cacheItem = new PermissionGrantCacheItem(name, false); + + await SetCacheItemsAsync(providerName, providerKey, name, cacheItem); - cacheItem = new PermissionGrantCacheItem( - name, - (await PermissionGrantRepository.FindAsync(name, providerName, providerKey)) != null - ); + return cacheItem; + } - Logger.LogDebug($"Setting the cache item: {cacheKey}"); + protected virtual async Task SetCacheItemsAsync( + string providerName, + string providerKey, + string currentName, + PermissionGrantCacheItem currentCacheItem) + { + var permissions = PermissionDefinitionManager.GetPermissions(); + + Logger.LogDebug($"Getting all granted permissions from the repository for this provider name,key: {providerName},{providerKey}"); - await Cache.SetAsync( - cacheKey, - cacheItem - ); + var permissionGrants = await PermissionGrantRepository.GetListAsync(providerName, providerKey); - Logger.LogDebug($"Finished setting the cache item: {cacheKey}"); + Logger.LogDebug($"Setting the cache items. Count: {permissions.Count}"); - return cacheItem; + foreach (var permission in permissions) + { + var isGranted = permissionGrants.Any(pg => pg.Name == permission.Name); //TODO: Optimize? Dictionary/Hash + + await Cache.SetAsync( + CalculateCacheKey(permission.Name, providerName, providerKey), + new PermissionGrantCacheItem(permission.Name, isGranted) + ); + + if (permission.Name == currentName) + { + currentCacheItem.IsGranted = isGranted; + } + } + + Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}"); } protected virtual string CalculateCacheKey(string name, string providerName, string providerKey) @@ -67,4 +97,4 @@ namespace Volo.Abp.PermissionManagement return PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey); } } -} +} \ No newline at end of file