casbin/yii-adapter

此包已被废弃,不再维护。没有建议的替代包。

在 Yii2 PHP 框架中使用 Casbin,Casbin 是一个强大且高效的开源访问控制库。

v1.0.0 2019-09-03 06:31 UTC

This package is auto-updated.

Last update: 2024-07-18 18:21:19 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

值得一提的是,我们现在推荐使用 Yii-Permission

在 Yii 2.0 PHP 框架中使用 Casbin

安装

获取 Composer 包

在您的 Yii 2.0 项目的 composer.json 中要求此包。这将下载该包。

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
}

定义您的 model.conf

支持模型.

学习 Casbin

您可以在网站上找到 Casbin 的完整文档 在这里