예제 #1
0
        public async Task TestRoleApiWithDomainsAsync()
        {
            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");

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

            _ = await e.AddRoleForUserAsync("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");

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

            _ = await e.DeleteRolesForUserAsync("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");

            _ = await e.AddRolesForUserAsync("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 async Task TestRoleApiAsync()
        {
            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);

            await e.AddRoleForUserAsync("alice", "data1_admin");

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

            await e.DeleteRoleForUserAsync("alice", "data1_admin");

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

            await e.DeleteRolesForUserAsync("alice");

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

            await e.AddRoleForUserAsync("alice", "data1_admin");

            await e.DeleteUserAsync("alice");

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

            await e.AddRoleForUserAsync("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);

            await e.DeleteRoleAsync("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);
        }