• This repository has been archived on 22/Aug/2023
  • Stars
    star
    154
  • Rank 242,095 (Top 5 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created about 6 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

专为ThinkPHP定制的Casbin的扩展包,Casbin是一个功能强大,高效的开源访问控制库。

Think-Casbin

Build Status Coverage Status Latest Stable Version Total Downloads License

PHP-Casbin 是一个强大的、高效的开源访问控制框架,它支持基于各种访问控制模型的权限管理。

Think-Casbin 是一个专为ThinkPHP5.1定制的Casbin的扩展包,使开发者更便捷的在thinkphp项目中使用Casbin。

针对 ThinkPHP6.0 现在推出了更加强大的扩展 ThinkPHP 6.0 Authorization.

知识储备

  • 熟练使用Composer包管理工具
  • 掌握ThinkPHP框架各个功能,例如:门面(Facade)、模型、数据库迁移工具等
  • 熟悉PHP命令行、ThinkPHP命令行的使用
  • 了解Casbin工作原理及用法

安装

  1. 创建thinkphp项目(如果没有):
composer create-project topthink/think=5.1.* tp5
  1. ThinkPHP项目里,安装Think-Casbin扩展:
composer require casbin/think-adapter
  1. 发布资源:
php think casbin:publish

这将自动创建model配置文件config/casbin-basic-model.conf,和Casbin的配置文件config/casbin.php

  1. 数据迁移:

由于Think-Casbin默认将Casbin的策略(Policy)存储在数据库中,所以需要初始化数据库表信息。

执行前,请确保数据库连接信息配置正确,如需单独修改Casbin的数据库连接信息或表名,可以修改config/casbin.php里的配置。

php think casbin:migrate

这将会自动创建Casbin的策略(Policy)表casbin_rule

用法

为用户分配权限

use Casbin;

// 给用户alice赋予对data1的read权限
Casbin::addPolicy('alice', 'data1', 'read');

判断是权限策略是否存在

Casbin::hasPolicy('alice', 'data1', 'read'); // true

移除权限

Casbin::removePolicy('alice', 'data1', 'read');

使用决策器,验证权限

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 (true === Casbin::enforce($sub, $obj, $act)) {
    // permit alice to read data1
    echo 'permit alice to read data1';
} else {
    // deny the request, show an error
}

自定义配置

config/casbin-basic-model.conf为Casbin的model文件

config/casbin.php为Casbin的adapter、db配置信息

更多API参考

关于

Think-Casbin

  • 实现基于Think-ORM的Adapter存储(将Policy存储在数据库中)
  • 实现Casbin的门面(think\Facade)调用,使用\Casbin::可以静态调用PHP-CasbinEnforcer的所有方法。
  • 使用配置文件对Casbin的Model、Adapter的可配置化

通过Casbin官网 (https://casbin.org )查看更多用法。

More Repositories

1

php-casbin

An authorization library that supports access control models like ACL, RBAC, ABAC in PHP .
PHP
1,204
star
2

laravel-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in Laravel.
PHP
279
star
3

think-authz

An authorization library that supports access control models like ACL, RBAC, ABAC in ThinkPHP.
PHP
260
star
4

codeigniter-permission

Associate users with roles and permissions, use Casbin in CodeIgniter4 Web Framework.
PHP
45
star
5

yii-permission

Use casbin in Yii PHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
45
star
6

laravel-casbin

This repository has moved to https://github.com/php-casbin/laravel-authz
PHP
43
star
7

webman-permission

🔒 An authorization library that supports access control models like ACL, RBAC, ABAC for webman plugin
PHP
39
star
8

database-adapter

Database adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
28
star
9

hyperf-permission

An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf.
PHP
12
star
10

casbin-tutorials

Here are some tutorials of PHP-Casbin.
10
star
11

dbal-adapter

Doctrine DBAL Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
10
star
12

easyswoole-permission

An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.
PHP
9
star
13

casbin-with-slim

Casbin skeleton application with Slim Framework 4.
PHP
9
star
14

yii-casbin

Use casbin in Yii PHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
7
star
15

casbin-client

PHP's client for Casbin-Server. Casbin-Server is the Access Control as a Service (ACaaS) solution based on Casbin.
PHP
6
star
16

symfony-permission

Use Casbin in Symfony Framework, Casbin is a powerful and efficient open-source access control library.
PHP
5
star
17

medoo-adapter

Medoo Adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
18

workerman-redis-watcher

Workerman Redis watcher for PHP-Casbin
PHP
4
star
19

laminas-db-adapter

Laminas-db Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
20

cake-permission

Use Casbin in CakePHP Framework, Casbin is a powerful and efficient open-source access control library.
PHP
4
star
21

phalcon-permission

An authorization library that supports access control models like ACL, RBAC, ABAC in Phalcon.
PHP
3
star
22

redis-watcher

Redis watcher for PHP-Casbin
2
star
23

psr3-bridge

This library provides a PSR-3 compliant bridge for Casbin Logger.
PHP
2
star
24

zend-db-adapter

Zend Db Adapter for Casbin, Casbin is a powerful and efficient open-source access control library.
PHP
2
star
25

swoole-redis-watcher

Redis watcher for PHP-Casbin in Swoole.
PHP
1
star