예제 #1
0
        public void TestModifyPolicy()
        {
            var e = new Enforcer(_testModelFixture.GetNewRbacTestModel());

            e.BuildRoleLinks();

            TestGetPolicy(e, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write"),
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write")));

            e.RemovePolicy("alice", "data1", "read");
            e.RemovePolicy("bob", "data2", "write");
            e.RemovePolicy("alice", "data1", "read");
            e.AddPolicy("eve", "data3", "read");
            e.AddPolicy("eve", "data3", "read");

            var rules = AsList(
                AsList("jack", "data4", "read"),
                AsList("jack", "data4", "read"),
                AsList("jack", "data4", "read"),
                AsList("katy", "data4", "write"),
                AsList("leyo", "data4", "read"),
                AsList("katy", "data4", "write"),
                AsList("katy", "data4", "write"),
                AsList("ham", "data4", "write")
                );

            _ = e.AddPolicies(rules);
            _ = e.AddPolicies(rules);

            TestGetPolicy(e, AsList(
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write"),
                              AsList("eve", "data3", "read"),
                              AsList("jack", "data4", "read"),
                              AsList("katy", "data4", "write"),
                              AsList("leyo", "data4", "read"),
                              AsList("ham", "data4", "write")
                              )
                          );

            _ = e.RemovePolicies(rules);
            _ = e.RemovePolicies(rules);

            var namedPolicy = AsList("eve", "data3", "read");

            e.RemoveNamedPolicy("p", namedPolicy);
            e.AddNamedPolicy("p", namedPolicy);

            TestGetPolicy(e, AsList(
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write"),
                              AsList("eve", "data3", "read")));

            e.RemoveFilteredPolicy(1, "data2");
            TestGetPolicy(e, AsList(AsList("eve", "data3", "read")));

            e.RemoveFilteredPolicy(1);
            TestGetPolicy(e, AsList(AsList("eve", "data3", "read")));

            e.RemoveFilteredPolicy(1, "");
            TestGetPolicy(e, AsList(AsList("eve", "data3", "read")));
        }
예제 #2
0
        public void TestAdapterAutoSave()
        {
            var adapter  = new EFCoreAdapter <int>(_context);
            var enforcer = new Enforcer(_modelProvideFixture.GetNewRbacModel(), adapter);

            #region Load policy test
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write"),
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 5);
            #endregion

            #region Add policy test
            enforcer.AddPolicy("alice", "data1", "write");
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write"),
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write"),
                              AsList("alice", "data1", "write")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 6);
            #endregion

            #region Remove poliy test
            enforcer.RemovePolicy("alice", "data1", "write");
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write"),
                              AsList("data2_admin", "data2", "read"),
                              AsList("data2_admin", "data2", "write")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 5);

            enforcer.RemoveFilteredPolicy(0, "data2_admin");
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 3);
            #endregion

            #region Batch APIs test
            enforcer.AddPolicies(new []
            {
                new List <string> {
                    "alice", "data2", "write"
                },
                new List <string> {
                    "bob", "data1", "read"
                }
            });
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read"),
                              AsList("bob", "data2", "write"),
                              AsList("alice", "data2", "write"),
                              AsList("bob", "data1", "read")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 5);

            enforcer.RemovePolicies(new []
            {
                new List <string> {
                    "alice", "data1", "read"
                },
                new List <string> {
                    "bob", "data2", "write"
                }
            });
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data2", "write"),
                              AsList("bob", "data1", "read")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 3);
            #endregion

            #region IFilteredAdapter test
            enforcer.LoadFilteredPolicy(new Filter
            {
                P = new List <string> {
                    "bob", "data1", "read"
                },
            });
            TestGetPolicy(enforcer, AsList(
                              AsList("bob", "data1", "read")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 3);

            enforcer.LoadFilteredPolicy(new Filter
            {
                P = new List <string> {
                    "", "", "write"
                },
            });
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data2", "write")
                              ));
            Assert.True(_context.CasbinRule.AsNoTracking().Count() is 3);
            #endregion
        }