PHP code example of gmars / tp5-rbac

1. Go to this page and download the library: Download gmars/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/ */

    

gmars / tp5-rbac example snippets


'rbac' => [
    'type' => 'jwt',    //验证方式 jwt(token方式)形式或者service(基于cookie)方式
    'db' => '',        //rbac要使用的数据库配置为空则为默认库(生成表的前缀依赖此配置)
    'salt_token' => 'asdfasfdafasf',    //token加密密钥
    'token_key' => 'Authorization'      //header中用于验证token的名称
]

$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->cachePermission(1);

$rbac->can('article/channel/list');

$rbac->generateToken(1);

array(3) {
  ["token"] => string(32) "4c56b80f06d3d8810b97db33a1291694"
  ["refresh_token"] => string(32) "17914241bde6bfc46b20e643b2c58279"
  ["expire"] => int(7200)
}

$rbac->refreshToken('17914241bde6bfc46b20e643b2c58279');

$rbac->can('article/channel/list');

'migration' => [
    'path' => ROOT_PATH .'vendor/gmars/tp5-rbac/'
],

php think migrate:run

user              用户表
user_role         用户角色对应表
role              角色表
role_permission   角色权限对应表
permission        角色表

$rbacObj = new Rbac();
$data = ['user_name' => 'zhangsan', 'status' => 1, 'password' => md5('zhangsan')];
$rbacObj->createUser($data);

$rbacObj = new Rbac();
$data = [
    'name' => '商品列表',
    'status' => 1,
    'description' => '查看商品的所有列表',
    'path' => '/index/goods/list',
    'create_time' => time()
];
$rbacObj->createPermission($data);

$rbacObj = new Rbac();
$data = [
    'name' => '商品管理员',
    'status' => 1,
    'description' => '商品管理员负责商品的查看修改删除等操作',
    'sort_num' => 10,
    'parent_id' => 1
];
$rbacObj->createRole($data);

$rbacObj = new Rbac();
$rbacObj->assignUserRole(1, [1, 2]);

$rbacObj = new Rbac();
$rbacObj->assignRolePermission(1, [1, 2]);

$rbacObj = new Rbac();
$rbacObj->delRole(1);

$rbacObj = new Rbac();
$rbacObj->moveRole(1,3);

$rbacObj = new Rbac();
$rbacObj->cachePermission(1);

$rbacObj = new Rbac();
$rbacObj->can('/index/goods/list');