public async override Task <MultiplePermissionGrantResult> CheckAsync(PermissionValuesCheckContext context)
        {
            var permissionNames = context.Permissions.Select(x => x.Name).ToList();
            var result          = new MultiplePermissionGrantResult(permissionNames.ToArray());

            var roles = context.Principal?.FindAll(AbpClaimTypes.Role).Select(c => c.Value).ToArray();

            if (roles == 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);
        }
Beispiel #2
0
        public async Task <MultiplePermissionGrantResult> IsGrantedAsync(ClaimsPrincipal claimsPrincipal, string[] names)
        {
            Check.NotNull(names, nameof(names));

            var multiTenancySide = claimsPrincipal?.GetMultiTenancySide() ?? CurrentTenant.GetMultiTenancySide();

            var result = new MultiplePermissionGrantResult();

            if (!names.Any())
            {
                return(result);
            }

            var permissionDefinitions = new List <PermissionDefinition>();

            foreach (var name in names)
            {
                var permission = PermissionDefinitionManager.Get(name);

                result.Result.Add(name, PermissionGrantResult.Undefined);

                if (permission.IsEnabled &&
                    await PermissionStateManager.IsEnabledAsync(permission) &&
                    permission.MultiTenancySide.HasFlag(multiTenancySide))
                {
                    permissionDefinitions.Add(permission);
                }
            }

            foreach (var provider in PermissionValueProviderManager.ValueProviders)
            {
                var context = new PermissionValuesCheckContext(
                    permissionDefinitions.Where(x => !x.Providers.Any() || x.Providers.Contains(provider.Name)).ToList(),
                    claimsPrincipal);

                var multipleResult = await provider.CheckAsync(context);

                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;
                    permissionDefinitions.RemoveAll(x => x.Name == grantResult.Key);
                }

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

            return(result);
        }