casbin/laravel-adapter

此包已被弃用且不再维护。未建议替代包。

在 Laravel 中使用 casbin。

v0.2 2019-04-02 09:36 UTC

This package is auto-updated.

Last update: 2020-11-24 10:45:55 UTC


README

Build Status Coverage Status Latest Stable Version Total Downloads License

在 Laravel 中使用 Casbin

值得注意的是,我们现在推荐 laravel-authz

安装

在 Laravel 项目的 composer.json 中要求此包。这将下载包。

composer require casbin/laravel-adapter

CasbinAdapter\Laravel\CasbinServiceProvider 是默认自动发现的并已注册,但如果您想自己注册

config/app.php 中添加 ServiceProvider

'providers' => [
    /*
     * Package Service Providers...
     */
    CasbinAdapter\Laravel\CasbinServiceProvider::class,
]

Casbin 面板也是默认自动发现的,但如果您想手动添加

config/app.php 中添加 Facade

'aliases' => [
    // ...
    'Casbin' => CasbinAdapter\Laravel\Facades\Casbin::class,
]

要发布配置,运行 vendor 发布命令

php artisan vendor:publish

这将创建一个新的模型配置文件,名为 config/casbin-basic-model.conf,以及一个新的 casbin 配置文件,名为 config/casbin.php

要迁移迁移,运行迁移命令

php artisan migrate

这将创建一个名为 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 (Casbin::enforce($sub, $obj, $act) === true) {
    // permit alice to read data1
} else {
    // deny the request, show an error
}

定义您自己的 model.conf

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

学习 Casbin

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