Пример #1
0
        internal static IPermission[] SearchPermissions(this IRol me, bool forFilter, IFunction function, TypeDiscriminator typeDiscriminator, params IDiscriminator[] discriminators)
        {
            using (var res = Printer.CallResult <IPermission[]>())
            {
                discriminators = discriminators.RemoveNulls();
                using (Printer.Indent2("Inpupt Prameters"))
                {
                    Printer.WriteLine($"Rol: {me?.Name}");
                    Printer.WriteLine($"For filter: " + forFilter);
                    Printer.WriteLine($"Function: {function?.ToString() ?? "<null>"}");
                    Printer.WriteLine($"Type discriminator: {typeDiscriminator?.ToString() ?? "<null>"}");
                    Printer.Foreach($"Discriminators:", discriminators, dis => Printer.WriteLine($"{dis}"));
                }
                // Function validation
                if (!(function?.IsValid() ?? true))
                {
                    throw new ArgumentException($"The '{nameof(function)}' pararameter with value '{function}' has an invalid state", nameof(function));
                }

                // TypeDiscriminator validation
                if (!typeDiscriminator?.IsValid() ?? false)
                {
                    throw new InvalidStateException($"The '{typeDiscriminator}' discriminator has an invalid state");
                }

                // Discriminators validation
                var invalidDiscriminator = discriminators?.FirstOrDefault(d => !d.IsValid());

                if (invalidDiscriminator != null)
                {
                    throw new InvalidStateException($"The '{invalidDiscriminator}' discriminator has an invalid state");
                }

                // Get & print rol permissions
                var permissions = me.AllPermissions();
                using (Printer.Indent2("Permissions:"))
                    permissions.Print(PrintMode.Table);
                using (Printer.Indent2("Iterate permissions:"))
                    res.Value = permissions.Where(p => p.Match(forFilter, function, typeDiscriminator, discriminators)).ToArray();
                res.OnPrintResult = r => r.Print(PrintMode.Table);
                return(res.Value);
            }
        }