diff --git a/docs/modules/ROOT/examples/quickstart/example.cs b/docs/modules/ROOT/examples/quickstart/example.cs index 47d9662b6..59c0bce62 100644 --- a/docs/modules/ROOT/examples/quickstart/example.cs +++ b/docs/modules/ROOT/examples/quickstart/example.cs @@ -1,40 +1,49 @@ -using Cerbos.Sdk.Builders; -using Cerbos.Sdk; +using Cerbos.Api.V1.Effect; +using Cerbos.Sdk.Response; +using Cerbos.Sdk.Builder; +using Cerbos.Sdk.Utility; internal class Program { private static void Main(string[] args) { - var client = new CerbosClientBuilder("http://localhost:3593").WithPlaintext().BuildBlockingClient(); - string[] actions = { "view:public", "comment" }; - - CheckResourcesResult result = client - .CheckResources( - Principal.NewInstance("bugs_bunny", "user") - .WithAttribute("beta_tester", AttributeValue.BoolValue(true)), - - ResourceAction.NewInstance("album:object", "BUGS001") + var client = CerbosClientBuilder.ForTarget("http://localhost:3593").WithPlaintext().Build(); + var request = CheckResourcesRequest + .NewInstance() + .WithRequestId(RequestId.Generate()) + .WithIncludeMeta(true) + .WithPrincipal( + Principal + .NewInstance("bugs_bunny", "user") + .WithAttribute("beta_tester", AttributeValue.BoolValue(true)) + ) + .WithResourceEntries( + ResourceEntry + .NewInstance("album:object", "BUGS001") .WithAttribute("owner", AttributeValue.StringValue("bugs_bunny")) .WithAttribute("public", AttributeValue.BoolValue(false)) .WithAttribute("flagged", AttributeValue.BoolValue(false)) - .WithActions(actions), + .WithActions("comment", "view:public"), - ResourceAction.NewInstance("album:object", "DAFFY002") + ResourceEntry + .NewInstance("album:object", "DAFFY002") + .WithPolicyVersion("20210210") .WithAttribute("owner", AttributeValue.StringValue("daffy_duck")) .WithAttribute("public", AttributeValue.BoolValue(true)) .WithAttribute("flagged", AttributeValue.BoolValue(false)) - .WithActions(actions) + .WithActions("comment", "view:public") ); - foreach (string n in new string[] { "BUGS001", "DAFFY002" }) + CheckResourcesResponse result = client.CheckResources(request); + foreach (var resourceId in new[] { "BUGS001", "DAFFY002" }) { - var r = result.Find(n); - Console.Write(String.Format("\nResource: {0}\n", n)); - foreach (var i in r.GetAll()) + var resultEntry = result.Find(resourceId); + Console.Write($"\nResource ID: {resourceId}\n"); + foreach (var actionEffect in resultEntry.Actions) { - String action = i.Key; - Boolean isAllowed = i.Value; - Console.Write(String.Format("\t{0} -> {1}\n", action, isAllowed ? "EFFECT_ALLOW" : "EFFECT_DENY")); + string action = actionEffect.Key; + Effect effect = actionEffect.Value; + Console.Write($"\t{action} -> {(effect == Effect.Allow ? "EFFECT_ALLOW" : "EFFECT_DENY")}\n"); } } } diff --git a/docs/modules/ROOT/examples/quickstart/example.php b/docs/modules/ROOT/examples/quickstart/example.php index d849a48a7..f18ef327b 100644 --- a/docs/modules/ROOT/examples/quickstart/example.php +++ b/docs/modules/ROOT/examples/quickstart/example.php @@ -2,34 +2,47 @@ require __DIR__ . '/vendor/autoload.php'; +use Cerbos\Effect\V1\Effect; +use Cerbos\Sdk\Builder\AttributeValue; use Cerbos\Sdk\Builder\CerbosClientBuilder; +use Cerbos\Sdk\Builder\CheckResourcesRequest; use Cerbos\Sdk\Builder\Principal; -use Cerbos\Sdk\Builder\ResourceAction; -use Symfony\Component\HttpClient\HttplugClient; - -$clientBuilder = new CerbosClientBuilder("http://localhost:3592", new HttplugClient(), null, null, null); -$client = $clientBuilder->build(); - -$principal = Principal::newInstance("bugs_bunny") - ->withRole("user") - ->withAttribute("beta_tester", true); - -$resourceAction1 = ResourceAction::newInstance("album:object", "BUGS001") - ->withAction("view:public") - ->withAction("comment") - ->withAttribute("owner", "bugs_bunny") - ->withAttribute("public", false) - ->withAttribute("flagged", false); - -$resourceAction2 = ResourceAction::newInstance("album:object", "DAFFY002") - ->withAction("view:public") - ->withAction("comment") - ->withAttribute("owner", "daffy_duck") - ->withAttribute("public", true) - ->withAttribute("flagged", false); - -$checkResourcesResult = $client->checkResources($principal, array($resourceAction1, $resourceAction2), null, null); - -echo json_encode($checkResourcesResult, JSON_PRETTY_PRINT); - +use Cerbos\Sdk\Builder\ResourceEntry; +use Cerbos\Sdk\Utility\RequestId; + +$client = CerbosClientBuilder::newInstance("localhost:3593") + ->withPlaintext(true) + ->build(); + +$request = CheckResourcesRequest::newInstance() + ->withRequestId(RequestId::generate()) + ->withPrincipal( + Principal::newInstance("bugs_bunny") + ->withRole("user") + ->withAttribute("beta_tester", AttributeValue::boolValue(true)) + ) + ->withResourceEntries( + [ + ResourceEntry::newInstance("album:object", "BUGS001") + ->withAttribute("owner", AttributeValue::stringValue("bugs_bunny")) + ->withAttribute("public", AttributeValue::boolValue(false)) + ->withAttribute("flagged", AttributeValue::boolValue(false)) + ->withActions(["comment", "view:public"]), + + ResourceEntry::newInstance("album:object", "DAFFY002") + ->withAttribute("owner", AttributeValue::stringValue("daffy_duck")) + ->withAttribute("public", AttributeValue::boolValue(true)) + ->withAttribute("flagged", AttributeValue::boolValue(false)) + ->withActions(["comment", "view:public"]) + ] + ); + +$checkResourcesResponse = $client->checkResources($request); +foreach (["BUGS001", "DAFFY002"] as $resourceId) { + $resultEntry = $checkResourcesResponse->find($resourceId); + $actions = $resultEntry->getActions(); + foreach ($actions as $k => $v) { + printf("%s -> %s", $k, Effect::name($v)); + } +} ?>