PHPackages                             mathsgod/light-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. mathsgod/light-rbac

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

mathsgod/light-rbac
===================

A simple and lightweight role-based access control library for PHP.

1.0.3(2y ago)083↓33.3%11MITPHPPHP &gt;=8.0

Since Apr 12Pushed 2y ago1 watchersCompare

[ Source](https://github.com/mathsgod/light-rbac)[ Packagist](https://packagist.org/packages/mathsgod/light-rbac)[ Docs](https://github.com/mathsgod/light-rbac)[ RSS](/packages/mathsgod-light-rbac/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (1)Versions (5)Used By (1)

Light RBAC
==========

[](#light-rbac)

Light RBAC is a simple Role-Based Access Control (RBAC) system implemented in PHP.

Class: Rbac
-----------

[](#class-rbac)

The `Rbac` class is the main class of the system. It manages roles and users.

### Properties

[](#properties)

- `$roles`: An instance of `RoleManager` that manages all roles in the system.
- `$users`: An instance of `UserManager` that manages all users in the system.

### Methods

[](#methods)

- `addUser(string $name, array $roles = []): User`: Adds a user with the given name and roles to the system. If the user already exists, it adds the roles to the existing user.
- `addRole(string $name)`: Adds a role with the given name to the system. If the role already exists, it returns the existing role.
- `getRole(string $name)`: Returns the role with the given name.
- `removeRole(string $name)`: Removes the role with the given name from the system.
- `getUser(string $name)`: Returns the user with the given name.
- `removeUser(string $name)`: Removes the user with the given name from the system.
- `getPermissions(): array`: Returns all permissions in the system.

Usage
-----

[](#usage)

First, create an instance of the `Rbac` class. Then, use the `addUser` and `addRole` methods to add users and roles to the system. Use the `getRole` method to retrieve a role by its name.

```
$rbac = new \Light\Rbac\Rbac();
$rbac->addRole('admin');
$rbac->addUser('John Doe', ['admin']);

$admin = $rbac->getRole('admin');
```

### Permissions

[](#permissions)

Permissions can be assigned to roles. A permission is a string that represents a certain action or resource. For example, `post:read`, `post:write`, `post:delete`, etc.

```
$role = $rbac->addRole('admin');
$role->addPermission('post:read');
$role->addPermission('post:write');
```

### Checking Permissions

[](#checking-permissions)

To check if a user has a certain permission, use the `can` method of the `User` class.

```
$user = $rbac->addUser('John Doe', ['admin']);
if ($user->can('post:read')) {
    echo 'John Doe can read posts.';
}
```

### Asterisk Permission

[](#asterisk-permission)

You can use the asterisk `*` to represent all permissions.

```
$role = $rbac->addRole('admin');
$role->addPermission('*');

if ($role->can('post:read')) {
    echo 'Admin can read posts.';
}
```

You can also use the asterisk 'resource:\*' to represent all permissions for a specific resource.

```
$role = $rbac->addRole('admin');
$role->addPermission('post:*');

if ($role->can('post:read')) {
    echo 'Admin can read posts.';
}
```

### Checking Roles

[](#checking-roles)

To check if a user has a certain role, use the `hasRole` method of the `User` class.

```
$user = $rbac->addUser('John Doe', ['admin']);
if ($user->hasRole('admin')) {
    echo 'John Doe is an admin.';
}
```

### Hierarchical Roles

[](#hierarchical-roles)

```
$admin = $rbac->addRole('admin');
$admin->addChild('editor');

$rbac->getRole('editor')->addPermission('post:read');

if($admin->can('post:read')) {
    echo 'Admin can read posts.';
}
```

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity11

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~1 days

Total

4

Last Release

763d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/18732337?v=4)[Raymond](/maintainers/mathsgod)[@mathsgod](https://github.com/mathsgod)

---

Top Contributors

[![mathsgod](https://avatars.githubusercontent.com/u/18732337?v=4)](https://github.com/mathsgod "mathsgod (35 commits)")

---

Tags

phprbacrole-based-access-control

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/mathsgod-light-rbac/health.svg)

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

###  Alternatives

[santigarcor/laratrust

This package provides a flexible way to add Role-based Permissions to Laravel

2.3k5.4M43](/packages/santigarcor-laratrust)[hosseinhezami/laravel-permission-manager

Advanced permission manager for Laravel.

403.3k](/packages/hosseinhezami-laravel-permission-manager)[smart-crowd/laravel-rbac

Laravel RBAC implementation.

151.3k](/packages/smart-crowd-laravel-rbac)

PHPackages © 2026

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