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

        PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key).ShouldBe("aaa");
        PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull("aaabbbccc").ShouldBeNull();
    }
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 async Task Role_Deleted_Distributed_Event_Test()
        {
            var role = await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName("moderator"));

            var permissionGrantsInRole = await PermissionGrantRepository.GetListAsync("R", role.Name);

            var caches = permissionGrantsInRole.Select(x => new KeyValuePair <string, PermissionGrantCacheItem>(
                                                           PermissionGrantCacheItem.CalculateCacheKey(x.Name, x.ProviderName, x.ProviderKey),
                                                           new PermissionGrantCacheItem(true))).ToList();
            await Cache.SetManyAsync(caches);


            using (var uow = UowManager.Begin())
            {
                await RoleRepository.DeleteAsync(role);

                await uow.CompleteAsync();
            }

            var permissionGrantCaches = await Cache.GetManyAsync(caches.Select(x => x.Key));

            foreach (var cache in permissionGrantCaches)
            {
                cache.Value.ShouldBeNull();
            }
        }
Beispiel #4
0
        public virtual async Task UpdateAsync(string providerName, string providerKey, UpdatePermissionsDto input)
        {
            await CheckProviderPolicy(providerName);

            var permissions = await PermissionGrantRepository.GetListAsync(providerName, providerKey);

            foreach (var permission in input.Permissions)
            {
                var editPermission = permissions.FirstOrDefault(p => p.Name.Equals(permission.Name));
                if (editPermission == null)
                {
                    if (permission.IsGranted)
                    {
                        var permissionGrant = new PermissionGrant(GuidGenerator.Create(),
                                                                  permission.Name, providerName, providerKey, CurrentTenant.Id);
                        await PermissionGrantRepository.InsertAsync(permissionGrant);
                    }
                }
                else
                {
                    if (!permission.IsGranted)
                    {
                        await PermissionGrantRepository.DeleteAsync(editPermission.Id);
                    }
                }
                // 同步变更缓存里的权限配置
                var cacheKey  = CalculateCacheKey(permission.Name, providerName, providerKey);
                var cacheItem = new PermissionGrantCacheItem(permission.Name, permission.IsGranted);
                await Cache.SetAsync(cacheKey, cacheItem);
            }
        }
Beispiel #5
0
    public async Task PermissionStore_IsGrantedAsync_Should_Cache_PermissionGrant()
    {
        (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                          UserPermissionValueProvider.ProviderName,
                                                                          PermissionTestDataBuilder.User1Id.ToString()))).ShouldBeNull();


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


        (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                          UserPermissionValueProvider.ProviderName,
                                                                          PermissionTestDataBuilder.User1Id.ToString()))).ShouldNotBeNull();
    }
Beispiel #6
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 #7
0
    public async Task Cache_Should_Invalidator_WhenPermissionGrantChanged()
    {
        // IsGrantedAsync will cache PermissionGrant
        await _permissionStore.IsGrantedAsync("MyPermission1",
                                              UserPermissionValueProvider.ProviderName,
                                              PermissionTestDataBuilder.User1Id.ToString());

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

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

        (await _cache.GetAsync(PermissionGrantCacheItem.CalculateCacheKey("MyPermission1",
                                                                          UserPermissionValueProvider.ProviderName,
                                                                          PermissionTestDataBuilder.User1Id.ToString()))).ShouldBeNull();
    }
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 GetPermissionNameFormCacheKeyOrNull(string key)
 {
     //TODO: throw ex when name is null?
     return(PermissionGrantCacheItem.GetPermissionNameFormCacheKeyOrNull(key));
 }
Beispiel #10
0
 protected virtual string CalculateCacheKey(string name, string providerName, string providerKey)
 {
     return(PermissionGrantCacheItem.CalculateCacheKey(name, providerName, providerKey));
 }