Casbin is used to build Casbin-cpp into a PHP dynamic library via PHP-CPP.
This application needs to be compiled, and also relies on the PHP-CPP library, so you will need requirements:
- PHP-CPP
- PHP >= 7.0
- GCC
- make
$ git clone https://github.com/php-casbin/casbin.git
$ cd casbin
$ make
$ make library
$ sudo make install
To get rid of intermediate files generated during building of library:
$ make clean
New a Casbin enforcer with a model file and a policy file:
use Casbin\Enforcer;
$e = new Enforcer("path/to/model.conf", "path/to/policy.csv");
Add an enforcement hook into your code right before the access happens:
$params = [
"alice", // the user that wants to access a resource.
"data1", // the resource that is going to be accessed.
"read" // the operation that the user performs on the resource.
]
if ($e->enforce($params) === true) {
// permit alice to read data1
} else {
// deny the request, show an error
}
It provides a very rich api to facilitate various operations on the Policy:
Gets all roles:
$e->getAllRoles();
Gets all the authorization rules in the policy.:
$e->getPolicy();
Gets the roles that a user has.
$e->getRolesForUser('eve');
Gets the users that has a role.
$e->getUsersForRole('writer');
Determines whether a user has a role.
$e->hasRoleForUser('eve', 'writer');
Adds a role for a user.
$e->addRoleForUser('eve', 'writer');
Adds a permission for a user or role.
// to user
$e->addPermissionForUser('eve', ['articles', 'read']);
// to role
$e->addPermissionForUser('writer', ['articles','edit']);
Deletes a role for a user.
$e->deleteRoleForUser('eve', 'writer');
Deletes all roles for a user.
$e->deleteRolesForUser('eve');
Deletes a role.
$e->deleteRole('writer');
Deletes a permission.
$e->deletePermission(['articles', 'read']);
Deletes a permission for a user or role.
$e->deletePermissionForUser('eve', ['articles', 'read']);
Deletes permissions for a user or role.
// to user
$e->deletePermissionsForUser('eve');
// to role
$e->deletePermissionsForUser('writer');
Gets permissions for a user or role.
$e->getPermissionsForUser('eve');
Determines whether a user has a permission.
$e->hasPermissionForUser('eve', ['articles', 'read']);
See Casbin API for more APIs.
Casbin in Laravel. You can find the full documentation of Casbin on the website.
This project is licensed under the Apache 2.0 license.