PHPackages                             krubio/perfect-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. krubio/perfect-rbac

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

krubio/perfect-rbac
===================

RBAC - Role Based Access

v0.1.2(2y ago)035MITPHP

Since Jun 27Pushed 2y ago1 watchersCompare

[ Source](https://github.com/benanamen/perfect-rbac)[ Packagist](https://packagist.org/packages/krubio/perfect-rbac)[ RSS](/packages/krubio-perfect-rbac/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (6)Versions (4)Used By (0)

[![codecov](https://camo.githubusercontent.com/b03c07c2b0e3dd8a2b752771a492313165e749b6c5e7c83689d98a3cdb468132/68747470733a2f2f636f6465636f762e696f2f67682f62656e616e616d656e2f706572666563742d726261632f6272616e63682f6d61737465722f67726170682f62616467652e7376673f746f6b656e3d31747a61547959646c6a)](https://codecov.io/gh/benanamen/perfect-rbac)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e857a188b526c2c77944b3cc58509a0b78a345185bb8887abe5676e34bc9f075/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d726261632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-rbac/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/0973e1a6ad32906ab6435972fa243e05be1f7a4ce172cd9a7fe6046530a1b1fa/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d726261632f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-rbac/?branch=master)[![Build Status](https://camo.githubusercontent.com/5183537333883bf592c76c329d9ce3086a116670dac1517964d555fbcdff8943/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d726261632f6261646765732f6275696c642e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/benanamen/perfect-rbac/build-status/master)[![Code Intelligence Status](https://camo.githubusercontent.com/7e936810a8e7e16adef831add1261d31c32c1803dc1073b08ba0609a4ebb17d9/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f62656e616e616d656e2f706572666563742d726261632f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)

[![Coverage](https://camo.githubusercontent.com/e2e2f1f52857a819b7fd40f8ee2cc25217d58087c69fe45d11a3be4704849f38/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d636f766572616765)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Maintainability Rating](https://camo.githubusercontent.com/310a52e1a3b548904be333e86e7ee75e22c6e01f79928de560d7943cc78be09a/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Code Smells](https://camo.githubusercontent.com/f5bc8755d7607b6c36764e2dffdb0b1106d2d038ae9a02b14dbe57c19d7407b8/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d636f64655f736d656c6c73)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Technical Debt](https://camo.githubusercontent.com/e4833552533385f89640c177b1abf0ad3433f30a066ff606fe0bfa28f73a120e/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d7371616c655f696e646578)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Quality Gate Status](https://camo.githubusercontent.com/537f219d299b0f7dda79301812d289d7eee9b258253e42fad2f21ca8f2db4017/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d616c6572745f737461747573)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Reliability Rating](https://camo.githubusercontent.com/0537e05953ecb8c93fafd92b5ec48553f94e63836b2e5b3661566bc1771e5d82/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)

[![Duplicated Lines (%)](https://camo.githubusercontent.com/c2aa419285687e500c5bf7aae9f83567a1516ee48f1341c66e9f6fd69ebf08e2/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d6475706c6963617465645f6c696e65735f64656e73697479)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Vulnerabilities](https://camo.githubusercontent.com/ca0f2cde978bcdd1d490ffdc7bed3c849e742e0808b9e85c2fdfaaa622e0c438/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d76756c6e65726162696c6974696573)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Bugs](https://camo.githubusercontent.com/9783d911a53f99229dc6a0e08ea89691f7d30c185133fbebc646d117ad47826a/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d62756773)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)[![Security Rating](https://camo.githubusercontent.com/8487dff3dcbe0966b2b8ea8da433225ec46ebacd1ae96b3312321e2249bfdee7/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d62656e616e616d656e5f706572666563742d72626163266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/summary/new_code?id=benanamen_perfect-rbac)

RBAC Authorization Service Documentation
========================================

[](#rbac-authorization-service-documentation)

The RBAC (Role-Based Access Control) Authorization Service provides a simple and effective way to implement role-based access control in your PHP application. This documentation will guide you on how to use the RBAC classes and their methods to perform user role authorization and permission checks.

Prerequisites
-------------

[](#prerequisites)

Before using the RBAC Authorization Service, make sure you have the following:

- PHP installed on your system
- PDO extension enabled (for database connectivity)
- A database with the required tables (user\_roles, roles, role\_permissions, and permissions) populated with relevant data

Class Overview
--------------

[](#class-overview)

The RBAC Authorization Service consists of the following classes:

- `AuthorizationService`: Provides methods to check if a user role is authorized based on a set of allowed roles.
- `PermissionsRepository`: Handles the retrieval of permissions for user roles from the database.
- `UserRolesRepository`: Handles the retrieval of user roles from the database.

Getting Started
---------------

[](#getting-started)

To start using the RBAC Authorization Service, follow these steps:

1. Include the necessary RBAC classes in your PHP file:

```
use PerfectApp\RBAC\AuthorizationService;
use PerfectApp\RBAC\PermissionsRepository;
use PerfectApp\RBAC\UserRolesRepository;
```

2. Create an instance of the `PDO` class with the appropriate database connection details.

```
$pdo = new PDO('mysql:host=localhost;dbname=your_database', 'username', 'password');
```

3. Create instances of the `PermissionsRepository` and `UserRolesRepository` classes, passing the `PDO` instance to their constructors.

```
$permissionsRepository = new PermissionsRepository($pdo);
$userRolesRepository = new UserRolesRepository($pdo);
```

4. Create an instance of the `AuthorizationService` class, passing the `UserRolesRepository` instance to its constructor.

```
$authorizationService = new AuthorizationService($userRolesRepository);
```

5. You are now ready to use the RBAC Authorization Service in your application.

Checking User Role Authorization
--------------------------------

[](#checking-user-role-authorization)

The `AuthorizationService` class provides the `isUserRoleAuthorized` method to check if a user role is authorized based on a set of allowed roles.

```
/**
 * @param int $userId
 * @param array $allowedRoles
 * @return bool
 */
public function isUserRoleAuthorized(int $userId, array $allowedRoles): bool
```

### Parameters

[](#parameters)

- `$userId` (integer): The ID of the user whose role needs to be checked.
- `$allowedRoles` (array): An array of allowed roles. The user's role will be compared against these roles.

### Return Value

[](#return-value)

- `true` if the user's role matches any of the allowed roles, indicating authorization.
- `false` if the user's role does not match any of the allowed roles or an error occurs.

### Example Usage

[](#example-usage)

```
$userId = 1;
$allowedRoles = ['admin', 'editor'];

if ($authorizationService->isUserRoleAuthorized($userId, $allowedRoles)) {
    echo "User is authorized.";
} else {
    echo "User is not authorized.";
}
```

Checking User Role Permissions
------------------------------

[](#checking-user-role-permissions)

The `PermissionsRepository` class provides the `userRoleHasPermission` method to check if a user role has a specific permission.

```
/**
 * @param int $userId
 * @param string $requiredPermission
 * @return bool
 */
public function userRoleHasPermission(int $userId, string $requiredPermission): bool
```

### Parameters

[](#parameters-1)

- `$userId` (integer): The ID of the user whose role's permission needs to be checked.
- `$requiredPermission` (string): The name of the required permission.

### Return Value

[](#return-value-1)

- `

true` if the user's role has the required permission.

- `false` if the user's role does not have the required permission or an error occurs.

### Example Usage

[](#example-usage-1)

```
$userId = 1;
$requiredPermission = 'edit_post';

if ($permissionsRepository->userRoleHasPermission($userId, $requiredPermission)) {
    echo "User has the required permission.";
} else {
    echo "User does not have the required permission.";
}
```

Conclusion
----------

[](#conclusion)

You have now learned how to use the RBAC Authorization Service to perform user role authorization and permission checks in your PHP application. By following the provided instructions and examples, you can easily integrate RBAC functionality into your application to control access based on user roles and permissions.

###  Health Score

20

—

LowBetter than 14% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity36

Early-stage or recently created project

 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 ~55 days

Total

3

Last Release

938d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/92efcd3e8d87753dc819d970a254160b7ea8a60de6a127e7f93acada576b49c5?d=identicon)[benanamen](/maintainers/benanamen)

---

Top Contributors

[![benanamen](https://avatars.githubusercontent.com/u/1039435?v=4)](https://github.com/benanamen "benanamen (13 commits)")

---

Tags

validaton

###  Code Quality

TestsCodeception

Static AnalysisPHPStan, Psalm

Type Coverage Yes

### Embed Badge

![Health badge](/badges/krubio-perfect-rbac/health.svg)

```
[![Health](https://phpackages.com/badges/krubio-perfect-rbac/health.svg)](https://phpackages.com/packages/krubio-perfect-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)
