Beispiel #1
0
        public virtual async Task <bool> IsGrantedAsync(ClaimsPrincipal claimsPrincipal, string name)
        {
            Check.NotNull(name, nameof(name));

            var isGranted = false;

            var permission = PermissionDefinitionManager.Get(name);
            var context    = new PermissionValueCheckContext(permission, claimsPrincipal);

            foreach (var provider in ValueProviders)
            {
                if (context.Permission.Providers.Any() &&
                    !context.Permission.Providers.Contains(provider.Name))
                {
                    continue;
                }

                var result = await provider.CheckAsync(context);

                if (result == PermissionGrantResult.Granted)
                {
                    isGranted = true;
                }
                else if (result == PermissionGrantResult.Prohibited)
                {
                    return(false);
                }
            }

            return(isGranted);
        }
Beispiel #2
0
        public virtual async Task <PermissionGrantInfo> CheckAsync(ClaimsPrincipal claimsPrincipal, string name)
        {
            Check.NotNull(name, nameof(name));

            var context = new PermissionValueCheckContext(
                PermissionDefinitionManager.Get(name),
                claimsPrincipal
                );

            foreach (var provider in ValueProviders)
            {
                if (context.Permission.Providers.Any() &&
                    !context.Permission.Providers.Contains(provider.Name))
                {
                    continue;
                }

                var result = await provider.CheckAsync(context);

                if (result.IsGranted)
                {
                    return(new PermissionGrantInfo(context.Permission.Name, true, provider.Name, result.ProviderKey));
                }
            }

            return(new PermissionGrantInfo(context.Permission.Name, false));
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public virtual async Task <bool> IsGrantedAsync(
            ClaimsPrincipal claimsPrincipal,
            string name)
        {
            Check.NotNull(name, nameof(name));

            var permission = PermissionDefinitionManager.Get(name);

            if (!permission.IsEnabled)
            {
                return(false);
            }

            if (!await StateCheckerManager.IsEnabledAsync(permission))
            {
                return(false);
            }

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

            if (!permission.MultiTenancySide.HasFlag(multiTenancySide))
            {
                return(false);
            }

            var isGranted = false;
            var context   = new PermissionValueCheckContext(permission, claimsPrincipal);

            foreach (var provider in PermissionValueProviderManager.ValueProviders)
            {
                if (context.Permission.Providers.Any() &&
                    !context.Permission.Providers.Contains(provider.Name))
                {
                    continue;
                }

                var result = await provider.CheckAsync(context);

                if (result == PermissionGrantResult.Granted)
                {
                    isGranted = true;
                }
                else if (result == PermissionGrantResult.Prohibited)
                {
                    return(false);
                }
            }

            return(isGranted);
        }