Simple RBAC Manager for Yii 2.0. Modified of yii2-mimin
Note: This extension is modified according to my needs
Before you install and use this extension, then make sure that your application has been using the login authentication to the database. especially for yii basic template. Because without it, this extension will produce error and useless.
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist dickyermawan/yii2-admin "*"
or add
"dickyermawan/yii2-admin": "*"
to the require section of your composer.json
file.
Once the extension is installed, simply use it in your code by :
in config
'as access' => [
'class' => '\dickyermawan\admin\components\AccessControl',
'allowActions' => [
// add wildcard allowed action here!
'site/*',
'debug/*',
'admin/*', // only in dev mode
],
],
...,
'modules' => [
'admin' => [
'class' => '\dickyermawan\admin\Module',
],
...
],
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
in console
'components' => [
'authManager' => [
'class' => 'yii\rbac\DbManager', // only support DbManager
],
],
You should migrate :
yii migrate --migrationPath=@yii/rbac/migrations
yii migrate --migrationPath=@dickyermawan/admin/migrations
And the last one :
Copy User.php in "Extras" folder to your app models.
First login
username: admin
password: admin
default
password: 1234
This RBAC manager have three main page, they are:
http://localhost/path/to/index.php?r=admin/route
http://localhost/path/to/index.php?r=admin/role
http://localhost/path/to/index.php?r=admin/user
It is used for checking if route right to access
if ((Admin::checkRoute($this->context->id.'/create'))){
echo Html::a('Create Note', ['create'], ['class' => 'btn btn-success']);
}
// in other case maybe You want ensure same of route so You can add parameter strict true
if ((Admin::checkRoute($this->context->id.'/create',true))){
It is is used for filtering right access menu
use dickyermawan\admin\components\Admin;
$menuItems = [
['label' => 'Home', 'url' => ['/site/index']],
['label' => 'About', 'url' => ['/site/about']],
['label' => 'Contact', 'url' => ['/site/contact']],
];
if (\Yii::$app->user->isGuest){
$menuItems[] = ['label' => 'Login', 'url' => ['/site/login']];
}
else{
$menuItems[] = ['label' => 'App', 'items' => [
['label' => 'Category', 'url' => ['/category/index']],
['label' => 'Product', 'url' => ['/product/index']],
['label' => 'Cart', 'url' => ['/cart/index']],
]];
$menuItems[] = [
'label' => 'Logout (' . \Yii::$app->user->identity->username . ')',
'url' => ['/site/logout'],
'linkOptions' => ['data-method' => 'post']
];
}
$menuItems = Admin::filterMenu($menuItems);
// in other case maybe You want ensure same of route so You can add parameter strict true
// $menuItems = Admin::filterMenu($menuItems,true);
echo Nav::widget([
'options' => ['class' => 'navbar-nav navbar-right'],
'items' => $menuItems,
]);
It is used for filtering template of Gridview Action Column
use dickyermawan\admin\components\Admin;
echo GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
...,
[
'class' => 'yii\grid\ActionColumn',
'template' => Admin::filterActionColumn([
'update','delete','download'
],$this->context->route),
...
]
]
]);