1. Go to this page and download the library: Download casbin/hyperf-permission 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/ */
casbin / hyperf-permission example snippets
use Hyperf\Permission\Casbin;
$casbin = new Casbin();
// adds permissions to a user
$casbin->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$casbin->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$casbin->addPolicy('writer', 'articles', 'edit');
// to check if a user has permission
if ($casbin->enforce('eve', 'articles', 'edit')) {
// permit eve to edit articles
} else {
// deny the request, show an error
}
Enforcer::getAllRoles(); // ['writer', 'reader']
Enforcer::getPolicy();
Enforcer::getRolesForUser('eve'); // ['writer']
Enforcer::getUsersForRole('writer'); // ['eve']
Enforcer::hasRoleForUser('eve', 'writer'); // true or false
Enforcer::addRoleForUser('eve', 'writer');
// to user
Enforcer::addPermissionForUser('eve', 'articles', 'read');
// to role
Enforcer::addPermissionForUser('writer', 'articles','edit');
Enforcer::deleteRoleForUser('eve', 'writer');
Enforcer::deleteRolesForUser('eve');
Enforcer::deleteRole('writer');
Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).