PHPackages                             crayoon/hyperf-casbin - 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. crayoon/hyperf-casbin

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

crayoon/hyperf-casbin
=====================

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

15PHP

Since Jan 4Pushed 3y agoCompare

[ Source](https://github.com/crayxn/hyperf-casbin)[ Packagist](https://packagist.org/packages/crayoon/hyperf-casbin)[ RSS](/packages/crayoon-hyperf-casbin/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Hyperf Casbin
=============

[](#hyperf-casbin)

[![Test](https://github.com/donjan-deng/hyperf-casbin/actions/workflows/test.yml/badge.svg)](https://github.com/donjan-deng/hyperf-casbin/actions/workflows/test.yml)[![Latest Stable Version](https://camo.githubusercontent.com/24dc8e8a63db37ddd5b6c51b64b49b828dec5c582d5768cb45dd11c7ef261e00/68747470733a2f2f706f7365722e707567782e6f72672f646f6e6a616e2d64656e672f6879706572662d63617362696e2f762f737461626c65)](https://packagist.org/packages/donjan-deng/hyperf-casbin)[![Total Downloads](https://camo.githubusercontent.com/37cd70a8cd9673c7ea4f59fd9f1aa35fcdf8f34d406163cd047be213d28b6c69/68747470733a2f2f706f7365722e707567782e6f72672f646f6e6a616e2d64656e672f6879706572662d63617362696e2f646f776e6c6f616473)](https://packagist.org/packages/donjan-deng/hyperf-casbin)[![License](https://camo.githubusercontent.com/6a05b56a79e98397e072d243d53c05b2a8be40c846ec1f939fc4422bec87c683/68747470733a2f2f706f7365722e707567782e6f72672f646f6e6a616e2d64656e672f6879706572662d63617362696e2f6c6963656e7365)](https://github.com/php-casbin/laravel-authz/blob/master/LICENSE)

Casbin是一个强大的、高效的开源访问控制框架，其权限管理机制支持多种访问控制模型。本项目做了Hyperf适配并自带了一个RBAC模型，使用本项目前你需要先学会如何使用Casbin。

简单使用
----

[](#简单使用)

Casbin只负责访问控制，在RBAC方案中也只是储存用户和角色之间的映射关系，身份认证、管理用户列表或角色列表应由项目自身来管理。这里有个简单的示例：[example](https://github.com/donjan-deng/hyperf-casbin-example)

### 版本定义

[](#版本定义)

本项目主版本与Casbin主版本相同，子版本和修订版本为项目独有

### 重大变更

[](#重大变更)

- 3.21增加Redis Watcher 升级需要重新发布配置
- 3.6 升级需要重新发布配置

### 安装

[](#安装)

```
 composer require donjan-deng/hyperf-casbin

```

发布配置

```
 php bin/hyperf.php vendor:publish donjan-deng/hyperf-casbin

```

配置文件config/autoload/casbin.php

API

```
use Donjan\Casbin\Enforcer;

为用户添加权限

Enforcer::addPermissionForUser('user1', '/user', 'read');

删除一个用户的权限

Enforcer::deletePermissionForUser('user1', '/user', 'read');

获取用户所有权限

Enforcer::getPermissionsForUser('user1');

为用户添加角色

Enforcer::addRoleForUser('user1', 'role1');

为角色添加权限

Enforcer::addPermissionForUser('role1', '/user', 'edit');

获取所有角色

Enforcer::getAllRoles();

获取用户所有角色

Enforcer::getRolesForUser('user1');

根据角色获取用户

Enforcer::getUsersForRole('role1');

判断用户是否属于一个角色

Enforcer::hasRoleForUser('use1', 'role1');

删除用户角色

Enforcer::deleteRoleForUser('use1', 'role1');

删除用户所有角色

Enforcer::deleteRolesForUser('use1');

删除角色

Enforcer::deleteRole('role1');

删除权限

Enforcer::deletePermission('/user', 'read');

删除用户或者角色的所有权限

Enforcer::deletePermissionsForUser('user1');
Enforcer::deletePermissionsForUser('role1');

检查权限，返回 true or false

Enforcer::enforce("user1", "/user", "edit")
```

更多使用方法详见官方API

官方资源
----

[](#官方资源)

- [官方文档](https://casbin.org/docs/zh-CN/overview)
- [模型编辑器](https://casbin.org/zh-CN/editor)
- [管理API](https://casbin.org/docs/zh-CN/management-api)
- [RBAC API](https://casbin.org/docs/zh-CN/rbac-api)

参考库
---

[](#参考库)

- [php-casbin](https://github.com/php-casbin/php-casbin)
- [database-adapter](https://github.com/php-casbin/database-adapter)
- [laravel-authz](https://github.com/php-casbin/laravel-authz)

License
-------

[](#license)

This project is licensed under the [Apache 2.0 license](LICENSE).

###  Health Score

15

—

LowBetter than 3% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity23

Early-stage or recently created project

 Bus Factor1

Top contributor holds 50% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/1c968710990269a99648ae69495ae0850220f6eee547ae36ad1af3da6f799a98?d=identicon)[tangshaoxu](/maintainers/tangshaoxu)

---

Top Contributors

[![donjan-deng](https://avatars.githubusercontent.com/u/8256490?v=4)](https://github.com/donjan-deng "donjan-deng (2 commits)")[![nashgao](https://avatars.githubusercontent.com/u/9336171?v=4)](https://github.com/nashgao "nashgao (1 commits)")[![onekb](https://avatars.githubusercontent.com/u/16450084?v=4)](https://github.com/onekb "onekb (1 commits)")

### Embed Badge

![Health badge](/badges/crayoon-hyperf-casbin/health.svg)

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

###  Alternatives

[namshi/jose

JSON Object Signing and Encryption library for PHP.

1.8k99.6M101](/packages/namshi-jose)[league/oauth1-client

OAuth 1.0 Client Library

99698.8M106](/packages/league-oauth1-client)[bezhansalleh/filament-shield

Filament support for `spatie/laravel-permission`.

2.8k2.9M88](/packages/bezhansalleh-filament-shield)[gesdinet/jwt-refresh-token-bundle

Implements a refresh token system over Json Web Tokens in Symfony

70516.4M35](/packages/gesdinet-jwt-refresh-token-bundle)[league/oauth2-google

Google OAuth 2.0 Client Provider for The PHP League OAuth2-Client

41721.2M118](/packages/league-oauth2-google)[illuminate/auth

The Illuminate Auth package.

9327.3M1.0k](/packages/illuminate-auth)

PHPackages © 2026

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