From e0a7135928c1430d94fddd113863ca88c210ddf7 Mon Sep 17 00:00:00 2001 From: sagilio Date: Fri, 4 Nov 2022 01:04:02 +0800 Subject: [PATCH] feat: Add list request values --- .../GenericTests/SupportCountTests.cs | 22 ++++++ .../EnforcerExtension.GenericEnforce.cs | 78 +++++++++++++++++++ Casbin/Model/Request.cs | 8 +- Casbin/Model/RequestValues.cs | 31 ++++++-- 4 files changed, 128 insertions(+), 11 deletions(-) diff --git a/Casbin.UnitTests/GenericTests/SupportCountTests.cs b/Casbin.UnitTests/GenericTests/SupportCountTests.cs index d28d0ed..bf5b16a 100644 --- a/Casbin.UnitTests/GenericTests/SupportCountTests.cs +++ b/Casbin.UnitTests/GenericTests/SupportCountTests.cs @@ -41,56 +41,78 @@ private static void TestEnforce(IEnforcer enforcer, EnforceContext context, int { case 1: Assert.True(enforcer.Enforce(context, "value1")); + Assert.True(enforcer.Enforce(context, new[] { "value1" })); break; case 2: Assert.True(enforcer.Enforce(context, "value1", "value2")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2" })); break; case 3: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3" })); break; case 4: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", "value4" })); break; case 5: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5" })); break; case 6: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6" })); break; case 7: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7" })); break; case 8: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8" })); break; case 9: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8", "value9" })); break; case 10: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8", "value9", "value10" })); break; case 11: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11" })); break; case 12: Assert.True(enforcer.Enforce(context, "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12")); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12" })); break; case 13: Assert.True(enforcer.Enforce(context, Request.CreateValues("value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13"))); + Assert.True(enforcer.Enforce(context, new[] { "value1", "value2", "value3", + "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13" })); break; default: throw new ArgumentOutOfRangeException(nameof(requestCount)); diff --git a/Casbin/Extensions/Enforcer/EnforcerExtension.GenericEnforce.cs b/Casbin/Extensions/Enforcer/EnforcerExtension.GenericEnforce.cs index 607eb4a..5eed596 100644 --- a/Casbin/Extensions/Enforcer/EnforcerExtension.GenericEnforce.cs +++ b/Casbin/Extensions/Enforcer/EnforcerExtension.GenericEnforce.cs @@ -6,6 +6,84 @@ namespace Casbin; public static partial class EnforcerExtension { + #region Generic List Enforce + + public static bool Enforce(this IEnforcer enforcer, params T[] value) + { + var request = Request.CreateValues(value); + return enforcer.Enforce(enforcer.CreateContext(), request); + } + + public static Task EnforceAsync(this IEnforcer enforcer, params T[] value) + { + var request = Request.CreateValues(value); + return enforcer.EnforceAsync(enforcer.CreateContext(), request); + } + + public static bool Enforce(this IEnforcer enforcer, EnforceContext context, params T[] value) + { + var request = Request.CreateValues(value); + return enforcer.Enforce(context, request); + } + + public static Task EnforceAsync(this IEnforcer enforcer, EnforceContext context, params T[] value) + { + var request = Request.CreateValues(value); + return enforcer.EnforceAsync(context, request); + } + + public static (bool Result, IEnumerable> Explains) + EnforceEx(this IEnforcer enforcer, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContext(true); + bool result = enforcer.Enforce(context, request); + return (result, context.Explanations); + } + + public static async Task<(bool Result, IEnumerable> Explains)> + EnforceExAsync(this IEnforcer enforcer, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContext(true); + bool result = await enforcer.EnforceAsync(context, request); + return (result, context.Explanations); + } + + public static bool EnforceWithMatcher(this IEnforcer enforcer, string matcher, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContextWithMatcher(matcher); + return enforcer.Enforce(context, request); + } + + public static Task EnforceWithMatcherAsync(this IEnforcer enforcer, string matcher, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContextWithMatcher(matcher); + return enforcer.EnforceAsync(context, request); + } + + public static (bool Result, IEnumerable> Explains) + EnforceExWithMatcher(this IEnforcer enforcer, string matcher, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContextWithMatcher(matcher, true); + bool result = enforcer.Enforce(context, request); + return (result, context.Explanations); + } + + public static async Task<(bool Result, IEnumerable> Explains)> + EnforceExWithMatcherAsync(this IEnforcer enforcer, string matcher, params T[] value) + { + var request = Request.CreateValues(value); + EnforceContext context = enforcer.CreateContextWithMatcher(matcher, true); + bool result = await enforcer.EnforceAsync(context, request); + return (result, context.Explanations); + } + + #endregion + #region Generic Enforce Count 1 public static bool Enforce(this IEnforcer enforcer, T value) diff --git a/Casbin/Model/Request.cs b/Casbin/Model/Request.cs index 92583a3..a648d76 100644 --- a/Casbin/Model/Request.cs +++ b/Casbin/Model/Request.cs @@ -9,7 +9,6 @@ public static bool SupportGeneric(int count) return count is >= 1 and <= 12; } - public static RequestValues CreateValues(T value) { return new RequestValues(value); @@ -101,9 +100,10 @@ public static RequestValues CreateValues new(values); - - public static ObjectListRequestValues CreateValues(IReadOnlyList values) => new(values); + public static ListRequestValues CreateValues(params T[] value) + { + return new ListRequestValues(value); + } public static bool TryGetStringKey(TRequest requestValues, out string key) where TRequest : IRequestValues { diff --git a/Casbin/Model/RequestValues.cs b/Casbin/Model/RequestValues.cs index 3305240..2b4ed8f 100644 --- a/Casbin/Model/RequestValues.cs +++ b/Casbin/Model/RequestValues.cs @@ -838,7 +838,6 @@ public bool TrySetValue(int index, T value) { return TrySetValue(index, v); } - return false; } @@ -888,12 +887,25 @@ public bool TrySetValue(int index, string value) } } -public readonly struct ObjectListRequestValues : IRequestValues +public readonly struct ListRequestValues : IRequestValues { - private readonly IList _values; - - public ObjectListRequestValues(IReadOnlyList values) => - _values = values as IList ?? values.ToList(); + private readonly IList _values; + + public ListRequestValues(IEnumerable values) => + _values = values as IList ?? values.ToArray(); + + public TValue Value1 => _values[0]; + public TValue Value2 => _values[1]; + public TValue Value3 => _values[2]; + public TValue Value4 => _values[3]; + public TValue Value5 => _values[4]; + public TValue Value6 => _values[5]; + public TValue Value7 => _values[6]; + public TValue Value8 => _values[7]; + public TValue Value9 => _values[8]; + public TValue Value10 => _values[9]; + public TValue Value11 => _values[10]; + public TValue Value12 => _values[11]; public string this[int index] => RequestValues.ToStringValue(_values[index]); public int Count => _values.Count; @@ -905,7 +917,12 @@ public bool TrySetValue(int index, T value) return false; } - _values[index] = value; + if (value is not TValue v) + { + return false; + } + + _values[index] = v; return true; } }