PHPackages                             sunnyphp/rbac-db - 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. sunnyphp/rbac-db

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

sunnyphp/rbac-db
================

Yii RBAC Database storage

04PHP

Since Jan 4Pushed 2y agoCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

 [ ![](https://github.com/yiisoft.png) ](https://github.com/yiisoft)

Yii RBAC Database
=================

[](#yii-rbac-database)

The package provides [Yii Database](https://github.com/yiisoft/db) storage for [Yii RBAC](https://github.com/yiisoft/rbac).

[![Latest Stable Version](https://camo.githubusercontent.com/651dd30d9420fdeae85ddc59fe085fe8de37f4b95fb8e8743da0c16b9debf749/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f726261632d64622f762f737461626c652e706e67)](https://packagist.org/packages/yiisoft/rbac-db)[![Total Downloads](https://camo.githubusercontent.com/645472ff1d0d5675c7b7452ee7953463e551390e4e8b467f5dc580793bbb721a/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f726261632d64622f646f776e6c6f6164732e706e67)](https://packagist.org/packages/yiisoft/rbac-db)[![Build status](https://github.com/yiisoft/rbac-db/workflows/build/badge.svg)](https://github.com/yiisoft/rbac-db/actions?query=workflow%3Abuild)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e96791ee5da5fcf5696cb494804d64acc0ebc40a85606c7213214b8c5fc8104f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969736f66742f726261632d64622f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/yiisoft/rbac-db/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/d620c41a367d3a00bd0c253049a532a511ded547497221fe22180afd2cde6b2e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969736f66742f726261632d64622f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/yiisoft/rbac-db/?branch=master)[![Mutation testing badge](https://camo.githubusercontent.com/22cda41a64eee15455d78ee03d10f42f4e28235af776c6e359744613d164a290/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f6674253246726261632d64622532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/rbac-db/master)[![static analysis](https://github.com/yiisoft/rbac-db/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/rbac-db/actions?query=workflow%3A%22static+analysis%22)

Requirements
------------

[](#requirements)

- PHP 8.0 or higher.
- `PDO` PHP extension.
- One of the following drivers:
    - [SQLite](https://github.com/yiisoft/db-sqlite) (minimal required version is 3.8.3)
    - [MySQL](https://github.com/yiisoft/db-mysql)
    - [PostgreSQL](https://github.com/yiisoft/db-pgsql)
    - [Microsoft SQL Server](https://github.com/yiisoft/db-mssql)
    - [Oracle](https://github.com/yiisoft/db-oracle)
- `PDO` PHP extension for the selected driver.

Installation
------------

[](#installation)

The package could be installed with composer:

```
composer require yiisoft/rbac-db
```

General usage
-------------

[](#general-usage)

### Configuring database connection

[](#configuring-database-connection)

Configuration depends on a selected driver. Here is an example for PostgreSQL:

```
use Yiisoft\Cache\ArrayCache; // Requires https://github.com/yiisoft/cache
use Yiisoft\Db\Cache\SchemaCache;
use Yiisoft\Db\Pgsql\Connection;
use Yiisoft\Db\Pgsql\Driver;

$pdoDriver = new Driver('pgsql:host=127.0.0.1;dbname=yiitest;port=5432', 'user', 'password');
$pdoDriver->charset('UTF8');
$connection = Connection(
    $pdoDriver,
    new SchemaCache(
        new ArrayCache(), // Any other PSR-16 compatible cache can be used.
    )
);
```

More comprehensive examples can be found at [Yii Database docs](https://github.com/yiisoft/db/blob/master/docs/en/README.md#prerequisites).

### Working with schema

[](#working-with-schema)

In order to keep less dependencies, this package doesn't provide any CLI for working with schema. There are multiple options to choose from:

- Use migration tool like [Yii DB Migration](https://github.com/yiisoft/yii-db-migration). Migrations are dumped as plain SQL in `sql/migrations` folder.
- Without migrations, `DbSchemaManager` class can be used. An example of CLI command containing it can be found [here](examples/Command/RbacDbInit.php).
- Use plain SQL that is actual at the moment of installing `rbac-db` package (located at the root of `sql` folder).

The structure of plain SQL files:

- `pgsql-up.sql` - apply the changes for PostgreSQL driver.
- `pgsql-down.sql` - revert the changes for PostgreSQL driver.

Plain SQL assumes using default names for all 3 tables (`yii_rbac_` prefix is used):

- `yii_rbac_item`.
- `yii_rbac_assignment`.
- `yii_rbac_item_child`.

`DbSchemaManager` allows to customize table names:

```
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Rbac\Db\DbSchemaManager;

/** @var ConnectionInterface $database */
$schemaManager = new DbSchemaManager(
    database: $database,
    itemsTable: 'custom_items',
    assignmentsTable: 'custom_assignments',
    itemsChildrenTable: 'custom_items_children',
);
$schemaManager->ensureTables();
$schemaManager->ensureNoTables(); // Note: All existing data will be erased.
```

### Using storages

[](#using-storages)

The storages are not intended to be used directly. Instead, use them with `Manager` from [Yii RBAC](https://github.com/yiisoft/rbac) package:

```
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Rbac\Db\AssignmentsStorage;
use Yiisoft\Rbac\Db\ItemsStorage;
use Yiisoft\Rbac\Db\TransactionalManagerDecorator;
use Yiisoft\Rbac\Manager;
use Yiisoft\Rbac\Permission;
use Yiisoft\Rbac\RuleFactoryInterface;

/** @var ConnectionInterface $database */
$itemsStorage = new ItemsStorage($database);
$assignmentsStorage = new AssignmentsStorage($database);
/** @var RuleFactoryInterface $rulesContainer */
$manager = new TransactionalManagerDecorator(
    new Manager(
        itemsStorage: $itemsStorage,
        assignmentsStorage: $assignmentsStorage,
        // Requires https://github.com/yiisoft/rbac-rules-container or other compatible factory.
        ruleFactory: $rulesContainer,
    ),
);
$manager->addPermission(new Permission('posts.create'));
```

> Note wrapping manager with decorator - it additionally provides database transactions to guarantee data integrity.

> Note that it's not necessary to use both DB storages. Combining different implementations is possible. A quite popular case is to manage items via [PHP files](https://github.com/yiisoft/rbac-php) while store assignments in database.

More examples can be found in [Yii RBAC](https://github.com/yiisoft/rbac) documentation.

Testing
-------

[](#testing)

### Unit testing

[](#unit-testing)

The package is tested with [PHPUnit](https://phpunit.de/). To run tests:

```
./vendor/bin/phpunit
```

### Mutation testing

[](#mutation-testing)

The package tests are checked with [Infection](https://infection.github.io/) mutation framework. To run it:

```
./vendor/bin/infection
```

### Static analysis

[](#static-analysis)

The code is statically analyzed with [Psalm](https://psalm.dev). To run static analysis:

```
./vendor/bin/psalm
```

###  Health Score

14

—

LowBetter than 2% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity20

Early-stage or recently created project

 Bus Factor2

2 contributors hold 50%+ of commits

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/2d3c06ba089f07a9a059a64949f5e608ae3aa4e0eaec67424f4d052c5c27ad44?d=identicon)[sunnyphp](/maintainers/sunnyphp)

---

Top Contributors

[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (20 commits)")[![arogachev](https://avatars.githubusercontent.com/u/8326201?v=4)](https://github.com/arogachev "arogachev (16 commits)")[![sunnyphp](https://avatars.githubusercontent.com/u/3903438?v=4)](https://github.com/sunnyphp "sunnyphp (4 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (3 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (3 commits)")[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (2 commits)")[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (2 commits)")[![sankaest](https://avatars.githubusercontent.com/u/21160342?v=4)](https://github.com/sankaest "sankaest (1 commits)")

### Embed Badge

![Health badge](/badges/sunnyphp-rbac-db/health.svg)

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

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