denchotsanov/yii2-user-rbac

Installs: 55

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

v0.3.7 2019-11-18 20:02 UTC

This package is auto-updated.

Last update: 2024-10-25 08:20:12 UTC


README

Yii2 RBAC USER Extension


Latest Stable Version Total Downloads Latest Unstable Version License

Installation

The preferred way to install this extension is through composer.

Either run

composer require --prefer-dist denchotsanov/yii2-user-rbac "*"

or add

"denchotsanov/yii2-user-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' => 'denchotsanov\rbac\Module',
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'yii\rbac\DbManager',
        ],
    ],
];

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

or add in console config file

'controllerMap' => [
        'migrate' => [
            'class' => 'yii\console\controllers\MigrateController',
            'migrationPath' => [
                ...
                '@yii/rbac/migrations',
                ...
            ],
        ],
    ],

You can then access Auth manager through the following URL:

[SERVER]/rbac/
[SERVER]/rbac/route
[SERVER]/rbac/permission
[SERVER]/rbac/role
[SERVER]/rbac/assignment

Applying rules:

  1. For applying rules only for controller add the following code:
use denchotsanov\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 denchotsanov\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' => 'denchotsanov\rbac\Module',
        'as access' => [
            'class' => denchotsanov\rbac\filters\AccessControl::class
        ],
    ]
]

// or apply globally for whole application

'modules' => [
    ...
],
'components' => [
    ...
],
'as access' => [
    'class' => denchotsanov\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.
    ]
 ],