From 285003bc1d517cba7c92a38ca696ba08c7bc4c93 Mon Sep 17 00:00:00 2001 From: sagilio Date: Wed, 26 Jul 2023 03:22:39 +0800 Subject: [PATCH] feat: Move functions from Casbin.Util to Casbin.Functions Signed-off-by: sagilio --- Casbin.Benchmark/BuildInFunctionsBenchmark.cs | 2 +- .../DefaultRoleManagerBenchmark.cs | 2 +- .../EntropyPool/DefaultRandomEntropyPool.cs | 143 ++++++++++++------ .../DefaultGetAccessTransaction.cs | 22 ++- Casbin.UnitTests/RbacTests/RoleManagerTest.cs | 2 +- .../UtilTests/BuiltInFunctionTest.cs | 105 +++++-------- Casbin/Abstractions/Persist/ISingleAdapter.cs | 3 +- Casbin/Caching/GFunctionCache.cs | 14 +- Casbin/Casbin.csproj | 1 + Casbin/EnforceView.cs | 1 - Casbin/Evaluation/ExpressionHandler.cs | 1 + Casbin/Extensions/Model/ModelExtension.cs | 2 +- .../{Util => Functions}/BuiltInFunctions.cs | 25 ++- Casbin/{Model => Functions}/FunctionMap.cs | 8 +- Casbin/Model/DefaultModel.cs | 2 +- Casbin/Model/Request.cs | 4 +- 16 files changed, 179 insertions(+), 158 deletions(-) rename Casbin/{Util => Functions}/BuiltInFunctions.cs (97%) rename Casbin/{Model => Functions}/FunctionMap.cs (86%) diff --git a/Casbin.Benchmark/BuildInFunctionsBenchmark.cs b/Casbin.Benchmark/BuildInFunctionsBenchmark.cs index e327f19..88dcb00 100644 --- a/Casbin.Benchmark/BuildInFunctionsBenchmark.cs +++ b/Casbin.Benchmark/BuildInFunctionsBenchmark.cs @@ -2,7 +2,7 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Engines; using BenchmarkDotNet.Jobs; -using Casbin.Util; +using Casbin.Functions; namespace Casbin.Benchmark { diff --git a/Casbin.Benchmark/DefaultRoleManagerBenchmark.cs b/Casbin.Benchmark/DefaultRoleManagerBenchmark.cs index de341a9..019d0bf 100644 --- a/Casbin.Benchmark/DefaultRoleManagerBenchmark.cs +++ b/Casbin.Benchmark/DefaultRoleManagerBenchmark.cs @@ -4,9 +4,9 @@ using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Engines; using BenchmarkDotNet.Jobs; +using Casbin.Functions; using Casbin.Model; using Casbin.Rbac; -using Casbin.Util; using static Casbin.Benchmark.TestHelper; namespace Casbin.Benchmark diff --git a/Casbin.UnitTests/ParallelTestHelper/EntropyPool/DefaultRandomEntropyPool.cs b/Casbin.UnitTests/ParallelTestHelper/EntropyPool/DefaultRandomEntropyPool.cs index 918bb21..99837c5 100644 --- a/Casbin.UnitTests/ParallelTestHelper/EntropyPool/DefaultRandomEntropyPool.cs +++ b/Casbin.UnitTests/ParallelTestHelper/EntropyPool/DefaultRandomEntropyPool.cs @@ -1,35 +1,40 @@ using System; using System.Collections.Generic; using Casbin.Model; -using Casbin.UnitTests.Extensions; namespace Casbin.UnitTests.ParallelTest { public class DefaultRandomEntropyPool : IEntropyPool where TRequest : IRequestValues { - private readonly string _randomStringSource = "abcdefghijklmnopqrstuvwxyz0123456789"; private readonly int _defaultRandomStringLength = 3; + private readonly string _randomStringSource = "abcdefghijklmnopqrstuvwxyz0123456789"; + private Func, TRequest> _convertFunc; + /// /// A dictionary to save the random element pools for each category. /// public Dictionary> _elementPools; - private Func, TRequest> _convertFunc; + public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc) { _convertFunc = list2RequestFunc; _elementPools = new Dictionary>(); } - public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc, Dictionary> elementPools) + + public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc, + Dictionary> elementPools) { _convertFunc = list2RequestFunc; _elementPools = elementPools; } + /// - /// + /// /// /// The first element is the category, the second is the count /// of elements you want to put in the element pool. - public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc, params KeyValuePair[] categoryAndCount) + public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc, + params KeyValuePair[] categoryAndCount) { _convertFunc = list2RequestFunc; _elementPools = new Dictionary>(); @@ -40,22 +45,48 @@ public DefaultRandomEntropyPool(Func, TRequest> list2RequestFunc, p { values.Add(GetRandomString(_defaultRandomStringLength)); } + _elementPools.Add(categoryAndCount[i].Key, values); } } + + public TRequest Get() + { + List res = new List(); + foreach (var keyValue in _elementPools) + { + Random rd = new Random(); + res.Add(keyValue.Value[rd.Next(0, keyValue.Value.Count)]); + } + + return _convertFunc(res); + } + + public void Add(TRequest request) + { + var enumerator = _elementPools.GetEnumerator(); + for (int i = 0; i < request.Count; i++) + { + enumerator.MoveNext(); + enumerator.Current.Value.Add(request[i]); + } + } + public DefaultRandomEntropyPool Add(string key, int count) { List values = new List(); - while(count-- > 0) + while (count-- > 0) { values.Add(GetRandomString(_defaultRandomStringLength)); } + _elementPools.Add(key, values); return this; } + public DefaultRandomEntropyPool Add(string key, string value) { - if(_elementPools.ContainsKey(key)) + if (_elementPools.ContainsKey(key)) { _elementPools[key].Add(value); } @@ -63,117 +94,129 @@ public DefaultRandomEntropyPool Add(string key, string value) { _elementPools.Add(key, new List() { value }); } + return this; } - public TRequest Get() - { - List res = new List(); - foreach(var keyValue in _elementPools) - { - Random rd = new Random(); - res.Add(keyValue.Value[rd.Next(0, keyValue.Value.Count)]); - } - return _convertFunc(res); - } - public void Add(TRequest request) - { - var enumerator = _elementPools.GetEnumerator(); - for (int i = 0; i < request.Count; i++) - { - enumerator.MoveNext(); - enumerator.Current.Value.Add(request[i]); - } - } + private string GetRandomString(int length) { string res = ""; - while(length-- > 0) + while (length-- > 0) { Random rd = new Random(); res += _randomStringSource[rd.Next(0, _randomStringSource.Length)]; } + return res; } } public static class DefaultRandomEntropyPool { - public static DefaultRandomEntropyPool> Create(params KeyValuePair[] categoryAndCount) + public static DefaultRandomEntropyPool> Create( + params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>(x => Request.CreateValues(x[0]), categoryAndCount); + return new DefaultRandomEntropyPool>(x => Request.CreateValues(x[0]), + categoryAndCount); } - public static DefaultRandomEntropyPool> Create2(params KeyValuePair[] categoryAndCount) + + public static DefaultRandomEntropyPool> Create2( + params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>(x => Request.CreateValues(x[0], x[1]), categoryAndCount); + return new DefaultRandomEntropyPool>(x => Request.CreateValues(x[0], x[1]), + categoryAndCount); } - public static DefaultRandomEntropyPool> Create3(params KeyValuePair[] categoryAndCount) + + public static DefaultRandomEntropyPool> Create3( + params KeyValuePair[] categoryAndCount) { return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create4(params KeyValuePair[] categoryAndCount) { return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2], x[3]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create5(params KeyValuePair[] categoryAndCount) { return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create6(params KeyValuePair[] categoryAndCount) { return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create7(params KeyValuePair[] categoryAndCount) { return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool< + RequestValues> Create8(params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>( + return new DefaultRandomEntropyPool< + RequestValues>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool< + RequestValues> Create9(params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>( + return new DefaultRandomEntropyPool< + RequestValues>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool< + RequestValues> Create10(params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>( + return new DefaultRandomEntropyPool>( x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9]), categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create11(params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>( - x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10]), categoryAndCount + return new DefaultRandomEntropyPool>( + x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10]), + categoryAndCount ); } - public static DefaultRandomEntropyPool> + + public static DefaultRandomEntropyPool> Create12(params KeyValuePair[] categoryAndCount) { - return new DefaultRandomEntropyPool>( - x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11]), categoryAndCount + return new DefaultRandomEntropyPool>( + x => Request.CreateValues(x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7], x[8], x[9], x[10], x[11]), + categoryAndCount ); } } -} \ No newline at end of file +} diff --git a/Casbin.UnitTests/ParallelTestHelper/Transaction/DefaultGetAccessTransaction.cs b/Casbin.UnitTests/ParallelTestHelper/Transaction/DefaultGetAccessTransaction.cs index f226dd2..737faf9 100644 --- a/Casbin.UnitTests/ParallelTestHelper/Transaction/DefaultGetAccessTransaction.cs +++ b/Casbin.UnitTests/ParallelTestHelper/Transaction/DefaultGetAccessTransaction.cs @@ -1,32 +1,40 @@ -using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Casbin.Model; -using Casbin.UnitTests.Extensions; namespace Casbin.UnitTests.ParallelTest { public class DefaultGetAccessTransaction : ITransaction where TRequest : IRequestValues { private List _requests = new List(); - public bool ExpectedResult { get; private set; } = false; - public bool ActualResult { get; private set; } = true; - public bool HasCompleted { get; private set; } = false; + public DefaultGetAccessTransaction(TRequest request) { _requests.Add(request); } + + public bool ExpectedResult { get; private set; } = false; + public bool ActualResult { get; private set; } = true; + public bool HasCompleted { get; private set; } = false; + public async Task ExecuteAsync(IConsumer consumer) { ActualResult = await consumer.GetAccessAsync(Request.First()); HasCompleted = true; return true; } + public void SetTruth(IEnforcer enforcer) { ExpectedResult = enforcer.Enforce(enforcer.CreateContext(), Request.First()); } - public IEnumerable Request { get { return _requests; } } + + public IEnumerable Request + { + get { return _requests; } + } + public TransactionType TransactionType { get; } = TransactionType.GetAccess; } -} \ No newline at end of file +} diff --git a/Casbin.UnitTests/RbacTests/RoleManagerTest.cs b/Casbin.UnitTests/RbacTests/RoleManagerTest.cs index 3a9cd90..5fcdaab 100644 --- a/Casbin.UnitTests/RbacTests/RoleManagerTest.cs +++ b/Casbin.UnitTests/RbacTests/RoleManagerTest.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; +using Casbin.Functions; using Casbin.Rbac; -using Casbin.Util; using Xunit; using static Casbin.UnitTests.Util.TestUtil; diff --git a/Casbin.UnitTests/UtilTests/BuiltInFunctionTest.cs b/Casbin.UnitTests/UtilTests/BuiltInFunctionTest.cs index 0168575..645bfe2 100644 --- a/Casbin.UnitTests/UtilTests/BuiltInFunctionTest.cs +++ b/Casbin.UnitTests/UtilTests/BuiltInFunctionTest.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using Casbin.Util; +using Casbin.Functions; using Xunit; namespace Casbin.UnitTests.UtilTests; @@ -34,97 +34,66 @@ public class BuiltInFunctionTest public static IEnumerable KeyGetTestData = new[] { - new object[] { "/foo", "/foo", "" }, - new object[] { "/foo", "/foo*", "" }, - new object[] { "/foo", "/foo/*", "" }, - new object[] { "/foo/bar", "/foo", "" }, - new object[] { "/foo/bar", "/foo*", "/bar" }, - new object[] { "/foo/bar", "/foo/*", "bar" }, - new object[] { "/foobar", "/foo", "" }, - new object[] { "/foobar", "/foo*", "bar" }, + new object[] { "/foo", "/foo", "" }, new object[] { "/foo", "/foo*", "" }, + new object[] { "/foo", "/foo/*", "" }, new object[] { "/foo/bar", "/foo", "" }, + new object[] { "/foo/bar", "/foo*", "/bar" }, new object[] { "/foo/bar", "/foo/*", "bar" }, + new object[] { "/foobar", "/foo", "" }, new object[] { "/foobar", "/foo*", "bar" }, new object[] { "/foobar", "/foo/*", "" } }; public static IEnumerable KeyGet2TestData = new[] { - new object[] { "/foo", "/foo", "id", ""}, - new object[] { "/foo", "/foo*", "id", ""}, - new object[] { "/foo", "/foo/*", "id", ""}, - new object[] { "/foo/bar", "/foo", "id", ""}, - new object[] { "/foo/bar", "/foo*", "id", ""}, // different with KeyMatch. - new object[] { "/foo/bar", "/foo/*", "id", ""}, - new object[] { "/foobar", "/foo", "id", ""}, - new object[] { "/foobar", "/foo*", "id", ""}, // different with KeyMatch. - new object[] { "/foobar", "/foo/*", "id", ""}, - - new object[] { "/", "/:resource", "resource", ""}, - new object[] { "/resource1", "/:resource", "resource", "resource1"}, - new object[] { "/myid", "/:id/using/:resId", "id", ""}, - new object[] { "/myid/using/myresid", "/:id/using/:resId", "id", "myid"}, - new object[] { "/myid/using/myresid", "/:id/using/:resId", "resId", "myresid"}, - - new object[] { "/proxy/myid", "/proxy/:id/*", "id", ""}, - new object[] { "/proxy/myid/", "/proxy/:id/*", "id", "myid"}, - new object[] { "/proxy/myid/res", "/proxy/:id/*", "id", "myid"}, - new object[] { "/proxy/myid/res/res2", "/proxy/:id/*", "id", "myid"}, - new object[] { "/proxy/myid/res/res2/res3", "/proxy/:id/*", "id", "myid"}, - new object[] { "/proxy/myid/res/res2/res3", "/proxy/:id/res/*", "id", "myid"}, - new object[] { "/proxy/", "/proxy/:id/*", "id", ""}, - - new object[] { "/alice", "/:id", "id", "alice"}, - new object[] { "/alice/all", "/:id/all", "id", "alice"}, - new object[] { "/alice", "/:id/all", "id", ""}, - new object[] { "/alice/all", "/:id", "id", ""}, - - new object[] { "/alice/all", "/:/all", "", ""} + new object[] { "/foo", "/foo", "id", "" }, new object[] { "/foo", "/foo*", "id", "" }, + new object[] { "/foo", "/foo/*", "id", "" }, new object[] { "/foo/bar", "/foo", "id", "" }, + new object[] { "/foo/bar", "/foo*", "id", "" }, // different with KeyMatch. + new object[] { "/foo/bar", "/foo/*", "id", "" }, new object[] { "/foobar", "/foo", "id", "" }, + new object[] { "/foobar", "/foo*", "id", "" }, // different with KeyMatch. + new object[] { "/foobar", "/foo/*", "id", "" }, new object[] { "/", "/:resource", "resource", "" }, + new object[] { "/resource1", "/:resource", "resource", "resource1" }, + new object[] { "/myid", "/:id/using/:resId", "id", "" }, + new object[] { "/myid/using/myresid", "/:id/using/:resId", "id", "myid" }, + new object[] { "/myid/using/myresid", "/:id/using/:resId", "resId", "myresid" }, + new object[] { "/proxy/myid", "/proxy/:id/*", "id", "" }, + new object[] { "/proxy/myid/", "/proxy/:id/*", "id", "myid" }, + new object[] { "/proxy/myid/res", "/proxy/:id/*", "id", "myid" }, + new object[] { "/proxy/myid/res/res2", "/proxy/:id/*", "id", "myid" }, + new object[] { "/proxy/myid/res/res2/res3", "/proxy/:id/*", "id", "myid" }, + new object[] { "/proxy/myid/res/res2/res3", "/proxy/:id/res/*", "id", "myid" }, + new object[] { "/proxy/", "/proxy/:id/*", "id", "" }, new object[] { "/alice", "/:id", "id", "alice" }, + new object[] { "/alice/all", "/:id/all", "id", "alice" }, new object[] { "/alice", "/:id/all", "id", "" }, + new object[] { "/alice/all", "/:id", "id", "" }, new object[] { "/alice/all", "/:/all", "", "" } }; public static IEnumerable KeyGet3TestData = new[] { new object[] { "/", "/{resource}", "resource", "" }, - new object[] { "/resource1", "/{resource}", "resource", "resource1" }, - new object[] { "/myid", "/{id}/using/{resId}", "id", "" }, - new object[] { "/myid/using/myresid", "/{id}/using/{resId}", "id", "myid" }, - new object[] { "/myid/using/myresid", "/{id}/using/{resId}", "resId", "myresid" }, - - new object[] { "/proxy/myid", "/proxy/{id}/*", "id", "" }, - new object[] { "/proxy/myid/", "/proxy/{id}/*", "id", "myid" }, - new object[] { "/proxy/myid/res", "/proxy/{id}/*", "id", "myid" }, - new object[] { "/proxy/myid/res/res2", "/proxy/{id}/*", "id", "myid" }, - new object[] { "/proxy/myid/res/res2/res3", "/proxy/{id}/*", "id", "myid" }, - new object[] { "/proxy/", "/proxy/{id}/*", "id", "" }, - - - new object[] { "/api/group1_group_name/project1_admin/info", "/api/{proj}_admin/info", - "proj", "" }, - + new object[] { "/api/group1_group_name/project1_admin/info", "/api/{proj}_admin/info", "proj", "" }, new object[] { "/{id/using/myresid", "/{id/using/{resId}", "resId", "myresid" }, - new object[] { "/{id/using/myresid/status}", "/{id/using/{resId}/status}", "resId", "myresid" }, - - new object[] { "/proxy/myid/res/res2/res3", "/proxy/{id}/*/{res}", "res", "res3" }, - new object[] { "/api/project1_admin/info", "/api/{proj}_admin/info", "proj", "project1" }, - - new object[] { "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", - "g", "group1" }, - - new object[] { "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", - "gn", "group_name" }, - - new object[] { "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", - "proj", "project1" } + new object[] + { + "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", "g", "group1" + }, + new object[] + { + "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", "gn", "group_name" + }, + new object[] + { + "/api/group1_group_name/project1_admin/info", "/api/{g}_{gn}/{proj}_admin/info", "proj", "project1" + } }; public static IEnumerable keyMatchTestData = new[] diff --git a/Casbin/Abstractions/Persist/ISingleAdapter.cs b/Casbin/Abstractions/Persist/ISingleAdapter.cs index 5e6c608..85d83a8 100644 --- a/Casbin/Abstractions/Persist/ISingleAdapter.cs +++ b/Casbin/Abstractions/Persist/ISingleAdapter.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Threading.Tasks; +using System.Threading.Tasks; using Casbin.Model; namespace Casbin.Persist diff --git a/Casbin/Caching/GFunctionCache.cs b/Casbin/Caching/GFunctionCache.cs index 19a315a..cac51f5 100644 --- a/Casbin/Caching/GFunctionCache.cs +++ b/Casbin/Caching/GFunctionCache.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; -using System.Collections.Concurrent; - +using System.Collections.Concurrent; namespace Casbin.Caching; @@ -18,6 +16,11 @@ public bool TryGet(string name1, string name2, out bool result, string domain = return _cache.TryGetValue(Key(name1, name2, domain), out result); } + public void Clear() + { + _cache.Clear(); + } + private static string Key(string name1, string name2, string domain = null) { bool hasDomain = domain is not null; @@ -25,9 +28,4 @@ private static string Key(string name1, string name2, string domain = null) ? string.Join(":", name1, name2, domain) : string.Join(":", name1, name2); } - - public void Clear() - { - _cache.Clear(); - } } diff --git a/Casbin/Casbin.csproj b/Casbin/Casbin.csproj index 2d6f565..1bfa420 100644 --- a/Casbin/Casbin.csproj +++ b/Casbin/Casbin.csproj @@ -108,4 +108,5 @@ + diff --git a/Casbin/EnforceView.cs b/Casbin/EnforceView.cs index 2eaaebc..cc0dc99 100644 --- a/Casbin/EnforceView.cs +++ b/Casbin/EnforceView.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Text.RegularExpressions; diff --git a/Casbin/Evaluation/ExpressionHandler.cs b/Casbin/Evaluation/ExpressionHandler.cs index 358ec34..3206eb1 100644 --- a/Casbin/Evaluation/ExpressionHandler.cs +++ b/Casbin/Evaluation/ExpressionHandler.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Threading; using Casbin.Caching; +using Casbin.Functions; using Casbin.Model; using DynamicExpresso; diff --git a/Casbin/Extensions/Model/ModelExtension.cs b/Casbin/Extensions/Model/ModelExtension.cs index 93c3a2f..08abae3 100644 --- a/Casbin/Extensions/Model/ModelExtension.cs +++ b/Casbin/Extensions/Model/ModelExtension.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using Casbin.Functions; using Casbin.Persist; using Casbin.Rbac; -using Casbin.Util; namespace Casbin.Model { diff --git a/Casbin/Util/BuiltInFunctions.cs b/Casbin/Functions/BuiltInFunctions.cs similarity index 97% rename from Casbin/Util/BuiltInFunctions.cs rename to Casbin/Functions/BuiltInFunctions.cs index 5e4d9a0..69f3637 100644 --- a/Casbin/Util/BuiltInFunctions.cs +++ b/Casbin/Functions/BuiltInFunctions.cs @@ -6,7 +6,7 @@ using Casbin.Rbac; using DotNet.Globbing; -namespace Casbin.Util +namespace Casbin.Functions { public static class BuiltInFunctions { @@ -15,7 +15,6 @@ public static class BuiltInFunctions private static readonly Regex s_keyMatch4Regex = new(@"\{([^/]+)\}"); private static readonly Regex s_keyGet2Regex = new(@":[^/]+"); private static readonly Regex s_keyGet3Regex = new(@"\{[^/]+?\}"); - private delegate bool GFunction(string subject1, string subject2, string domain = null); /// /// KeyGet returns the matched part @@ -28,14 +27,15 @@ public static class BuiltInFunctions public static string KeyGet(string key1, string key2) { int m = key1.Length, n = key2.Length; - if(m < n) return ""; + if (m < n) return ""; var span1 = key1.AsSpan(); var span2 = key2.AsSpan(); for (int i = 0; i < n; i++) { - if(span1[i] != span2[i]) + if (span1[i] != span2[i]) return span2[i] == '*' ? span1.Slice(i, m - i).ToString() : ""; } + return ""; } @@ -56,18 +56,20 @@ public static string KeyGet2(string key1, string key2, string pathVar) replacedKey2 = "^" + replacedKey2 + "$"; var regex = new Regex(replacedKey2); var values = regex.Matches(key1); - if(values.Count == 0) + if (values.Count == 0) { return ""; } + var pathVarSpan = pathVar.AsSpan(); for (int i = 0; i < keys.Count; i++) { - if(keys[i].Value.AsSpan(1).SequenceEqual(pathVarSpan)) + if (keys[i].Value.AsSpan(1).SequenceEqual(pathVarSpan)) { - return values[0].Groups[i+1].Value; + return values[0].Groups[i + 1].Value; } } + return ""; } @@ -92,6 +94,7 @@ public static string KeyGet3(string key1, string key2, string pathVar) { return ""; } + var pathVarSpan = pathVar.AsSpan(); for (int i = 0; i < keys.Count; i++) { @@ -100,6 +103,7 @@ public static string KeyGet3(string key1, string key2, string pathVar) return values[0].Groups[i + 1].Value; } } + return ""; } @@ -208,6 +212,7 @@ public static bool KeyMatch4(string key1, string key2) valueDictionary.Add(token, group[i + 1].Value); continue; } + if (valueDictionary[token] != group[i + 1].Value) { return false; @@ -216,6 +221,7 @@ public static bool KeyMatch4(string key1, string key2) return true; } + /// /// KeyMatch determines whether key1 matches the pattern of key2 and ignores the parameters in key2. /// For example, "/foo/bar?status=1&type=2" matches "/foo/bar" @@ -323,7 +329,10 @@ bool GFunction(string name1, string name2, string domain = null) cache.Set(name1, name2, result, domain); return result; } - return (GFunction) GFunction; + + return (GFunction)GFunction; } + + private delegate bool GFunction(string subject1, string subject2, string domain = null); } } diff --git a/Casbin/Model/FunctionMap.cs b/Casbin/Functions/FunctionMap.cs similarity index 86% rename from Casbin/Model/FunctionMap.cs rename to Casbin/Functions/FunctionMap.cs index bfc7701..7fd266c 100644 --- a/Casbin/Model/FunctionMap.cs +++ b/Casbin/Functions/FunctionMap.cs @@ -1,8 +1,7 @@ using System; using System.Collections.Generic; -using Casbin.Util; -namespace Casbin.Model +namespace Casbin.Functions { internal class FunctionMap { @@ -15,10 +14,7 @@ private void AddFunction(string name, Delegate function) internal static FunctionMap LoadFunctionMap() { - var map = new FunctionMap - { - FunctionDict = new Dictionary() - }; + var map = new FunctionMap { FunctionDict = new Dictionary() }; map.AddFunction("keyGet", BuiltInFunctions.KeyGet); map.AddFunction("keyGet2", BuiltInFunctions.KeyGet2); diff --git a/Casbin/Model/DefaultModel.cs b/Casbin/Model/DefaultModel.cs index ef558c5..e77f6de 100644 --- a/Casbin/Model/DefaultModel.cs +++ b/Casbin/Model/DefaultModel.cs @@ -5,9 +5,9 @@ using Casbin.Config; using Casbin.Effect; using Casbin.Evaluation; +using Casbin.Functions; using Casbin.Model.Holder; using Casbin.Rbac; -using Casbin.Util; namespace Casbin.Model { diff --git a/Casbin/Model/Request.cs b/Casbin/Model/Request.cs index a648d76..4372010 100644 --- a/Casbin/Model/Request.cs +++ b/Casbin/Model/Request.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace Casbin.Model; +namespace Casbin.Model; public static class Request {