示例#1
0
        public async Task TestPermissionApiAsync()
        {
            var e = new Enforcer(_testModelFixture.GetBasicWithoutResourceTestModel());

            e.BuildRoleLinks();

            await TestEnforceWithoutUsersAsync(e, "alice", "read", true);
            await TestEnforceWithoutUsersAsync(e, "alice", "write", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "read", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "write", true);

            TestGetPermissions(e, "alice", AsList(AsList("alice", "read")));
            TestGetPermissions(e, "bob", AsList(AsList("bob", "write")));

            TestHasPermission(e, "alice", AsList("read"), true);
            TestHasPermission(e, "alice", AsList("write"), false);
            TestHasPermission(e, "bob", AsList("read"), false);
            TestHasPermission(e, "bob", AsList("write"), true);

            _ = await e.DeletePermissionAsync("read");

            await TestEnforceWithoutUsersAsync(e, "alice", "read", false);
            await TestEnforceWithoutUsersAsync(e, "alice", "write", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "read", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "write", true);

            _ = await e.AddPermissionForUserAsync("bob", "read");

            await TestEnforceWithoutUsersAsync(e, "alice", "read", false);
            await TestEnforceWithoutUsersAsync(e, "alice", "write", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "read", true);
            await TestEnforceWithoutUsersAsync(e, "bob", "write", true);

            _ = await e.DeletePermissionForUserAsync("bob", "read");

            await TestEnforceWithoutUsersAsync(e, "alice", "read", false);
            await TestEnforceWithoutUsersAsync(e, "alice", "write", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "read", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "write", true);

            _ = await e.DeletePermissionsForUserAsync("bob");

            await TestEnforceWithoutUsersAsync(e, "alice", "read", false);
            await TestEnforceWithoutUsersAsync(e, "alice", "write", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "read", false);
            await TestEnforceWithoutUsersAsync(e, "bob", "write", false);
        }