casbin/cake-permission

在 CakePHP 中使用 casbin。

v2.0.0 2023-09-11 08:48 UTC

This package is auto-updated.

Last update: 2024-09-11 10:53:26 UTC


README

Test Coverage Status Latest Stable Version Total Downloads License

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

安装

在您的 CakePHP 项目的 composer.json 文件中要求此包。这将下载该包。

composer require casbin/cake-permission

为 Casbin 创建配置文件 config/casbin.php

<?php

return [
    'Casbin' => [
         /*
         * Cake-casbin model setting.
         */
        'model' => [
            // Available Settings: "file", "text"
            'config_type' => 'file',
            'config_file_path' => __DIR__.'/casbin-model.conf',
            'config_text' => '',
        ],

        // Cake-casbin adapter .
        'adapter' => '\Cake\Permission\Adapter',

        /*
         * Cake-casbin database setting.
         */
        'database' => [
            // Database connection for following tables.
            'connection' => '',
            // CasbinRule tables and model.
            'casbin_rules_table' => '',
        ],
    ],
];

创建一个名为 config/casbin-model.conf 的新模型配置文件。

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj, act

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act

用法

$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.

$casbin = new \Cake\Permission\Casbin();

if (true === $casbin->enforce($sub, $obj, $act)) {
    // permit alice to read data1
} else {
    // deny the request, show an error
}

定义自己的 model.conf

您可以修改名为 config/casbin-model.conf 的配置文件

学习 Casbin

您可以在网站上找到 Casbin 的完整文档 (链接)