Skip to content

Commit

Permalink
Support build Incremental role links
Browse files Browse the repository at this point in the history
Signed-off-by: Sagilio <Sagilio@outlook.com>
  • Loading branch information
sagilio committed Feb 4, 2021
1 parent e1b9cc5 commit 05a0e04
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 49 deletions.
6 changes: 6 additions & 0 deletions Casbin.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Casbin.Benchmark", "Casbin.Benchmark\Casbin.Benchmark.csproj", "{1DBC2931-4981-4DB5-A30B-FF6EB8622B04}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Casbin.Samples", "Casbin.Samples\Casbin.Samples.csproj", "{BE93FBFB-0D11-4647-BD4A-48FDDF3DACB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -41,6 +43,10 @@ Global
{1DBC2931-4981-4DB5-A30B-FF6EB8622B04}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1DBC2931-4981-4DB5-A30B-FF6EB8622B04}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1DBC2931-4981-4DB5-A30B-FF6EB8622B04}.Release|Any CPU.Build.0 = Release|Any CPU
{BE93FBFB-0D11-4647-BD4A-48FDDF3DACB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BE93FBFB-0D11-4647-BD4A-48FDDF3DACB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BE93FBFB-0D11-4647-BD4A-48FDDF3DACB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BE93FBFB-0D11-4647-BD4A-48FDDF3DACB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
25 changes: 25 additions & 0 deletions Casbin.Samples/Casbin.Samples.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NetCasbin\NetCasbin.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="Examples\rbac_model.conf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Examples\rbac_policy.csv">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions Casbin.Samples/Examples/rbac_model.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[role_definition]
g = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
5 changes: 5 additions & 0 deletions Casbin.Samples/Examples/rbac_policy.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
p, alice, data1, read
p, bob, data2, write
p, data2_admin, data2, read
p, data2_admin, data2, write
g, alice, data2_admin
23 changes: 23 additions & 0 deletions Casbin.Samples/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NetCasbin;

namespace Casbin.Samples
{
class Program
{
static void Main(string[] args)
{
Enforcer enforcer = new (
"Examples/rbac_model.conf",
"Examples/rbac_policy.csv");

//var provider = new ServiceCollection()
// .AddLogging(builder => builder.AddConsole())
// .BuildServiceProvider();

Console.ReadKey();
}
}
}
103 changes: 80 additions & 23 deletions NetCasbin/InternalEnforcer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using NetCasbin.Model;

Expand Down Expand Up @@ -44,7 +45,14 @@ protected bool InternalAddPolicy(string sec, string ptype, List<string> rule)
return false;
}

NoticePolicyChanged(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLink(roleManager, PolicyOperation.PolicyAdd,
sec, ptype, rule);
ExpressionHandler.SetGFunctions();
}

NotifyPolicyChanged();
return true;
}

Expand Down Expand Up @@ -81,7 +89,14 @@ protected async Task<bool> InternalAddPolicyAsync(string sec, string ptype, List
return false;
}

await NoticePolicyChangedAsync(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLink(roleManager, PolicyOperation.PolicyAdd,
sec, ptype, rule);
ExpressionHandler.SetGFunctions();
}

await NotifyPolicyChangedAsync();
return true;
}

Expand Down Expand Up @@ -120,7 +135,14 @@ protected bool InternalAddPolicies(string sec, string ptype, IEnumerable<List<st
return false;
}

NoticePolicyChanged(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLinks(roleManager, PolicyOperation.PolicyAdd,
sec, ptype, ruleArray);
ExpressionHandler.SetGFunctions();
}

NotifyPolicyChanged();
return true;
}

Expand Down Expand Up @@ -160,7 +182,14 @@ protected async Task<bool> InternalAddPoliciesAsync(string sec, string ptype, IE
return false;
}

await NoticePolicyChangedAsync(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLinks(roleManager, PolicyOperation.PolicyAdd,
sec, ptype, ruleArray);
ExpressionHandler.SetGFunctions();
}

await NotifyPolicyChangedAsync();
return true;
}

Expand Down Expand Up @@ -197,7 +226,14 @@ protected bool InternalRemovePolicy(string sec, string ptype, List<string> rule)
return false;
}

NoticePolicyChanged(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLink(roleManager, PolicyOperation.PolicyRemove,
sec, ptype, rule);
ExpressionHandler.SetGFunctions();
}

NotifyPolicyChanged();
return true;
}

Expand Down Expand Up @@ -234,7 +270,14 @@ protected async Task<bool> InternalRemovePolicyAsync(string sec, string ptype, L
return false;
}

await NoticePolicyChangedAsync(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLink(roleManager, PolicyOperation.PolicyRemove,
sec, ptype, rule);
ExpressionHandler.SetGFunctions();
}

await NotifyPolicyChangedAsync();
return true;
}

Expand Down Expand Up @@ -273,7 +316,14 @@ protected bool InternalRemovePolicies(string sec, string ptype, IEnumerable<List
return false;
}

NoticePolicyChanged(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLinks(roleManager, PolicyOperation.PolicyRemove,
sec, ptype, ruleArray);
ExpressionHandler.SetGFunctions();
}

NotifyPolicyChanged();
return true;
}

Expand Down Expand Up @@ -312,7 +362,14 @@ protected async Task<bool> InternalRemovePoliciesAsync(string sec, string ptype,
return false;
}

await NoticePolicyChangedAsync(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
model.BuildIncrementalRoleLinks(roleManager, PolicyOperation.PolicyRemove,
sec, ptype, ruleArray);
ExpressionHandler.SetGFunctions();
}

await NotifyPolicyChangedAsync();
return true;
}

Expand Down Expand Up @@ -345,7 +402,13 @@ protected bool InternalRemoveFilteredPolicy(string sec, string ptype, int fieldI
return false;
}

NoticePolicyChanged(sec);
if (sec.Equals(PermConstants.Section.RoleSection))
{
BuildRoleLinks();
ExpressionHandler.SetGFunctions();
}

NotifyPolicyChanged();
return true;
}

Expand Down Expand Up @@ -378,32 +441,26 @@ protected async Task<bool> InternalRemoveFilteredPolicyAsync(string sec, string
return false;
}

await NoticePolicyChangedAsync(sec);
return true;
}

private void NoticePolicyChanged(string section)
{
if (autoBuildRoleLinks && section.Equals(PermConstants.Section.RoleSection))
if (sec.Equals(PermConstants.Section.RoleSection))
{
BuildRoleLinks();
ExpressionHandler.SetGFunctions();
}

await NotifyPolicyChangedAsync();
return true;
}

private void NotifyPolicyChanged()
{
if (autoNotifyWatcher)
{
watcher?.Update();
}
}

private async Task NoticePolicyChangedAsync(string section)
private async Task NotifyPolicyChangedAsync()
{
if (autoBuildRoleLinks && section.Equals(PermConstants.Section.RoleSection))
{
BuildRoleLinks();
ExpressionHandler.SetGFunctions();
}

if (autoNotifyWatcher && watcher is not null)
{
await watcher.UpdateAsync();
Expand Down
Loading

0 comments on commit 05a0e04

Please sign in to comment.