public async Task <MultiplePermissionGrantResult> CheckAsync(ClaimsPrincipal principal, List <PermissionDefinition> permissions)
        {
            var permissionNames = permissions.Select(x => x.Name).ToList();
            var result          = new MultiplePermissionGrantResult(permissionNames.ToArray());

            var roles = principal?.FindAll(ClaimTypes.Role).Select(c => c.Value).ToArray();

            if (roles is null || !roles.Any())
            {
                return(result);
            }

            foreach (var role in roles)
            {
                var multipleResult = await _permissionStore.IsGrantedAsync(permissionNames.ToArray(), Name !, role);

                foreach (var grantResult in multipleResult.Result.Where(grantResult => result.Result.ContainsKey(grantResult.Key) && result.Result[grantResult.Key] == PermissionGrantResult.Undefined && grantResult.Value != PermissionGrantResult.Undefined))
                {
                    result.Result[grantResult.Key] = grantResult.Value;
                    permissionNames.RemoveAll(x => x == grantResult.Key);
                }

                if (result.AllGranted || result.AllProhibited)
                {
                    break;
                }
            }

            return(result);
        }
예제 #2
0
        public async Task <MultiplePermissionGrantResult> IsGrantedAsync([NotNull] string[] names, [MaybeNull] string providerName, [MaybeNull] string providerKey)
        {
            if (names is null)
            {
                throw new ArgumentNullException(nameof(names));
            }

            MultiplePermissionGrantResult 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(Key, IsGranted) in cacheItems)
            {
                result.Result.Add(GetPermissionInfoFormCacheKey(Key).Name, IsGranted ? PermissionGrantResult.Granted : PermissionGrantResult.Undefined);
            }

            return(result);
        }