PHP code example of iset / tp5-rbac
1. Go to this page and download the library: Download iset/tp5-rbac 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/ */
iset / tp5-rbac example snippets
// +----------------------------------------------------------------------
// | RBAC设置,rbac模块配置依赖
// +----------------------------------------------------------------------
'rbac' => [
// 验证方式 jwt(token方式)形式或者service(基于cookie)方式
'type' => 'jwt',
// rbac要使用的数据库配置为空则为默认库(生成表的前缀依赖此配置)
'db' => [
// 数据库表前缀
'prefix' => 'xt_tp_',
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'iset_demo',
// 用户名
'username' => 'root',
// 密码
'password' => '',
// 端口
'hostport' => '',
],
// 密码加密密钥
'salt_token' => 'abcddfasdfsd',
// 权限缓存前缀
'permission_cache_prefix' => '_RBAC_PERMISSION_CACHE_'
],
$rbac = new Rbac();
//可传入参数$db为数据库配置项默认为空则为默认数据库(考虑到多库的情形)
$rbac->createTable();
$rbac->savePermissionCategory([
'name' => '用户管理组',
'description' => '网站用户的管理',
'status' => 1
]);
$rbac->createPermission([
'name' => '文章列表查询',
'description' => '文章列表查询',
'status' => 1,
'type' => 1,
'category_id' => 1,
'path' => 'article/content/list',
]);
$rbac->createRole([
'name' => '内容管理员',
'description' => '负责网站内容管理',
'status' => 1
], '1,2,3');
$rbac->assignUserRole(1, [1]);
$rbac->getPermissionCategory([['status', '=', 1]]);
$rbac->getPermission([['status', '=', 1]]);
$rbac->getRole([], true);
$rbac->delPermissionCategory([1,2,3,4]);
$rbac->delPermission([1,2,3,4]);
$rbac->delRole([1,2,3,4]);
$rbac->can('article/channel/list');
$rbac->saveUser([]);
$rbac->getUser([]);
$rbac->delUser([]);