Пример #1
0
 /**
  * 删除菜单
  * @return Response
  * @throws \Exception
  */
 public function actionMenudel()
 {
     $id = Yii::$app->request->get('id');
     $level = Yii::$app->request->get('level');
     //循环删除是为了在afterDelete删除对应的permission
     //一级菜单先删除孙子节点
     if ($level == 1) {
         $son = TMenu::find()->where(['parentid' => $id, 'level' => 2])->all();
         foreach ($son as $s) {
             $gsons = TMenu::find()->where(['parentid' => $s->id])->all();
             foreach ($gsons as $g) {
                 $g->delete();
             }
         }
     }
     //一二级菜单删除儿子节点
     if ($level <= 2) {
         $son = TMenu::find()->where(['parentid' => $id])->all();
         foreach ($son as $s) {
             $s->delete();
         }
     }
     //删除自身
     TMenu::findOne($id)->delete();
     Yii::$app->session->setFlash('success');
     return $this->redirect(['sys/menu']);
 }
Пример #2
0
 /**
  * 给角色分配权限
  * @return string
  */
 public function actionAssignauth()
 {
     if (Yii::$app->request->isPost) {
         $posts = Yii::$app->request->post();
         $auth = Yii::$app->authManager;
         $role = $auth->getRole($posts['rolename']);
         $thismenu = TMenu::findOne($posts['menuid']);
         $route = $thismenu->route;
         $permission = $auth->getPermission($route);
         if ($posts['ck'] == 'true') {
             if ($posts['level'] == 3) {
                 //2级菜单
                 $father = $thismenu->father;
                 $fpermission = $auth->getPermission($father->route);
                 $this->addChild($role, $fpermission);
                 //1级菜单
                 $this->addChild($role, $auth->getPermission($father->father->route));
             }
             if ($posts['level'] == 2) {
                 //1级菜单
                 $fpermission = $auth->getPermission($thismenu->father->route);
                 $this->addChild($role, $fpermission);
                 //3级菜单
                 $children = $thismenu->son;
                 foreach ($children as $son) {
                     $this->addChild($role, $auth->getPermission($son->route));
                 }
             }
             if ($posts['level'] == 1) {
                 //子子孙孙都加权限
                 $sons = $thismenu->son;
                 foreach ($sons as $son) {
                     $this->addChild($role, $auth->getPermission($son->route));
                     if ($son->level == 2) {
                         $gsons = $son->son;
                         foreach ($gsons as $gson) {
                             $this->addChild($role, $auth->getPermission($gson->route));
                         }
                     }
                 }
             }
             //自身加入权限
             $auth->addChild($role, $permission);
         } else {
             if ($posts['level'] == 3 && $posts['cntlv3'] == 0) {
                 $father = $thismenu->father;
                 $auth->removeChild($role, $auth->getPermission($father->route));
                 if ($posts['cntlv3'] == 0) {
                     $auth->removeChild($role, $auth->getPermission($father->route));
                 }
                 if ($posts['cntlv2'] == 0) {
                     $auth->removeChild($role, $auth->getPermission($father->father->route));
                 }
             }
             if ($posts['level'] == 2) {
                 foreach ($thismenu->son as $son) {
                     $auth->removeChild($role, $auth->getPermission($son->route));
                 }
                 if ($posts['cntlv2'] == 0) {
                     $auth->removeChild($role, $auth->getPermission($thismenu->father->route));
                 }
             }
             if ($posts['level'] == 1) {
                 foreach ($thismenu->son as $son) {
                     $auth->removeChild($role, $auth->getPermission($son->route));
                     foreach ($son->son as $gson) {
                         $auth->removeChild($role, $auth->getPermission($gson->route));
                     }
                 }
             }
             //删除自身
             $auth->removeChild($role, $permission);
         }
     }
     $list = TMenu::find()->where('level=1')->all();
     $rolename = Yii::$app->request->get('rolename');
     $model = AuthItem::findOne($rolename);
     return $this->render('assignauth', ['list' => $list, 'rolename' => $rolename, 'role' => Yii::$app->authManager->getRole($rolename), 'model' => $model]);
 }
Пример #3
0
 /**
  * 生成菜单
  * @return string
  */
 public static function generateMenuByUser()
 {
     $list = TMenu::find()->where('level=1')->all();
     $menu = Yii::$app->controller->renderPartial('@backend/views/home/_menu', ['list' => $list, 'admin' => Yii::$app->user->id == 1 ? true : false]);
     return $menu;
 }