PHPackages                             yii2mod/yii2-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. yii2mod/yii2-rbac

ActiveYii2-extension[Authentication &amp; Authorization](/categories/authentication)

yii2mod/yii2-rbac
=================

RBAC management module for Yii2

2.3(8y ago)150351.4k—7.1%56[7 issues](https://github.com/yii2mod/yii2-rbac/issues)[2 PRs](https://github.com/yii2mod/yii2-rbac/pulls)7MITPHPPHP &gt;=7.0.0CI failing

Since Jan 22Pushed 4y ago13 watchersCompare

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

READMEChangelog (10)Dependencies (5)Versions (20)Used By (7)

 [ ![](https://avatars0.githubusercontent.com/u/993323) ](https://github.com/yiisoft)

Yii2 RBAC Extension
===================

[](#yii2-rbac-extension)

Yii2-RBAC provides a web interface for advanced access control and includes following features:

- Allows CRUD operations for roles, permissions, rules
- Allows to assign multiple roles or permissions to the user
- Allows to create console migrations
- Integrated with [yii2mod/base](https://github.com/yii2mod/base)

[![Latest Stable Version](https://camo.githubusercontent.com/aacad81602f5883b3821e35c9abc17ab5c1a9e8f147abd9f818c564e440d6fdf/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d726261632f762f737461626c65)](https://packagist.org/packages/yii2mod/yii2-rbac) [![Total Downloads](https://camo.githubusercontent.com/02e328fc5aabdedd9429e298637d7f38ff6553995e2261ce0f1fee39dac389ee/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d726261632f646f776e6c6f616473)](https://packagist.org/packages/yii2mod/yii2-rbac) [![License](https://camo.githubusercontent.com/2947e3d22d2f4a051c9f92eda5f4161b8ed5e6d53d899be5a65874162a879d36/68747470733a2f2f706f7365722e707567782e6f72672f796969326d6f642f796969322d726261632f6c6963656e7365)](https://packagist.org/packages/yii2mod/yii2-rbac)[![Build Status](https://camo.githubusercontent.com/dba385e58f64ec2b6a79cca37e9ab2029660504454d00ce43bc4329318be9bf8/68747470733a2f2f7472617669732d63692e6f72672f796969326d6f642f796969322d726261632e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/yii2mod/yii2-rbac)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/a2640d5989089acfc9423e4df584406881327f9374b3bb586c923847ddf49bec/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f796969326d6f642f796969322d726261632f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/yii2mod/yii2-rbac/?branch=master)

Support us
----------

[](#support-us)

Does your business depend on our contributions? Reach out and support us on [Patreon](https://www.patreon.com/yii2mod). All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

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

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yii2mod/yii2-rbac "*"

```

or add

```
"yii2mod/yii2-rbac": "*"

```

to the require section of your composer.json.

Usage
-----

[](#usage)

Once the extension is installed, simply modify your application configuration as follows:

```
return [
    'modules' => [
        'rbac' => [
            'class' => 'yii2mod\rbac\Module',
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
            'defaultRoles' => ['guest', 'user'],
        ],
    ],
];
```

After you downloaded and configured Yii2-rbac, the last thing you need to do is updating your database schema by applying the migration:

```
$ php yii migrate/up --migrationPath=@yii/rbac/migrations
```

You can then access Auth manager through the following URL:

```
http://localhost/path/to/index.php?r=rbac/
http://localhost/path/to/index.php?r=rbac/route
http://localhost/path/to/index.php?r=rbac/permission
http://localhost/path/to/index.php?r=rbac/role
http://localhost/path/to/index.php?r=rbac/assignment

```

or if you have enabled pretty URLs, you may use the following URL:

```
http://localhost/path/to/index.php/rbac
http://localhost/path/to/index.php/rbac/route
http://localhost/path/to/index.php/rbac/permission
http://localhost/path/to/index.php/rbac/role
http://localhost/path/to/index.php/rbac/assignment

```

**Applying rules:**

1. For applying rules only for `controller` add the following code:

```
use yii2mod\rbac\filters\AccessControl;

class ExampleController extends Controller
{
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'allowActions' => [
                    'index',
                    // The actions listed here will be allowed to everyone including guests.
                ]
            ],
        ];
    }
}
```

2. For applying rules for `module` add the following code:

```
use Yii;
use yii2mod\rbac\filters\AccessControl;

/**
 * Class Module
 */
class Module extends \yii\base\Module
{
    /**
     * @return array
     */
    public function behaviors()
    {
        return [
            AccessControl::class
        ];
    }
}
```

3. Also you can apply rules via main configuration:

```
// apply for single module

'modules' => [
    'rbac' => [
        'class' => 'yii2mod\rbac\Module',
        'as access' => [
            'class' => yii2mod\rbac\filters\AccessControl::class
        ],
    ]
]

// or apply globally for whole application

'modules' => [
    ...
],
'components' => [
    ...
],
'as access' => [
    'class' => yii2mod\rbac\filters\AccessControl::class,
    'allowActions' => [
        'site/*',
        'admin/*',
        // The actions listed here will be allowed to everyone including guests.
        // So, 'admin/*' should not appear here in the production, of course.
        // But in the earlier stages of your development, you may probably want to
        // add a lot of actions here until you finally completed setting up rbac,
        // otherwise you may not even take a first step.
    ]
 ],
```

Internationalization
--------------------

[](#internationalization)

All text and messages introduced in this extension are translatable under category 'yii2mod.rbac'. You may use translations provided within this extension, using following application configuration:

```
return [
    'components' => [
        'i18n' => [
            'translations' => [
                'yii2mod.rbac' => [
                    'class' => 'yii\i18n\PhpMessageSource',
                    'basePath' => '@yii2mod/rbac/messages',
                ],
                // ...
            ],
        ],
        // ...
    ],
    // ...
];
```

Migrations
----------

[](#migrations)

You can create the console migrations for creating/updating RBAC items.

### Module setup

[](#module-setup)

To be able create the migrations, you need to add the following code to your console application configuration:

```
// console.php
'modules' => [
    'rbac' => [
        'class' => 'yii2mod\rbac\ConsoleModule'
    ]
]
```

### Methods

[](#methods)

1. `createPermission()`: creating a permission
2. `updatePermission()`: updating a permission
3. `removePermission()`: removing a permission
4. `createRole()`: creating a role
5. `updateRole()`: updating a role
6. `removeRole()`: removing a role
7. `createRule()`: creating a rule
8. `updateRule()`: updating a rule
9. `removeRule()`: removing a rule
10. `addChild()`: creating a child
11. `removeChild()`: removing a child
12. `assign()`: assign a role to a user

### Creating Migrations

[](#creating-migrations)

To create a new migration, run the following command:

```
$ php yii rbac/migrate/create
```

The required `name` argument gives a brief description about the new migration. For example, if the migration is about creating a new role named admin, you may use the name `create_role_admin` and run the following command:

```
$ php yii rbac/migrate/create create_role_admin
```

The above command will create a new PHP class file named m160817\_085702\_create\_role\_admin.php in the @app/rbac/migrations directory. The file contains the following code which mainly declares a migration class m160817\_085702\_create\_role\_admin with the skeleton code:

```
