项目作者: php-casbin

项目描述 :
PHP's client for Casbin-Server. Casbin-Server is the Access Control as a Service (ACaaS) solution based on Casbin.
高级语言: PHP
项目地址: git://github.com/php-casbin/casbin-client.git
创建时间: 2018-11-14T03:08:45Z
项目社区:https://github.com/php-casbin/casbin-client

开源协议:Apache License 2.0

下载


PHP client for Casbin Server

Latest Stable Version
Total Downloads
License
Discord

Casbin-client is PHP’s client for Casbin-Server. Casbin-Server is the Access Control as a Service (ACaaS) solution based on Casbin.

Prerequisites

  • php 5.5 or above, 7.0 or above
  • gRPC PHP extension

This guide gets you started with gRPC in PHP with a simple working example:gRPC in PHP.

Installation

  1. composer require casbin/casbin-client

Examples

Client

  1. require_once './vendor/autoload.php';
  2. use Proto\CasbinClient;
  3. use Proto\NewEnforcerRequest;
  4. use Proto\NewAdapterRequest;
  5. use Proto\EnforceRequest;
  6. $client = new CasbinClient('localhost:50051', [
  7. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  8. ]);

AdapterRequest

  1. $newAdapterRequest = new NewAdapterRequest();
  2. $newAdapterRequest->setDriverName('file');
  3. $newAdapterRequest->setConnectString('path/to/rbac_policy.csv');
  4. list($newAdapterReply, $status) = $client->NewAdapter($newAdapterRequest)->wait();
  5. if (0 !== $status->code) {
  6. throw new \Exception($status->details, $status->code);
  7. }
  8. $adapterHandle = $newAdapterReply->getHandler();

EnforcerRequest

  1. $newEnforcerRequest = new NewEnforcerRequest();
  2. $newEnforcerRequest->setModelText(<<<EOT
  3. [request_definition]
  4. r = sub, obj, act
  5. [policy_definition]
  6. p = sub, obj, act
  7. [role_definition]
  8. g = _, _
  9. [policy_effect]
  10. e = some(where (p.eft == allow))
  11. [matchers]
  12. m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
  13. EOT
  14. );
  15. $newEnforcerRequest->setAdapterHandle($adapterHandle);
  16. list($newEnforcerReply, $status) = $client->NewEnforcer($newEnforcerRequest)->wait();
  17. if (0 !== $status->code) {
  18. throw new \Exception($status->details, $status->code);
  19. }

EnforceRequest

  1. $enforceRequest = new EnforceRequest();
  2. $enforceRequest->setEnforcerHandler($newEnforcerReply->getHandler());
  3. $enforceRequest->setParams(['alice', 'data1', 'read']);
  4. list($enforceReply, $status) = $client->Enforce($enforceRequest)->wait();
  5. if (0 !== $status->code) {
  6. throw new \Exception($status->details, $status->code);
  7. }
  8. if ($enforceReply->getRes()) {
  9. // permit alice to read data1
  10. } else {
  11. // deny the request, show an error
  12. }

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.