示例#1
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 StateCheckerManager.IsEnabledAsync(permission) &&
                    permission.MultiTenancySide.HasFlag(multiTenancySide))
                {
                    permissionDefinitions.Add(permission);
                }
            }

            foreach (var provider in PermissionValueProviderManager.ValueProviders)
            {
                var permissions = permissionDefinitions
                                  .Where(x => !x.Providers.Any() || x.Providers.Contains(provider.Name))
                                  .ToList();

                if (permissions.IsNullOrEmpty())
                {
                    break;
                }

                var context = new PermissionValuesCheckContext(
                    permissions,
                    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);
        }