Yii-Casbin:在 Yii 裡使用 Casbin,支援 ACL、RBAC多種模型的許可權管理框架
阿新 • • 發佈:2019-02-16
PHP-Casbin 是一個用 PHP 語言打造的輕量級開源訪問控制框架( https://github.com/php-casbin... ),目前在 GitHub 開源。PHP-Casbin 採用了元模型的設計思想,支援多種經典的訪問控制方案,如基於角色的訪問控制 RBAC、基於屬性的訪問控制 ABAC 等。
安裝
通過composer安裝
在Yii 2.0
專案裡,通過composer安裝這個擴充套件:
composer require casbin/yii-adapter
配置
使用此擴充套件時,需要在你的應用程式配置中配置 Casbin
類:
return [ //.... 'components' => [ 'casbin' => [ 'class' => '\CasbinAdapter\Yii\Casbin', /* * Yii-casbin model setting. */ 'model' => [ // Available Settings: "file", "text" 'config_type' => 'file', 'config_file_path' => '/path/to/casbin-model.conf', 'config_text' => '', ], // Yii-casbin adapter . 'adapter' => '\CasbinAdapter\Yii\Adapter', /* * Yii-casbin database setting. */ 'database' => [ // Database connection for following tables. 'connection' => '', // CasbinRule tables and model. 'casbin_rules_table' => '{{%casbin_rule}}', ], ], ] ];
用法
通過casbin
元件對Casbin的基本訪問:
$casbin = \Yii::$app->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 } else { // deny the request, show an error }
自定義models規則
casbin
支援多種models規則: