Skip to content

Commit 83d7d82

Browse files
committed
perf: Support priority deny override model without recursion.
Signed-off-by: Sagilio <[email protected]>
1 parent c96da8e commit 83d7d82

File tree

1 file changed

+24
-27
lines changed

1 file changed

+24
-27
lines changed

NetCasbin/CoreEnforcer.cs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -832,11 +832,11 @@ private Task<bool> InternalEnforceAsync(IReadOnlyList<object> requestValues, str
832832
/// <param name="requestValues">The request needs to be mediated, usually an array of strings,
833833
/// can be class instances if ABAC is used.</param>
834834
/// <param name="matcher">The custom matcher.</param>
835-
/// <param name="explains"></param>
835+
/// <param name="explains">Collection of matched policy explains</param>
836836
/// <returns>Whether to allow the request.</returns>
837837
private bool InternalEnforce(IReadOnlyList<object> requestValues, string matcher = null, ICollection<IEnumerable<string>> explains = null)
838838
{
839-
EnforceContext context = EnforceContext.Create(model, matcher, explains is not null);
839+
var context = EnforceContext.Create(model, matcher, explains is not null);
840840

841841
if (context.RequestTokens.Count != requestValues.Count)
842842
{
@@ -955,36 +955,41 @@ private bool InternalEnforce(IReadOnlyList<object> requestValues, string matcher
955955
/// <param name="context">Storage of enforcer variables</param>
956956
/// <param name="requestValues">The request needs to be mediated, usually an array of strings, can be class instances if ABAC is used.</param>
957957
/// <param name="explains">Collection of matched policy explains</param>
958-
/// <param name="maxPriority">Index of maxPriority to filter out lower tier policies</param>
959958
/// <returns>Whether to allow the request.</returns>
960959
private bool InternalEnforceWithChainEffector(
961960
EnforceContext context,
962961
IChainEffector chainEffector,
963962
IReadOnlyList<object> requestValues = null,
964-
ICollection<IEnumerable<string>> explains = null,
965-
PolicyEffectType effectType = PolicyEffectType.Custom,
966-
int maxPriority = int.MaxValue)
963+
ICollection<IEnumerable<string>> explains = null)
967964
{
968-
bool result = false;
965+
bool finalResult = false;
969966
chainEffector.StartChain(context.Effect);
970967

971968
bool hasPriority = context.PolicyAssertion.TryGetPriorityIndex(out int priorityIndex);
969+
bool isPriorityDenyOverrideEfffet = chainEffector.PolicyEffectType is PolicyEffectType.PriorityDenyOverride;
970+
int? priority = null;
972971

973972
if (context.Policies.Count is not 0)
974973
{
975-
IEnumerable<IReadOnlyList<string>> policies = context.Policies;
976-
if (hasPriority && chainEffector.PolicyEffectType is PolicyEffectType.PriorityDenyOverride)
977-
{
978-
policies = policies.Where(t => maxPriority == int.MaxValue || int.Parse(t[priorityIndex]) == maxPriority);
979-
}
980-
981-
foreach (IReadOnlyList<string> policyValues in policies)
974+
foreach (IReadOnlyList<string> policyValues in context.Policies)
982975
{
983976
if (context.PolicyTokens.Count != policyValues.Count)
984977
{
985978
throw new ArgumentException($"Invalid policy size: expected {context.PolicyTokens.Count}, got {policyValues.Count}.");
986979
}
987980

981+
if (hasPriority && isPriorityDenyOverrideEfffet)
982+
{
983+
if (int.TryParse(policyValues[priorityIndex], out int nowPriority))
984+
{
985+
if (priority.HasValue && nowPriority != priority.Value)
986+
{
987+
break;
988+
}
989+
priority = nowPriority;
990+
}
991+
}
992+
988993
ExpressionHandler.SetPolicyParameters(policyValues);
989994

990995
bool expressionResult;
@@ -1001,14 +1006,6 @@ private bool InternalEnforceWithChainEffector(
10011006

10021007
var nowEffect = GetEffect(expressionResult);
10031008

1004-
if (context.Effect.Equals(PermConstants.PolicyEffect.PriorityDenyOverride)
1005-
&& nowEffect == Effect.Effect.Allow
1006-
&& maxPriority == int.MaxValue)
1007-
{
1008-
return InternalEnforceWithChainEffector(context, chainEffector, requestValues, explains, effectType,
1009-
int.Parse(policyValues[priorityIndex]));
1010-
}
1011-
10121009
if (nowEffect is not Effect.Effect.Indeterminate
10131010
&& ExpressionHandler.Parameters.TryGetValue("p_eft", out Parameter parameter))
10141011
{
@@ -1034,7 +1031,7 @@ private bool InternalEnforceWithChainEffector(
10341031
}
10351032
}
10361033

1037-
result = chainEffector.Result;
1034+
finalResult = chainEffector.Result;
10381035
}
10391036
else
10401037
{
@@ -1049,7 +1046,7 @@ private bool InternalEnforceWithChainEffector(
10491046

10501047
if (chainEffector.TryChain(nowEffect))
10511048
{
1052-
result = chainEffector.Result;
1049+
finalResult = chainEffector.Result;
10531050
}
10541051

10551052
if (context.Explain && chainEffector.HitPolicy)
@@ -1061,14 +1058,14 @@ private bool InternalEnforceWithChainEffector(
10611058
#if !NET45
10621059
if (context.Explain)
10631060
{
1064-
Logger?.LogEnforceResult(requestValues, result, explains);
1061+
Logger?.LogEnforceResult(requestValues, finalResult, explains);
10651062
}
10661063
else
10671064
{
1068-
Logger?.LogEnforceResult(requestValues, result);
1065+
Logger?.LogEnforceResult(requestValues, finalResult);
10691066
}
10701067
#endif
1071-
return result;
1068+
return finalResult;
10721069
}
10731070

10741071
private static Effect.Effect GetEffect(bool expressionResult)

0 commit comments

Comments
 (0)