public void TestTypConvertorAttribute()
        {
            var adapter  = new EFCoreAdapter <Guid>(_context);
            var enforcer = new Enforcer(_modelProvideFixture.GetNewAbacModel(), adapter);

            enforcer.LoadPolicy();

            var flag = enforcer.Enforce(new
            {
                IsTenantMember = "true",
                Age            = 30
            }, "/data2", "write");

            Assert.True(flag);
        }
Example #2
0
        public void TestExtraFilter()
        {
            var adapter  = new EFCoreAdapter <Guid>(_context);
            var enforcer = new Enforcer(_modelProvideFixture.GetNewRbacModel(), adapter);

            enforcer.LoadFilteredPolicy(new Filter
            {
                P = new List <string> {
                    "", "", "", "formlang1"
                },
            });
            TestGetPolicy(enforcer, AsList(
                              AsList("alice", "data1", "read", "formlang1")
                              ));
            var left = _context.CasbinRule.AsNoTracking().Count();

            Assert.True(5 == left);
        }
        public void TestAgeAttribute()
        {
            var adapter  = new EFCoreAdapter <Guid>(_context);
            var enforcer = new Enforcer(_modelProvideFixture.GetNewAbacModel(), adapter);

            enforcer.LoadFilteredPolicy(new Filter
            {
                P = new List <string> {
                    "", "", "", "formlang"
                },
            });

            var flag = enforcer.Enforce(new
            {
                Age            = 30,
                IsTenantMember = "false",
            }, "/data1", "read");

            Assert.True(flag);
        }
Example #4
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
        }