Giter Site home page Giter Site logo

Comments (6)

casbin-bot avatar casbin-bot commented on May 27, 2024

@sagilio @sociometry @AsakusaRinne

from casbin.net.

hsluoyz avatar hsluoyz commented on May 27, 2024

@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

image

from casbin.net.

hsluoyz avatar hsluoyz commented on May 27, 2024

@marifaleiros

from casbin.net.

Tanyuu avatar Tanyuu commented on May 27, 2024

@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.

hsluoyz avatar hsluoyz commented on May 27, 2024

@marifaleiros any update?

from casbin.net.

hsluoyz avatar hsluoyz commented on May 27, 2024

Closed as stale

from casbin.net.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.