ThinkPHP-Casbin:一个专为ThinkPHP5.1定制的Casbin的扩展包

浏览:2406 发布日期:2018/11/13 分类:ThinkPHP5专区 关键字: casbin acl rbac 权限控制 thinkphp
一个专为ThinkPHP5.1定制的Casbin的扩展包( https://github.com/php-casbin/think-casbin )。

安装
在你的thinkphp项目里,通过composer安装这个扩展composer require casbin/think-adapter发布资源:php think casbin:publish这将自动创建model配置文件config/casbin-basic-model.conf,和Casbin的配置文件config/casbin.php

数据迁移:

执行前,请确保数据库连接信息配置正确,如需修改数据库连接信息或表名,可以修改config/casbin.php里的配置php think casbin:migrate这将会自动创建Casbin的策略表casbin_rule

用法use Casbin;

$sub = 'alice'; // the user that wants to access a resource.
$obj = 'data1'; // the resource that is going to be accessed.
$act = 'read'; // the operation that the user performs on the resource.

if (true === Casbin::enforce($sub, $obj, $act)) {
    // permit alice to read data1x
    echo 'permit alice to read data1';
} else {
    // deny the request, show an error
}
自定义配置
config/casbin-basic-model.conf为Casbin的model文件

config/casbin.php为Casbin的adapter、db配置信息

关于Casbin
Casbin官网文档 (https://casbin.org )查看更多用法。
最佳答案
评论( 相关
后面还有条评论,点击查看>>