• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RBAC Manager for Yii 2

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

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality

Support us

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.

Installation

The preferred way to install this extension is through composer.

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

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.
                ]
            ],
        ];
    }
}
  1. 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
        ];
    }
}
  1. 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

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

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

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

  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

To create a new migration, run the following command:

$ php yii rbac/migrate/create <name>

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:

<?php

use yii2mod\rbac\migrations\Migration;

class m160817_085702_create_role_admin extends Migration
{
    public function safeUp()
    {

    }

    public function safeDown()
    {
        echo "m160817_085702_create_role_admin cannot be reverted.\n";

        return false;
    }
}

The following code shows how you may implement the migration class to create a admin role:

<?php

use yii2mod\rbac\migrations\Migration;

class m160817_085702_create_role_admin extends Migration
{
    public function safeUp()
    {
        $this->createRole('admin', 'admin has all available permissions.');
    }

    public function safeDown()
    {
        $this->removeRole('admin');
    }
}

You can see a complex example of migration here.

Applying Migrations

To upgrade a database to its latest structure, you should apply all available new migrations using the following command:

$ php yii rbac/migrate

Reverting Migrations

To revert (undo) one or multiple migrations that have been applied before, you can run the following command:

$ php yii rbac/migrate/down     # revert the most recently applied migration
$ php yii rbac/migrate/down 3   # revert the most 3 recently applied migrations

Redoing Migrations

Redoing migrations means first reverting the specified migrations and then applying again. This can be done as follows:

$ php yii rbac/migrate/redo     # redo the last applied migration
$ php yii rbac/migrate/redo 3   # redo the last 3 applied migrations

More Repositories

1

yii2-comments

Comments module for Yii2
PHP
158
star
2

yii2-cart

Yii2 shopping cart
PHP
119
star
3

yii2-settings

Persistent settings in Yii2
PHP
102
star
4

yii2-swagger

Swagger Documentation Generator for Yii2 Framework
PHP
63
star
5

yii2-enum

Enumerable helper
PHP
63
star
6

yii2-editable

Editable widget and column for gridview.
PHP
54
star
7

base

Yii2 application template
PHP
53
star
8

yii2-cashier

Yii2 Cashier provides an interface to Stripe's subscription billing services.
PHP
45
star
9

yii2-cms

Simple CMS extension
PHP
44
star
10

yii2-sweet-alert

SweetAlert widget for Yii2 framework
PHP
43
star
11

yii2-array-query

Yii2 component that allows for searching/filtering the elements of an array.
PHP
34
star
12

yii2-ftp

FTP Client for Yii2
PHP
33
star
13

collection

Basic collection library for Yii Framework 2.0
PHP
30
star
14

yii2-behaviors

Collection of useful behaviors for Yii Framework 2.0
PHP
29
star
15

yii2-user

Flexible user registration and authentication module for Yii2.
PHP
27
star
16

yii2-link-preview

LinkPreview widget render page preview
PHP
27
star
17

yii2-image

Provides methods for the dynamic manipulation of images. Various image formats such as JPEG, PNG, and GIF can be resized, cropped, rotated.
PHP
26
star
18

yii2-cron-log

Component for logging cron jobs
PHP
23
star
19

yii2-ion-slider

Easily customizable range slider with skins support.
PHP
22
star
20

yii2-tree

Tree widget based on Fancytree extension
PHP
21
star
21

yii2-selectize

selectize.js wrapper for yii2.
PHP
19
star
22

yii2-google-maps-markers

Google Maps Markers Widget for Yii2
PHP
18
star
23

yii2-star-rating

Star rating widget based on jQuery Raty
PHP
18
star
24

yii2-validators

Collection of useful validators for Yii Framework 2.0
PHP
17
star
25

yii2-scheduling

Scheduling extension for Yii2 framework
PHP
17
star
26

yii2-helpers

Collection of useful helper functions for Yii Framework 2.0
PHP
15
star
27

yii2-timezone

Timezone detector
PHP
15
star
28

yii2-bootstrap-notify

Bootstrap Notify widget for Yii2 framework
PHP
14
star
29

yii2-chosen-select

Select Widget based on Chosen jQuery plugin
PHP
14
star
30

yii2-markdown

Markdown Widget for Yii 2
PHP
13
star
31

yii2-c3-chart

Yii2 wrapper for D3-based reusable chart library
PHP
11
star
32

yii2-bx-slider

bx-slider.js wrapper for yii2.
PHP
11
star
33

yii2-braintree

Yii2 Braintree provides an interface to Braintree subscription billing services.
PHP
9
star
34

yii2-gii-extended

This generator generates enumerable classes or controller and views that implement CRUD (Create, Read, Update, Delete) operations for the specified data model.
PHP
8
star
35

yii2-moderation

A simple Content Moderation System for Yii2
PHP
7
star
36

yii2-disqus

Yii2 disqus comment widget
PHP
6
star
37

yii2-feed

social feeds widget
PHP
6
star
38

yii2-pie

d3pie.js wrapper for yii2
PHP
5
star
39

yii2-toggle-column

Provides a toggle data column and action for Yii Framework 2.0
PHP
3
star