casdoor / casdoor-dotnet-sdk

.NET client SDK for Casdoor
https://github.com/casdoor/casdoor
Apache License 2.0
28 stars 25 forks source link

fix: enforce methods #74

Closed d1mak3 closed 1 year ago

d1mak3 commented 1 year ago

Test code:

using Casdoor.Client;
using ConsoleApp;

var httpClient = new HttpClient();
var options = new CasdoorOptions
{
    // Require: Basic options
    Endpoint = "http://localhost:8000",
    OrganizationName = "built-in",
    ApplicationName = "app-built-in",
    ApplicationType = "native", // webapp, webapi or native
    ClientId = "3edff1676210cca28d24",
    ClientSecret = "9f918ff437282d2eebf94777208e61227d39edd8",

    // Optional: The callback path that the client will be redirected to
    // after the user has authenticated. default is "/casdoor/signin-callback"
    CallbackPath = "/callback",
    // Optional: Whether require https for casdoor endpoint
    RequireHttpsMetadata = false,
    // Optional: The scopes that the client is requesting.
    Scope = "openid profile email"

    // More options can be found at README.md
    // https://github.com/casdoor/casdoor-dotnet-sdk/blob/master/README.md
};

var client = new CasdoorClient(httpClient, options);

var token = await client.RequestPasswordTokenAsync("admin", "123");
client = client.SetBearerToken(token.AccessToken);

Console.WriteLine("Enforce by enforcerId:");
ConsoleExtension.JsonWriteLine(await client.EnforceAsync(
    new[] { "built-in", "built-in/admin", "POST", "/api/signup", "built-in", "built-in/app-built-in" },
    null, null, null, enforcerId: "built-in/api-enforcer-built-in", CancellationToken.None));

Console.WriteLine("Enforce by modelId:");
ConsoleExtension.JsonWriteLine(await client.EnforceAsync(new[] { "built-in/admin", "app-built-in", "read" },
    null, modelId: "built-in/user-model-built-in", null, null, CancellationToken.None));

Console.WriteLine("Enforce by permissionId:");
ConsoleExtension.JsonWriteLine(await client.EnforceAsync(new[] { "built-in/admin", "app-built-in", "read" },
    permissionId: "built-in/permission-built-in", null, null, null, CancellationToken.None));

Console.WriteLine("Enforce by resourceId");
ConsoleExtension.JsonWriteLine(await client.EnforceAsync(
    new[] { "built-in/admin", "app-built-in", "read" },
    null, null, resourceId: "built-in", null, CancellationToken.None));

Console.WriteLine("Batch enforce by enforcerId:");
ConsoleExtension.JsonWriteLine(await client.BatchEnforceAsync(
    new[]
    {
        new[] { "built-in", "built-in/admin", "POST", "/api/signup", "built-in", "built-in/app-built-in" },
        new[] { "built-in", "built-in/admin", "POST", "/api/signup", "built-in", "built-in/app-built-in" }
    },
    null, null, enforcerId: "built-in/api-enforcer-built-in", CancellationToken.None));

Console.WriteLine("Batch enforce by modelId:");
ConsoleExtension.JsonWriteLine(await client.BatchEnforceAsync(new[]
    {
        new[] { "built-in/admin", "app-built-in", "read" },
        new[] { "built-in/admin", "app-built-in", "read" }
    },
    null, modelId: "built-in/user-model-built-in", null, CancellationToken.None));

Console.WriteLine("Batch enforce by permissionId:");
ConsoleExtension.JsonWriteLine(await client.BatchEnforceAsync(new[]
    {
        new[] { "built-in/admin", "app-built-in", "read" },
        new[] { "built-in/admin", "app-built-in", "read" }
    },permissionId: "built-in/permission-built-in", null, null, CancellationToken.None));

Output:

Enforce by enforcerId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": true,
  "data2": null
}
Enforce by modelId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    true
  ],
  "data2": null
}
Enforce by permissionId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    true
  ],
  "data2": null
}
Enforce by resourceId
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    true,
    true
  ],
  "data2": null
}
Batch enforce by enforcerId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    true,
    true
  ],
  "data2": null
}
Batch enforce by modelId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    [
      true,
      true
    ]
  ],
  "data2": null
}
Batch enforce by permissionId:
{
  "status": "ok",
  "msg": "",
  "sub": "",
  "name": "",
  "data": [
    [
      true,
      true
    ]
  ],
  "data2": null
}
casbin-bot commented 1 year ago

@seriouszyx @ComradeProgrammer @Resulte please review

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 1.8.1 :tada:

The release is available on GitHub release

Your semantic-release bot :package::rocket: