PHP-Casbin 是一個用 PHP 語言打造的輕量級開源訪問控制框架( https://github.com/php-casbin... ),目前在 GitHub 開源。PHP-Casbin 採用了元模型的設計思想,支持多種經典的訪問控制方案,如基於角色的訪問控制 RBAC、基於屬性的訪問控制 ABAC 等。php
Yii-Casbin 是一個專爲Yii 2.0定製的Casbin
的擴展包( https://github.com/php-casbin... )。git
在Yii 2.0
項目裏,經過composer安裝這個擴展:github
composer require casbin/yii-adapter
使用此擴展時,須要在你的應用程序配置中配置 Casbin
類:app
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的基本訪問:composer
$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 }
casbin
支持多種models規則:框架
Supported models.yii
Casbin官方網站:https://casbin.org網站