PHPackages                             casbin/think-adapter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. casbin/think-adapter

AbandonedArchivedLibrary[Authentication &amp; Authorization](/categories/authentication)

casbin/think-adapter
====================

Use casbin in thinkphp.

v1.0.3(6y ago)1523.2k21[1 issues](https://github.com/php-casbin/think-casbin/issues)1Apache-2.0PHP

Since Nov 13Pushed 5y ago3 watchersCompare

[ Source](https://github.com/php-casbin/think-casbin)[ Packagist](https://packagist.org/packages/casbin/think-adapter)[ RSS](/packages/casbin-think-adapter/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (7)Dependencies (6)Versions (9)Used By (1)

Think-Casbin
============

[](#think-casbin)

[![Build Status](https://camo.githubusercontent.com/a9a8b861b06174a8cc310770965b3d652c1adc1dc640c4ae27c9ee5bc2d680a0/68747470733a2f2f7472617669732d63692e6f72672f7068702d63617362696e2f7468696e6b2d63617362696e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/php-casbin/think-casbin)[![Coverage Status](https://camo.githubusercontent.com/e1d9ca72f3a78b9aee32196d6b3ff4cbd89a24f96c854eded70e27193e38f369/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7068702d63617362696e2f7468696e6b2d63617362696e2f62616467652e737667)](https://coveralls.io/github/php-casbin/think-casbin)[![Latest Stable Version](https://camo.githubusercontent.com/0da8e8547cc31251e765bd734691c807e7076aea92cf06c3a30d9d5d9fb813e9/68747470733a2f2f706f7365722e707567782e6f72672f63617362696e2f7468696e6b2d616461707465722f762f737461626c65)](https://packagist.org/packages/casbin/think-adapter)[![Total Downloads](https://camo.githubusercontent.com/ba60183fa543e5a05fe1c180e4e89745e08bf76c93b5806279815e587dc648c3/68747470733a2f2f706f7365722e707567782e6f72672f63617362696e2f7468696e6b2d616461707465722f646f776e6c6f616473)](https://packagist.org/packages/casbin/think-adapter)[![License](https://camo.githubusercontent.com/a55b99c4548e01eeb0908c952ed1d8a891c89cb4c392169ed1e99f4bbd1921e8/68747470733a2f2f706f7365722e707567782e6f72672f63617362696e2f7468696e6b2d616461707465722f6c6963656e7365)](https://packagist.org/packages/casbin/think-adapter)

[PHP-Casbin](https://github.com/php-casbin/php-casbin) 是一个强大的、高效的开源访问控制框架，它支持基于各种访问控制模型的权限管理。

[Think-Casbin](https://github.com/php-casbin/think-casbin) 是一个专为ThinkPHP5.1定制的Casbin的扩展包，使开发者更便捷的在thinkphp项目中使用Casbin。

> 针对 ThinkPHP6.0 现在推出了更加强大的扩展 [ThinkPHP 6.0 Authorization](https://github.com/php-casbin/think-authz).

### 知识储备

[](#知识储备)

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

### 安装

[](#安装)

1. 创建thinkphp项目（**如果没有**）：

```
composer create-project topthink/think=5.1.* tp5

```

2. 在`ThinkPHP`项目里，安装`Think-Casbin`扩展：

```
composer require casbin/think-adapter

```

3. 发布资源:

```
php think casbin:publish

```

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

4. 数据迁移:

由于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参考

[](#更多api参考)

- [Management API](https://casbin.org/docs/en/management-api)
- [RBAC API](https://casbin.org/docs/en/rbac-api)

### 关于

[](#关于)

**Think-Casbin**：

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

通过Casbin官网 ( )查看更多用法。

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 90.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~68 days

Recently: every ~82 days

Total

8

Last Release

2264d ago

Major Versions

v0.4 → v1.0.02019-08-21

### Community

Maintainers

![](https://www.gravatar.com/avatar/f32cb7625515c0969fec15915be4a137f057c3484420bb7abf025c8e975dd3a5?d=identicon)[leeqvip](/maintainers/leeqvip)

---

Top Contributors

[![leeqvip](https://avatars.githubusercontent.com/u/35752209?v=4)](https://github.com/leeqvip "leeqvip (19 commits)")[![Tinywan](https://avatars.githubusercontent.com/u/14959876?v=4)](https://github.com/Tinywan "Tinywan (2 commits)")

---

Tags

abacaccess-controlaclcasbinpermissionrbacrestfulrolessecuritythinkphpaclpermissionrbaccasbinthinkphpcasbin-adapter

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/casbin-think-adapter/health.svg)

```
[![Health](https://phpackages.com/badges/casbin-think-adapter/health.svg)](https://phpackages.com/packages/casbin-think-adapter)
```

###  Alternatives

[spatie/laravel-permission

Permission handling for Laravel 12 and up

12.9k89.8M1.0k](/packages/spatie-laravel-permission)[casbin/think-authz

An authorization library that supports access control models like ACL, RBAC, ABAC for ThinkPHP.

27918.5k6](/packages/casbin-think-authz)[casbin/casbin

a powerful and efficient open-source access control library for php projects.

1.3k1.4M54](/packages/casbin-casbin)[casbin/laravel-authz

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

324339.9k4](/packages/casbin-laravel-authz)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[casbin/webman-permission

webman casbin permission plugin

523.4k2](/packages/casbin-webman-permission)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
