PHP code example of casbin / yii-permission
1. Go to this page and download the library: Download casbin/yii-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 / yii-permission example snippets
return [
//....
'components' => [
'permission' => [
'class' => \yii\permission\Permission::class,
/*
* Casbin model setting.
*/
'model' => [
// Available Settings: "file", "text"
'config_type' => 'file',
'config_file_path' => '/path/to/casbin-model.conf',
'config_text' => '',
],
// Casbin adapter .
'adapter' => \yii\permission\Adapter::class,
/*
* Casbin database setting.
*/
'database' => [
// Database connection for following tables.
'connection' => '',
// CasbinRule tables and model.
'casbin_rules_table' => '{{%casbin_rule}}',
],
],
]
];
$permission = \Yii::$app->permission;
// adds permissions to a user
$permission->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$permission->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$permission->addPolicy('writer', 'articles','edit');
// to check if a user has permission
if ($permission->enforce("eve", "articles", "edit")) {
// permit eve to edit articles
} else {
// deny the request, show an error
}
$permission->getAllRoles(); // ['writer', 'reader']
$permission->getPolicy();
$permission->getRolesForUser('eve'); // ['writer']
$permission->getUsersForRole('writer'); // ['eve']
$permission->hasRoleForUser('eve', 'writer'); // true or false
$permission->addRoleForUser('eve', 'writer');
// to user
$permission->addPermissionForUser('eve', 'articles', 'read');
// to role
$permission->addPermissionForUser('writer', 'articles','edit');
$permission->deleteRoleForUser('eve', 'writer');
$permission->deleteRolesForUser('eve');
$permission->deleteRole('writer');
$permission->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
$permission->deletePermissionForUser('eve', 'articles', 'read');
// to user
$permission->deletePermissionsForUser('eve');
// to role
$permission->deletePermissionsForUser('writer');
$permission->getPermissionsForUser('eve'); // return array
$permission->hasPermissionForUser('eve', 'articles', 'read'); // true or false
$config = [
'components' => [
'user' => [
...
'accessChecker' => 'yii\permission\components\PermissionChecker',
]
];
$user->can('acrticles,read');
public function behaviors()
{
return [
'permission' => [
'class' => \yii\permission\components\PermissionControl::class,
'user' => $user, // optional, defaults to \Yii::$app->user
'only' => ['read-articles', 'write-articles'],
'policy' => [
[
'allow' => true,
'actions' => ['read-articles'],
'enforce' => ['articles', 'read']
],
[
'allow' => true,
'actions' => ['write-articles'],
'enforce' => ['articles', 'write']
]
],
'denyCallback' => function ($policy, $action) {
// custom action when access is denied
} // optional, defaults to throwing an exception
]
];
}