Think-Casbin:一个专为ThinkPHP打造的轻量级访问控制库,支持RBAC/ACL等权限控制

PHP-Casbin 是一个用 PHP 语言打造的轻量级开源访问控制框架( https://github.com/php-casbin... ),目前在 GitHub 开源。PHP-Casbin 采用了元模型的设计思想,支持多种经典的访问控制方案,如基于角色的访问控制 RBAC、基于属性的访问控制 ABAC 等。php

Think-Casbin一个专为ThinkPHP5.1定制的Casbin的扩展包( https://github.com/php-casbin... )。git

安装

在你的thinkphp项目里,经过composer安装这个扩展github

composer require casbin/think-adapter

发布资源:thinkphp

php think casbin:publish

这将自动建立model配置文件config/casbin-basic-model.conf,和Casbin的配置文件config/casbin.php数据库

数据迁移:composer

执行前,请确保数据库链接信息配置正确,如需修改数据库链接信息或表名,能够修改config/casbin.php里的配置框架

php think casbin:migrate

这将会自动建立Casbin的策略表casbin_ruleui

用法

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 data1
    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配置信息code

关于Casbin

Casbin官网文档 (https://casbin.org )查看更多用法。

相关文章
相关标签/搜索