Esempio n. 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");
        }
Esempio n. 2
0
        public async Task TestRbacModelInMemoryAsync()
        {
            var m = Model.Model.Create();

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

            var e = new Enforcer(m);

            await e.AddPermissionForUserAsync("alice", "data1", "read");

            await e.AddPermissionForUserAsync("bob", "data2", "write");

            await e.AddPermissionForUserAsync("data2_admin", "data2", "read");

            await e.AddPermissionForUserAsync("data2_admin", "data2", "write");

            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);
        }
Esempio n. 3
0
        public async Task TestRbacModelInMemory2Async()
        {
            string text =
                "[request_definition]\n"
                + "r = sub, obj, act\n"
                + "\n"
                + "[policy_definition]\n"
                + "p = sub, obj, act\n"
                + "\n"
                + "[role_definition]\n"
                + "g = _, _\n"
                + "\n"
                + "[policy_effect]\n"
                + "e = some(where (p.eft == allow))\n"
                + "\n"
                + "[matchers]\n"
                + "m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act\n";

            var m = Model.Model.CreateFromText(text);

            var e = new Enforcer(m);

            await e.AddPermissionForUserAsync("alice", "data1", "read");

            await e.AddPermissionForUserAsync("bob", "data2", "write");

            await e.AddPermissionForUserAsync("data2_admin", "data2", "read");

            await e.AddPermissionForUserAsync("data2_admin", "data2", "write");

            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);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
 public Task AssignRoleToUser(Guid userId, Guid familyId, string role)
 {
     return(_enforcer.AddRoleForUserAsync(userId.ToString(), role, familyId.ToString()));
 }