yii2.0 引入权限控制插件
权限控制:"mdmsoft/yii2-admin": "~2.0"
教程:http://www.cnblogs.com/zyf-zhaoyafei/p/5825455.html
1.下载yii-admin
php composer.phar require mdmsoft/yii2-admin "~2.0"
2.coupon
CREATE TABLE `coupon_user_profile` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL, `email` char(100) NOT NULL, `username` char(100) NOT NULL, `province` char(100) NOT NULL, `phone_number` char(11) NOT NULL, `user_group_id` char(11) NOT NULL, `status` int(10) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=80 DEFAULT CHARSET=utf8;
(1)配置中加入yii-admin的配置项
coupon\backend\config\main.php
'modules' => [ 'admin' => [ 'class' => 'mdm\admin\Module', 'layout' => 'top-menu', //yii2-admin的导航菜单//分配控制器配置 'controllerMap' => [ 'assignment' => [ 'class' => 'mdm\admin\controllers\AssignmentController', 'userClassName' => 'backend\models\UserProfile', 'idField' => 'id', 'usernameField' => 'username', ] ], ], ], 'aliases' => [ '@mdm/admin' => '@vendor/mdmsoft/yii2-admin', ], 'as access' => [ 'class' => 'mdm\admin\components\AccessControl', 'allowActions' => [ 'site/*', //允许访问的节点,可自行添加 'admin/*', //允许所有人访问admin节点及其子节点 ] ],
car-retail\console\config\main.php
'components' => [ 'authManager' => [ 'class' => 'yii\rbac\DbManager', ], ],
(2)配置数据库权限表
yii migrate --migrationPath=@yii/rbac/migrations
yii migrate --migrationPath=@mdm/admin/migrations
(3)进行菜单控制
coupon\backend\views\layouts\main.php
//var_dump(Yii::$app->user->id);exit; //var_dump(MenuHelper::getAssignedMenu(Yii::$app->user->id)); /***************************************************************/ if (Yii::$app->user->isGuest) { $menuItems[] = ['label' => 'Login', 'url' => ['/site/login']]; } else { $menuItems[] = '<li>' . Html::beginForm(['/site/logout'], 'post') . Html::submitButton( '退出 (' .Yii::$app->user->identity->profile->user_group_id. ' : ' .Yii::$app->user->identity->profile->username . ')', ['class' => 'btn btn-link'] ) . Html::endForm() . '</li>'; } echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => $menuItems, ]); //这里我们对一开始写的菜单menu进行了优化 echo Nav::widget( [ "encodeLabels" => false, "options" => ['class' => 'navbar-nav navbar-right'], "items" => MenuHelper::getAssignedMenu(Yii::$app->user->id), ] );
(4)对分配模块进行修改
coupon\vendor\mdmsoft\yii2-admin\controllers\AssignmentController.php
coupon\vendor\mdmsoft\yii2-admin\views\assignment\view.php