PHP code example of casbin / codeigniter-permission
1. Go to this page and download the library: Download casbin/codeigniter-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 / codeigniter-permission example snippets
$enforcer = \Config\Services::enforcer();
// adds permissions to a user
$enforcer->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$enforcer->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$enforcer->addPolicy('writer', 'articles','edit');
// to check if a user has permission
if ($enforcer->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).