Comments (6)
@sagilio @sociometry @AsakusaRinne
from casbin.net.
@marifaleiros hi, we cannot reproduce your issue at: https://editor.casbin.org/#2CNB8XD9F , you can see the result is false
here. Please also share your editor page
from casbin.net.
@marifaleiros
from casbin.net.
@marifaleiros
Hi, as mentioned in the query above, the issue you raised is not reproduced in the online editor, but I have modified it to some extent, the link (https://editor.casbin.org/#7VRMT6ZLH), and I think this is the problem you are experiencing.
I guess there are three problems with your code, the first one about regular expression syntax, the second one about needing to register regular matching methods with the enforcer, and the third one maybe that you forgot to add the grouping policy.
The code that I think is correct is as follows, I hope it helps you.
// using System;
using NetCasbin;
using NetCasbin.Model;
// using System.Linq;
using System.Text.RegularExpressions;
// using System.Reflection;
using NetCasbin.Extensions;
using NetCasbin.Util;
// using NetCasbin.Util.Function;
public class Program
{
public static void Main()
{
var modelText = @"[request_definition]
r = sub, dom, obj, act
[policy_definition]
p = sub, dom, obj, act, eft
[role_definition]
g = _, _, _
[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
[matchers]
m = g(r.sub, p.sub, r.dom) && r.dom == p.dom && regexMatch(r.obj, p.obj) && regexMatch(r.act, p.act)";
var casbinRules = @"
p, MyResourceName, domain-123, my-resourceId, access, allow
g, .*, Team::TeamEveryoneKey, domain-123
g, Team::TeamEveryoneKey, MyResourceName, domain-123
";
// 1 (not '*' but '.*')
var model = Model.CreateDefaultFromText(modelText);
var enforcer = new Enforcer(model);
enforcer.EnableAutoBuildRoleLinks(false);
// 2 (https://casbin.org/zh/docs/rbac-with-pattern)
enforcer.AddNamedMatchingFunc("g", BuiltInFunctions.RegexMatch);
var rules = Regex.Split(casbinRules, "\r\n|\r|\n");
var policies = rules.Where(l => l.StartsWith("p")).Select(x => x.Split(',').Skip(1).Select(y => y.Trim()).ToList()).ToList();
Console.WriteLine(string.Join(",", policies.SelectMany(p => p)));
enforcer.AddPolicies(policies);
var groups = rules.Where(l => l.StartsWith("g")).Select(x => x.Split(',').Skip(1).Select(y => y.Trim()).ToList()).ToList();
Console.WriteLine(string.Join(",", groups.SelectMany(p => p)));
Console.WriteLine(string.Join(",", groups));
// 3 (Maybe you missed it)
enforcer.AddGroupingPolicies(groups);
enforcer.BuildRoleLinks();
var result = enforcer.EnforceEx("alice", "domain-123", "my-resourceId", "access");
Console.WriteLine(result.Result);
Console.WriteLine(result.Explains.FirstOrDefault());
}
}
from casbin.net.
@marifaleiros any update?
from casbin.net.
Closed as stale
from casbin.net.
Related Issues (20)
- .NET 6 support HOT 6
- InvalidOperationException when Enforcing HOT 4
- ParseException when pass a `RequestValue` to `Enforce` method HOT 9
- Fix CI error HOT 4
- An expression cannot be used in this context because it cannot be passed or returned by reference HOT 4
- Enforcer.Enforce throws exception when model contains similar definitions HOT 3
- Does using Casbin.NET support defining multiple M's in matchers? For example, m1 m2 HOT 3
- Casbin .NET doesn't support commas in policy HOT 3
- Where can I find ASP.NET 6 examples? HOT 7
- Check collection in ABAC with policy HOT 4
- Updating Policies directly on the IPolicyStore model not reflected in Enforcer HOT 4
- Use semantic-release to auto release new versions HOT 2
- Apply politic to objects of different class going to excaption, but in posible in Go version HOT 5
- .NET library does not support multi-line mode with '\' line feeds HOT 1
- Cannot add named groups in Casbin v2 HOT 3
- Cannot get Policy with null value in EF core Adapter. HOT 8
- Enforcer hangs during multi-threaded modifications HOT 8
- bug: fix CI error: error: Response status code does not indicate success: 413 (The total packages size for the pending uploads is too large. The feed owner may have to upgrade the MyGet subscription to resolve this issue.). HOT 2
- How to return a custom type instead of using IQueryable type in IPolicyFilter HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from casbin.net.