PHP-Casbin 是一個用 PHP 語言打造的輕量級開源訪問控制框架( https://github.com/php-casbin... ),目前在 GitHub 開源。PHP-Casbin 採用了元模型的設計思想,支持多種經典的訪問控制方案,如基於角色的訪問控制 RBAC、基於屬性的訪問控制 ABAC 等。
Think-Casbin一個專爲ThinkPHP5.1定製的Casbin的擴展包( https://github.com/php-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 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配置信息
關於Casbin
Casbin官網文檔 (https://casbin.org )查看更多用法。