项目作者: php-casbin

项目描述 :
This repository has moved to https://github.com/php-casbin/laravel-authz
高级语言: PHP
项目地址: git://github.com/php-casbin/laravel-casbin.git
创建时间: 2018-10-18T16:19:33Z
项目社区:https://github.com/php-casbin/laravel-casbin

开源协议:Apache License 2.0

下载


Laravel-Casbin

Build Status
Coverage Status
Latest Stable Version
Total Downloads
License

Use Casbin in Laravel.

It is worth mentioning that we now recommend laravel-authz.

Installation

Require this package in the composer.json of your Laravel project. This will download the package.

  1. composer require casbin/laravel-adapter

The CasbinAdapter\Laravel\CasbinServiceProvider is auto-discovered and registered by default, but if you want to register it yourself:

Add the ServiceProvider in config/app.php

  1. 'providers' => [
  2. /*
  3. * Package Service Providers...
  4. */
  5. CasbinAdapter\Laravel\CasbinServiceProvider::class,
  6. ]

The Casbin facade is also auto-discovered, but if you want to add it manually:

Add the Facade in config/app.php

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

To publish the config, run the vendor publish command:

  1. php artisan vendor:publish

This will create a new model config file named config/casbin-basic-model.conf and a new casbin config file named config/casbin.php.

To migrate the migrations, run the migrate command:

  1. php artisan migrate

This will create a new table named casbin_rule

Usage

  1. use \Casbin;
  2. $sub = "alice"; // the user that wants to access a resource.
  3. $obj = "data1"; // the resource that is going to be accessed.
  4. $act = "read"; // the operation that the user performs on the resource.
  5. if (Casbin::enforce($sub, $obj, $act) === true) {
  6. // permit alice to read data1
  7. } else {
  8. // deny the request, show an error
  9. }

Define your own model.conf

You can modify the config file named config/casbin-basic-model.conf

Learning Casbin

You can find the full documentation of Casbin on the website.