PHP code example of cerbos / cerbos-sdk-php
1. Go to this page and download the library: Download cerbos/cerbos-sdk-php library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
cerbos / cerbos-sdk-php example snippets
$client = CerbosClientBuilder::newInstance($this->host)
->withPlaintext(true)
->build();
$request = CheckResourcesRequest::newInstance()
->withRequestId(RequestId::generate())
->withPrincipal(
Principal::newInstance("john")
->withRole("employee")
->withPolicyVersion("20210210")
->withAttribute("department", AttributeValue::stringValue("marketing"))
->withAttribute("geography", AttributeValue::stringValue("GB"))
)
->withResourceEntry(
ResourceEntry::newInstance("leave_request", "xx125")
->withActions(["view:public", "approve"])
->withPolicyVersion("20210210")
->withAttribute("department", AttributeValue::stringValue("marketing"))
->withAttribute("geography", AttributeValue::stringValue("GB"))
->withAttribute("owner", AttributeValue::stringValue("john"))
)
$checkResourcesResponse = $client->checkResources($request);
$resultEntry = $checkResourcesResponse->find("xx125");
if ($resultEntry->isAllowed("view:public")) { // returns true if `view:public` action is allowed
// ...
}
if ($resultEntry->isAllowed("approve")) { // returns true if `approve` action is allowed
// ...
}
$request = CheckResourcesRequest::newInstance()
->withRequestId(RequestId::generate())
->withPrincipal(
Principal::newInstance("john")
->withRole("employee")
->withPolicyVersion("20210210")
->withAttribute("department", "marketing")
->withAttribute("geography", "GB")
)
->withResourceEntries(
array(
ResourceEntry::newInstance("leave_request", "xx125")
->withAction("approve")
->withPolicyVersion("20210210")
->withAttribute("department", AttributeValue::stringValue("marketing"))
->withAttribute("geography", AttributeValue::stringValue("GB"))
->withAttribute("owner", AttributeValue::stringValue("john")),
ResourceEntry::newInstance("leave_request", "xx225")
->withAction("defer")
->withPolicyVersion("20210210")
->withAttribute("department", AttributeValue::stringValue("marketing"))
->withAttribute("owner", AttributeValue::stringValue("john"))
)
)
$checkResourcesResponse = $client->checkResources($request);
$resultEntry = $checkResourcesResponse->find("xx125");
if ($resultEntry->isAllowed("approve")) { // returns true if `approve` action is allowed
// ...
}
$resultEntry = $checkResourcesResponse->find("xx225");
if ($resultEntry->isAllowed("defer")) { // returns true if `defer` action is allowed
// ...
}
$request = PlanResourcesRequest::newInstance()
->withRequestId(RequestId::generate())
->withAction("approve")
->withPrincipal(
Principal::newInstance("maggie")
->withRole("manager")
->withAttribute("department", AttributeValue::stringValue("marketing"))
->withAttribute("geography", AttributeValue::stringValue("GB"))
->withAttribute("team", AttributeValue::stringValue("design"))
)
->withResource(
Resource::newInstance("leave_request", "xx125")
->withPolicyVersion("20210210")
);
$planResourcesResponse = $this->client->planResources($request);
if ($planResourcesResponse->isAlwaysAllowed()) {
// ...
}
else if ($planResourcesResponse->isAlwaysDenied()) {
// ...
}
else {
// ...
}
$client = CerbosClientBuilder::newInstance("localhost:3593")
->withPlaintext(true)
->build();
$val = AttributeValue::boolValue(true);
$val = AttributeValue::stringValue("marketing");
$request = CheckResourcesRequest::newInstance()
->withRequestId(RequestId::generate())
->withPrincipal(
Principal::newInstance("john")
->withRole("employee")
->withPolicyVersion("20210210")
->withAttribute("department", "marketing")
)
->withResourceEntries(
array(
ResourceEntry::newInstance("leave_request", "xx125")
->withAction("approve")
->withAttribute("department", AttributeValue::stringValue("marketing")),
ResourceEntry::newInstance("leave_request", "xx225")
->withAction("defer")
->withAttribute("department", AttributeValue::stringValue("marketing"))
)
);
$request = PlanResourcesRequest::newInstance()
->withRequestId(RequestId::generate())
->withAction("approve")
->withPrincipal(
Principal::newInstance("maggie")
->withRole("manager")
->withAttribute("department", AttributeValue::stringValue("marketing"))
)
->withResource(
Resource::newInstance("leave_request", "xx125")
->withAttribute("department", AttributeValue::stringValue("marketing"))
);
bash
composer