示例#1
0
        public void TestRoleApiWithDomains()
        {
            var e = new Enforcer(_testModelFixture.GetNewRbacWithDomainsTestModel());

            e.BuildRoleLinks();

            TestHasRole(e, "alice", "admin", true, "domain1");
            TestHasRole(e, "alice", "admin", false, "domain2");

            TestGetRoles(e, "alice", AsList("admin"), "domain1");
            TestGetRoles(e, "bob", AsList(), "domain1");
            TestGetRoles(e, "admin", AsList(), "domain1");
            TestGetRoles(e, "non_exist", AsList(), "domain1");
            TestGetRoles(e, "alice", AsList(), "domain2");
            TestGetRoles(e, "bob", AsList("admin"), "domain2");
            TestGetRoles(e, "admin", AsList(), "domain2");
            TestGetRoles(e, "non_exist", AsList(), "domain2");

            _ = e.DeleteRoleForUser("alice", "admin", "domain1");

            _ = e.AddRoleForUser("bob", "admin", "domain1");

            TestGetRoles(e, "alice", AsList(), "domain1");
            TestGetRoles(e, "bob", AsList("admin"), "domain1");
            TestGetRoles(e, "admin", AsList(), "domain1");
            TestGetRoles(e, "non_exist", AsList(), "domain1");
            TestGetRoles(e, "alice", AsList(), "domain2");
            TestGetRoles(e, "bob", AsList("admin"), "domain2");
            TestGetRoles(e, "admin", AsList(), "domain2");
            TestGetRoles(e, "non_exist", AsList(), "domain2");

            _ = e.AddRoleForUser("alice", "admin", "domain1");

            _ = e.DeleteRolesForUser("bob", "domain1");

            TestGetRoles(e, "alice", AsList("admin"), "domain1");
            TestGetRoles(e, "bob", AsList(), "domain1");
            TestGetRoles(e, "admin", AsList(), "domain1");
            TestGetRoles(e, "non_exist", AsList(), "domain1");
            TestGetRoles(e, "alice", AsList(), "domain2");
            TestGetRoles(e, "bob", AsList("admin"), "domain2");
            TestGetRoles(e, "admin", AsList(), "domain2");
            TestGetRoles(e, "non_exist", AsList(), "domain2");

            _ = e.AddRolesForUser("bob", AsList("admin", "admin1", "admin2"), "domain1");

            TestGetRoles(e, "bob", AsList("admin", "admin1", "admin2"), "domain1");

            TestGetPermissions(e, "admin", AsList(
                                   AsList("admin", "domain1", "data1", "read"),
                                   AsList("admin", "domain1", "data1", "write")),
                               "domain1");
            TestGetPermissions(e, "admin", AsList(
                                   AsList("admin", "domain2", "data2", "read"),
                                   AsList("admin", "domain2", "data2", "write")),
                               "domain2");
        }
示例#2
0
        public void TestRoleApi()
        {
            var e = new Enforcer(_testModelFixture.GetNewRbacTestModel());

            e.BuildRoleLinks();

            TestGetRoles(e, "alice", AsList("data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());
            TestGetRoles(e, "non_exist", AsList());

            TestHasRole(e, "alice", "data1_admin", false);
            TestHasRole(e, "alice", "data2_admin", true);

            e.AddRoleForUser("alice", "data1_admin");

            TestGetRoles(e, "alice", AsList("data1_admin", "data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.DeleteRoleForUser("alice", "data1_admin");

            TestGetRoles(e, "alice", AsList("data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.DeleteRolesForUser("alice");

            TestGetRoles(e, "alice", AsList());
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.AddRoleForUser("alice", "data1_admin");
            e.DeleteUser("alice");

            TestGetRoles(e, "alice", AsList());
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.AddRoleForUser("alice", "data2_admin");

            TestEnforce(e, "alice", "data1", "read", true);
            TestEnforce(e, "alice", "data1", "write", false);

            TestEnforce(e, "alice", "data2", "read", true);
            TestEnforce(e, "alice", "data2", "write", true);

            TestEnforce(e, "bob", "data1", "read", false);
            TestEnforce(e, "bob", "data1", "write", false);
            TestEnforce(e, "bob", "data2", "read", false);
            TestEnforce(e, "bob", "data2", "write", true);

            e.DeleteRole("data2_admin");

            TestEnforce(e, "alice", "data1", "read", true);
            TestEnforce(e, "alice", "data1", "write", false);
            TestEnforce(e, "alice", "data2", "read", false);
            TestEnforce(e, "alice", "data2", "write", false);
            TestEnforce(e, "bob", "data1", "read", false);
            TestEnforce(e, "bob", "data1", "write", false);
            TestEnforce(e, "bob", "data2", "read", false);
            TestEnforce(e, "bob", "data2", "write", true);
        }
示例#3
0
        public void Test_RoleAPI()
        {
            Enforcer e = new Enforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");

            TestGetRoles(e, "alice", AsList("data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());
            TestGetRoles(e, "non_exist", AsList());

            TestHasRole(e, "alice", "data1_admin", false);
            TestHasRole(e, "alice", "data2_admin", true);

            e.AddRoleForUser("alice", "data1_admin");

            TestGetRoles(e, "alice", AsList("data1_admin", "data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.DeleteRoleForUser("alice", "data1_admin");

            TestGetRoles(e, "alice", AsList("data2_admin"));
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.DeleteRolesForUser("alice");

            TestGetRoles(e, "alice", AsList());
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.AddRoleForUser("alice", "data1_admin");
            e.DeleteUser("alice");

            TestGetRoles(e, "alice", AsList());
            TestGetRoles(e, "bob", AsList());
            TestGetRoles(e, "data2_admin", AsList());

            e.AddRoleForUser("alice", "data2_admin");

            TestEnforce(e, "alice", "data1", "read", true);
            TestEnforce(e, "alice", "data1", "write", false);

            TestEnforce(e, "alice", "data2", "read", true);
            TestEnforce(e, "alice", "data2", "write", true);

            TestEnforce(e, "bob", "data1", "read", false);
            TestEnforce(e, "bob", "data1", "write", false);
            TestEnforce(e, "bob", "data2", "read", false);
            TestEnforce(e, "bob", "data2", "write", true);

            e.DeleteRole("data2_admin");

            TestEnforce(e, "alice", "data1", "read", true);
            TestEnforce(e, "alice", "data1", "write", false);
            TestEnforce(e, "alice", "data2", "read", false);
            TestEnforce(e, "alice", "data2", "write", false);
            TestEnforce(e, "bob", "data1", "read", false);
            TestEnforce(e, "bob", "data1", "write", false);
            TestEnforce(e, "bob", "data2", "read", false);
            TestEnforce(e, "bob", "data2", "write", true);
        }