From f2671d6ab7d207e8f5177c82b0a3cdb0d326c13a Mon Sep 17 00:00:00 2001 From: DacongDA Date: Wed, 3 Apr 2024 23:30:47 +0800 Subject: [PATCH] fix: fix bugs in policy equal and persist --- Casbin/Abstractions/Model/IPolicyValues.cs | 2 ++ Casbin/Model/PolicyValues.cs | 20 +++++++++++++++++++- Casbin/Persist/PersistantPolicy.cs | 4 ++-- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Casbin/Abstractions/Model/IPolicyValues.cs b/Casbin/Abstractions/Model/IPolicyValues.cs index 41e3371..6783b4e 100644 --- a/Casbin/Abstractions/Model/IPolicyValues.cs +++ b/Casbin/Abstractions/Model/IPolicyValues.cs @@ -10,4 +10,6 @@ public interface IPolicyValues : IList public string ToText(); public bool Equals(IPolicyValues other); + + int GetRealCount(); } diff --git a/Casbin/Model/PolicyValues.cs b/Casbin/Model/PolicyValues.cs index 720bd46..f6b254a 100644 --- a/Casbin/Model/PolicyValues.cs +++ b/Casbin/Model/PolicyValues.cs @@ -17,6 +17,9 @@ public record PolicyValues : IPolicyValues public bool IsReadOnly => false; int ICollection.Count => Values.Count; + + + int IPolicyValues.GetRealCount() => Count; string IList.this[int index] { get => Values[index]; set => new NotImplementedException(); } @@ -34,7 +37,22 @@ internal static string ToText(IEnumerable values) => string.Join(PermConstants.PolicySeparatorString, values); internal static bool Equals(T values, T other) where T : IPolicyValues - => values.Count == other.Count && values.ToText().Equals(other.ToText()); + { + if (values.GetRealCount() != other.GetRealCount()) + { + return false; + } + + for(int i = 0; i< values.GetRealCount(); i++) + { + if (values[i] != other[i]) + { + return false; + } + } + + return true; + } internal static string ToStringValue(T value) => value as string ?? value.ToString(); diff --git a/Casbin/Persist/PersistantPolicy.cs b/Casbin/Persist/PersistantPolicy.cs index b6567e4..61f1427 100644 --- a/Casbin/Persist/PersistantPolicy.cs +++ b/Casbin/Persist/PersistantPolicy.cs @@ -31,7 +31,7 @@ public static TPersistPolicy Create(string section, string type, where TPersistPolicy : IPersistPolicy, new() { TPersistPolicy persistPolicy = new TPersistPolicy { Section = section, Type = type }; - switch (values.Count) + switch (values.GetRealCount()) { case 1: persistPolicy.Value1 = values[0]; @@ -136,7 +136,7 @@ public static TPersistPolicy Create(string section, string type, persistPolicy.Value12 = values[11]; break; default: - throw new ArgumentOutOfRangeException(nameof(values), values.Count, + throw new ArgumentOutOfRangeException(nameof(values), values.GetRealCount(), "The number of values must be between 1 and 12."); }