Skip to content

Commit

Permalink
chore: Complete cmments and fix typos
Browse files Browse the repository at this point in the history
Signed-off-by: Sagilio <Sagilio@outlook.com>
  • Loading branch information
sagilio committed Nov 6, 2021
1 parent 6797f37 commit 5295db5
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 48 deletions.
6 changes: 3 additions & 3 deletions NetCasbin.UnitTest/ModelTests/ModelTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ public void TestMultipleTypeModel()
e.BuildRoleLinks();

// Use default types
EnforceContext context = e.CreatContext();
EnforceContext context = e.CreateContext();

Assert.True(e.Enforce(context, "alice", "data1", "read"));
Assert.False(e.Enforce(context, "alice", "data1", "write"));
Expand All @@ -577,7 +577,7 @@ public void TestMultipleTypeModel()
Assert.False(e.Enforce(context, "bob", "data2", "write"));

// Use r2 p2 and m2 type
context = e.CreatContext
context = e.CreateContext
(
PermConstants.RequestType2,
PermConstants.PolicyType2,
Expand All @@ -592,7 +592,7 @@ public void TestMultipleTypeModel()
Assert.False(e.Enforce(context, "bob", "domain1", "data1", "write"));

// Use r3 p3 and m3 type
context = e.CreatContext
context = e.CreateContext
(
PermConstants.RequestType3,
PermConstants.PolicyType3,
Expand Down
4 changes: 3 additions & 1 deletion NetCasbin/Abstractions/IEnforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ public interface IEnforcer
/// Decides whether a "subject" can access a "object" with the operation
/// "action", input parameters are usually: (sub, obj, act).
/// </summary>
/// <param name="context"></param>
/// <param name="requestValues">The request needs to be mediated, usually an array of strings,
/// can be class instances if ABAC is used.</param>
/// <returns>Whether to allow the request.</returns>
public bool Enforce(in EnforceContext context, params object[] requestValues);

/// <summary>
/// Decides whether a "subject" can access a "object" with the operation
/// "action", input parameters are usually: (sub, obj, act).
/// </summary>
/// <param name="context"></param>
/// <param name="requestValues">The request needs to be mediated, usually an array of strings,
/// can be class instances if ABAC is used.</param>
/// <returns>Whether to allow the request.</returns>
Expand Down
11 changes: 4 additions & 7 deletions NetCasbin/EnforceContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace Casbin
public readonly struct EnforceContext
{
public EnforceContext(
Assertion requestAssertion, Assertion policyAssertion,
IReadOnlyList<IReadOnlyList<string>> policies,
IReadOnlyAssertion requestAssertion, IReadOnlyAssertion policyAssertion,
string effect, string matcher,
bool hasEval, bool explain)
{
Expand Down Expand Up @@ -58,17 +57,16 @@ public static EnforceContext Create(
(
requestAssertion: requestAssertion,
policyAssertion: policyAssertion,
policies: policyAssertion.Policy,
effect: model.GetRequiredAssertion(PermConstants.Section.PolicyEffectSection, effectType).Value,
matcher: matcher,
hasEval: hasEval,
explain: explain
);
}

public static EnforceContext CreatWithMatcher(IEnforcer enforcer, string matcher, bool explain)
public static EnforceContext CreateWithMatcher(IEnforcer enforcer, string matcher, bool explain)
{
return CreatWithMatcher(
return CreateWithMatcher(
enforcer,
matcher,
PermConstants.DefaultRequestType,
Expand All @@ -77,7 +75,7 @@ public static EnforceContext CreatWithMatcher(IEnforcer enforcer, string matcher
explain);
}

public static EnforceContext CreatWithMatcher(
public static EnforceContext CreateWithMatcher(
IEnforcer enforcer,
string matcher,
string requestType = PermConstants.DefaultRequestType,
Expand All @@ -94,7 +92,6 @@ public static EnforceContext CreatWithMatcher(
(
requestAssertion: requestAssertion,
policyAssertion: policyAssertion,
policies: policyAssertion.Policy,
effect: model.GetRequiredAssertion(PermConstants.Section.PolicyEffectSection, effectType).Value,
matcher: matcher,
hasEval: hasEval,
Expand Down
2 changes: 1 addition & 1 deletion NetCasbin/EnforceSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal struct EnforceSession
internal bool ExpressionResult { get; set; }

internal bool IsChainEffector { get; set; }
internal IEffectChain effectChain { get; set; }
internal IEffectChain EffectChain { get; set; }

internal bool HasPriority { get; set; }
internal int PriorityIndex { get; set; }
Expand Down
17 changes: 7 additions & 10 deletions NetCasbin/Enforcer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public IReadOnlyAdapter Adapter
}
public IWatcher Watcher { get; set; }
public IRoleManager RoleManager { get; set; } = new DefaultRoleManager(10);
public IEnforceCache EnforceCache { get; set; }
public IEnforceCache EnforceCache { get; set; } = new ReaderWriterEnforceCache(new ReaderWriterEnforceCacheOptions());
public IExpressionHandler ExpressionHandler { get; set; }
#if !NET45
public ILogger Logger { get; set; }
Expand All @@ -86,11 +86,11 @@ public IReadOnlyAdapter Adapter
public bool IsFiltered => Adapter is IFilteredAdapter {IsFiltered: true};

#region Enforce method

/// <summary>
/// Decides whether a "subject" can access a "object" with the operation
/// "action", input parameters are usually: (sub, obj, act).
/// </summary>
/// <param name="context">Enforce context include all status on enforcing</param>
/// <param name="requestValues">The request needs to be mediated, usually an array of strings,
/// can be class instances if ABAC is used.</param>
/// <returns>Whether to allow the request.</returns>
Expand All @@ -112,7 +112,6 @@ public bool Enforce(in EnforceContext context, params object[] requestValues)
}

string key = string.Join("$$", requestValues);
EnforceCache ??= new ReaderWriterEnforceCache(new ReaderWriterEnforceCacheOptions());
if (EnforceCache.TryGetResult(requestValues, key, out bool cachedResult))
{
#if !NET45
Expand All @@ -122,9 +121,7 @@ public bool Enforce(in EnforceContext context, params object[] requestValues)
}

bool result = InternalEnforce(context, PolicyManager, requestValues);
EnforceCache ??= new ReaderWriterEnforceCache(new ReaderWriterEnforceCacheOptions());
EnforceCache.TrySetResult(requestValues, key, result);

#if !NET45
LogEnforceResult(context, requestValues, result);
#endif
Expand All @@ -135,6 +132,7 @@ public bool Enforce(in EnforceContext context, params object[] requestValues)
/// Decides whether a "subject" can access a "object" with the operation
/// "action", input parameters are usually: (sub, obj, act).
/// </summary>
/// <param name="context">Enforce context</param>
/// <param name="requestValues">The request needs to be mediated, usually an array of strings,
/// can be class instances if ABAC is used.</param>
/// <returns>Whether to allow the request.</returns>
Expand Down Expand Up @@ -262,22 +260,22 @@ private ref EnforceSession HandleInitialRequest(in EnforceContext context, ref E

if (session.IsChainEffector)
{
session.effectChain = chainEffector.CreateChain(context.Effect);
session.EffectChain = chainEffector.CreateChain(context.Effect);
}
else
{
session.PolicyEffects = new PolicyEffect[session.PolicyCount];
}

session.EffectExpressionType = session.effectChain?.EffectExpressionType ?? DefaultEffector.ParseEffectExpressionType(session.ExpressionString);
session.EffectExpressionType = session.EffectChain?.EffectExpressionType ?? DefaultEffector.ParseEffectExpressionType(session.ExpressionString);
session.HasPriority = context.PolicyAssertion.TryGetTokenIndex("priority", out int priorityIndex);
session.PriorityIndex = priorityIndex;
return ref session;
}

private ref EnforceSession HandleBeforeExpression(in EnforceContext context, ref EnforceSession session)
{
IEffectChain effectChain = session.effectChain;
IEffectChain effectChain = session.EffectChain;
int policyTokenCount = context.PolicyAssertion.Tokens.Count;

if (session.PolicyCount is 0)
Expand Down Expand Up @@ -373,13 +371,12 @@ private static ref EnforceSession HandleExpressionResult(in EnforceContext conte

private static ref EnforceSession HandleExpressionResult(in EnforceContext context, ref EnforceSession session)
{
IEffectChain effectChain = session.effectChain;
IEffectChain effectChain = session.EffectChain;
PolicyEffect nowEffect;
if (session.PolicyCount is 0)
{
nowEffect = GetEffect(session.ExpressionResult);


if (effectChain.TryChain(nowEffect))
{
session.DetermineResult(effectChain.Result);
Expand Down
Loading

0 comments on commit 5295db5

Please sign in to comment.