Skip to content

Commit

Permalink
docs(sdk): Update SDK examples (#1731)
Browse files Browse the repository at this point in the history
* Update .NET SDK example in quickstart

Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>

* Update PHP SDK example in quickstart

Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>

* Update variable names for .NET SDK

Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>

---------

Signed-off-by: Oğuzhan Durgun <oguzhandurgun95@gmail.com>
  • Loading branch information
oguzhand95 committed Aug 7, 2023
1 parent 3d8dd13 commit c7c5f37
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 49 deletions.
51 changes: 30 additions & 21 deletions 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");
}
}
}
Expand Down
69 changes: 41 additions & 28 deletions docs/modules/ROOT/examples/quickstart/example.php
Expand Up @@ -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));
}
}
?>

0 comments on commit c7c5f37

Please sign in to comment.