Beispiel #1
0
        protected virtual async Task <PermissionGrantCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
        {
            var cacheKey = CalculateCacheKey(name, providerName, providerKey);

            Logger.LogDebug($"PermissionStore.GetCacheItemAsync: {cacheKey}");

            var cacheItem = await Cache.GetAsync(cacheKey);

            if (cacheItem != null)
            {
                Logger.LogDebug($"Found in the cache: {cacheKey}");
                return(cacheItem);
            }

            Logger.LogDebug($"Not found in the cache, getting from the repository: {cacheKey}");

            cacheItem = new PermissionGrantCacheItem(
                name,
                (await PermissionGrantRepository.FindAsync(name, providerName, providerKey)) != null
                );

            Logger.LogDebug($"Setting the cache item: {cacheKey}");

            await Cache.SetAsync(
                cacheKey,
                cacheItem
                );

            Logger.LogDebug($"Finished setting the cache item: {cacheKey}");

            return(cacheItem);
        }
Beispiel #2
0
        protected virtual async Task <PermissionGrantCacheItem> GetCacheItemAsync(
            string name,
            string providerName,
            string providerKey)
        {
            var cacheKey = CalculateCacheKey(name, providerName, providerKey);

            Logger.LogDebug($"PermissionStore.GetCacheItemAsync: {cacheKey}");

            var cacheItem = await Cache.GetAsync(cacheKey);

            if (cacheItem != null)
            {
                Logger.LogDebug($"Found in the cache: {cacheKey}");
                return(cacheItem);
            }

            Logger.LogDebug($"Not found in the cache: {cacheKey}");

            cacheItem = new PermissionGrantCacheItem(false);

            await SetCacheItemsAsync(providerName, providerKey, name, cacheItem);

            return(cacheItem);
        }
Beispiel #3
0
        public void GetPermissionNameFormCacheKeyOrNull()
        {
            var key = PermissionGrantCacheItem.CalculateCacheKey("aaa", "bbb", "ccc");

            PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key).ShouldBe("aaa");
            PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull("aaabbbccc").ShouldBeNull();
        }
Beispiel #4
0
        public async Task PermissionStore_IsGrantedAsync_Should_Cache_PermissionGrant()
        {
            (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                              UserPermissionValueProvider.ProviderName,
                                                                              PermissionTestDataBuilder.User1Id.ToString())).ConfigureAwait(false)).ShouldBeNull();


            await _permissionStore.IsGrantedAsync("MyPermission1",
                                                  UserPermissionValueProvider.ProviderName,
                                                  PermissionTestDataBuilder.User1Id.ToString()).ConfigureAwait(false);


            (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                              UserPermissionValueProvider.ProviderName,
                                                                              PermissionTestDataBuilder.User1Id.ToString())).ConfigureAwait(false)).ShouldNotBeNull();
        }
Beispiel #5
0
        public virtual async Task <PermissionGrant> UpdateProviderKeyAsync(PermissionGrant permissionGrant, string providerKey)
        {
            using (CurrentTenant.Change(permissionGrant.TenantId))
            {
                //Invalidating the cache for the old key
                await Cache.RemoveAsync(
                    PermissionGrantCacheItem.CalculateCacheKey(
                        permissionGrant.Name,
                        permissionGrant.ProviderName,
                        permissionGrant.ProviderKey
                        )
                    );
            }

            permissionGrant.ProviderKey = providerKey;
            return(await PermissionGrantRepository.UpdateAsync(permissionGrant));
        }
Beispiel #6
0
        public async Task Cache_Should_Invalidator_WhenPermissionGrantChanged()
        {
            // IsGrantedAsync will cache PermissionGrant
            await _permissionStore.IsGrantedAsync("MyPermission1",
                                                  UserPermissionValueProvider.ProviderName,
                                                  PermissionTestDataBuilder.User1Id.ToString()).ConfigureAwait(false);

            var permissionGrant = await _permissionGrantRepository.FindAsync("MyPermission1",
                                                                             UserPermissionValueProvider.ProviderName,
                                                                             PermissionTestDataBuilder.User1Id.ToString()).ConfigureAwait(false);

            permissionGrant.ShouldNotBeNull();
            await _permissionGrantRepository.DeleteAsync(permissionGrant).ConfigureAwait(false);

            (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                              UserPermissionValueProvider.ProviderName,
                                                                              PermissionTestDataBuilder.User1Id.ToString())).ConfigureAwait(false)).ShouldBeNull();
        }
Beispiel #7
0
        protected virtual async Task <PermissionGrantCacheItem> GetCacheItemAsync(string name, string providerName, string providerKey)
        {
            var cacheKey  = CalculateCacheKey(name, providerName, providerKey);
            var cacheItem = await Cache.GetAsync(cacheKey);

            if (cacheItem != null)
            {
                return(cacheItem);
            }

            cacheItem = new PermissionGrantCacheItem(
                name,
                await PermissionGrantRepository.FindAsync(name, providerName, providerKey) != null
                );

            await Cache.SetAsync(
                cacheKey,
                cacheItem
                );

            return(cacheItem);
        }
Beispiel #8
0
        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}");

            var grantedPermissionsHashSet = new HashSet <string>(
                (await PermissionGrantRepository.GetListAsync(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))
                               );

                if (permission.Name == currentName)
                {
                    currentCacheItem.IsGranted = isGranted;
                }
            }

            await Cache.SetManyAsync(cacheItems);

            Logger.LogDebug($"Finished setting the cache items. Count: {permissions.Count}");
        }
Beispiel #9
0
 protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
 {
     return(PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey));
 }
Beispiel #10
0
 protected virtual string GetPermissionNameFormCacheKeyOrNull(string key)
 {
     //TODO: throw ex when name is null?
     return(PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key));
 }