PHPackages                             shen2/simple-rbac - 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. shen2/simple-rbac

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

shen2/simple-rbac
=================

A simple RBAC library

351[1 PRs](https://github.com/shen2/SimpleRbac/pulls)PHP

Since Apr 25Pushed 7y ago2 watchersCompare

[ Source](https://github.com/shen2/SimpleRbac)[ Packagist](https://packagist.org/packages/shen2/simple-rbac)[ RSS](/packages/shen2-simple-rbac/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

SimpleRbac
==========

[](#simplerbac)

SimpleRbac是一个处理访控权限问题的php类，可以让web应用中的各种权限判断代码变得简单清晰。

```
if (!$visitor->isAllowedTo('delete', $image))
	throw Exception('你没有删除图片的权限');
```

Visitor (访问者)
-------------

[](#visitor-访问者)

故名思议，就是当前的访客信息，你可以编写自己的访客类，只要use SimpleRbac\\SubjectTrait，就可以获得isAllowedTo()方法。

```
class Visitor{
	use SimpleRbac\SubjectTrait;
	// your code...
}
```

Role (角色)
---------

[](#role-角色)

SimpleRbac中的角色，是指当前访问者相对于某个资源而言的角色，角色有可能有一个，也可能有多个，也可能没有。比如，我自己发布的文章，我既是author，又是system-administrator。而对于一个未登录的访客而言，他对于这篇文章没有任何角色。

Resource (资源)
-------------

[](#resource-资源)

资源可以是一篇文章，一张图片，通常可以和Model联系在一起。只要定义自己的$\_permissionAssignments访问表数组，同时定义getRoles()方法，可以实现SimpleRbac\\ResourceInterface接口。例如：

```
class Image implements \SimpleRbac\ResourceInterface{
	use \SimpleRbac\ResourceTrait;
	// 定义访控表
	public static $_permissionAssignments = [
		'author'	=> ['delete' => true, 'edit' => true, 'update' => true, 'close' => true,],
		'administrator'	=> ['delete' => true, 'edit' => true, 'update' => true, 'replace' => true,],
	];

	// 获取用户相对于当前对象的角色
	public function getRoles($subject){
		$roles = [];
		if ($this['author_id'] == $subject['user_id']){
			$roles[] = 'author';
		}

		if ($subject['role'] == 'admin'){
			$roles[] = 'administrator';
		}

		return $roles;
	}
}
```

大功告成
----

[](#大功告成)

于是你就可以写出像自然语言一样优雅的代码了：

```
if (!$visitor->isAllowedTo('delete', $image))
	throw Exception('你没有删除图片的权限');
```

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 83.3% 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/2515a1b7a6e85454fd15245886d030202007d1329757aa4bf0d0123f49789279?d=identicon)[shen2](/maintainers/shen2)

---

Top Contributors

[![shen2](https://avatars.githubusercontent.com/u/1523457?v=4)](https://github.com/shen2 "shen2 (10 commits)")[![hikenote](https://avatars.githubusercontent.com/u/8486259?v=4)](https://github.com/hikenote "hikenote (2 commits)")

### Embed Badge

![Health badge](/badges/shen2-simple-rbac/health.svg)

```
[![Health](https://phpackages.com/badges/shen2-simple-rbac/health.svg)](https://phpackages.com/packages/shen2-simple-rbac)
```

###  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)
