Пример #1
0
        public void testKeyMatchModelInMemory()
        {
            Model m = CoreEnforcer.NewModel();

            m.AddDef("r", "r", "sub, obj, act");
            m.AddDef("p", "p", "sub, obj, act");
            m.AddDef("e", "e", "some(where (p.eft == allow))");
            m.AddDef("m", "m", "r.sub == p.sub && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act)");

            IAdapter a = new DefaultFileAdapter("examples/keymatch_policy.csv");

            Enforcer e = new Enforcer(m, a);

            testEnforce(e, "alice", "/alice_data/resource1", "GET", true);
            testEnforce(e, "alice", "/alice_data/resource1", "POST", true);
            testEnforce(e, "alice", "/alice_data/resource2", "GET", true);
            testEnforce(e, "alice", "/alice_data/resource2", "POST", false);
            testEnforce(e, "alice", "/bob_data/resource1", "GET", false);
            testEnforce(e, "alice", "/bob_data/resource1", "POST", false);
            testEnforce(e, "alice", "/bob_data/resource2", "GET", false);
            testEnforce(e, "alice", "/bob_data/resource2", "POST", false);

            testEnforce(e, "bob", "/alice_data/resource1", "GET", false);
            testEnforce(e, "bob", "/alice_data/resource1", "POST", false);
            testEnforce(e, "bob", "/alice_data/resource2", "GET", true);
            testEnforce(e, "bob", "/alice_data/resource2", "POST", false);
            testEnforce(e, "bob", "/bob_data/resource1", "GET", false);
            testEnforce(e, "bob", "/bob_data/resource1", "POST", true);
            testEnforce(e, "bob", "/bob_data/resource2", "GET", false);
            testEnforce(e, "bob", "/bob_data/resource2", "POST", true);

            testEnforce(e, "cathy", "/cathy_data", "GET", true);
            testEnforce(e, "cathy", "/cathy_data", "POST", true);
            testEnforce(e, "cathy", "/cathy_data", "DELETE", false);

            e = new Enforcer(m);
            a.LoadPolicy(e.GetModel());

            testEnforce(e, "alice", "/alice_data/resource1", "GET", true);
            testEnforce(e, "alice", "/alice_data/resource1", "POST", true);
            testEnforce(e, "alice", "/alice_data/resource2", "GET", true);
            testEnforce(e, "alice", "/alice_data/resource2", "POST", false);
            testEnforce(e, "alice", "/bob_data/resource1", "GET", false);
            testEnforce(e, "alice", "/bob_data/resource1", "POST", false);
            testEnforce(e, "alice", "/bob_data/resource2", "GET", false);
            testEnforce(e, "alice", "/bob_data/resource2", "POST", false);

            testEnforce(e, "bob", "/alice_data/resource1", "GET", false);
            testEnforce(e, "bob", "/alice_data/resource1", "POST", false);
            testEnforce(e, "bob", "/alice_data/resource2", "GET", true);
            testEnforce(e, "bob", "/alice_data/resource2", "POST", false);
            testEnforce(e, "bob", "/bob_data/resource1", "GET", false);
            testEnforce(e, "bob", "/bob_data/resource1", "POST", true);
            testEnforce(e, "bob", "/bob_data/resource2", "GET", false);
            testEnforce(e, "bob", "/bob_data/resource2", "POST", true);

            testEnforce(e, "cathy", "/cathy_data", "GET", true);
            testEnforce(e, "cathy", "/cathy_data", "POST", true);
            testEnforce(e, "cathy", "/cathy_data", "DELETE", false);
        }
Пример #2
0
 private static Model.Model LoadModelFromMemory(Model.Model model, string policy)
 {
     model.ClearPolicy();
     using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(policy)))
     {
         DefaultFileAdapter fileAdapter = new DefaultFileAdapter(ms);
         fileAdapter.LoadPolicy(model);
     }
     model.RefreshPolicyStringSet();
     return(model);
 }