Пример #1
0
 public function actionIndex()
 {
     //缓存一个带有依赖的缓存
     $key = '_menu' . Yii::$app->user->id;
     if (Yii::$app->session->getFlash('reflush') || !Yii::$app->cache->get($key)) {
         //如果缓存依赖发生改变,重新生成缓存
         $dp = new ExpressionDependency(['expression' => 'count(Yii::$app->authManager->getPermissionsByUser(Yii::$app->user->id))']);
         $dp2 = new DbDependency(['sql' => 'select max(updated_at) from auth_item']);
         Yii::$app->cache->set($key, 'nothing', 0, new ChainedDependency(['dependencies' => [$dp, $dp2]]));
         //利用上面的缓存依赖生成菜单的永久缓存
         $_list = TMenu::generateMenuByUser();
         Yii::$app->cache->set('menulist-' . Yii::$app->user->id, $_list, 0);
     }
     return $this->render('index');
 }
Пример #2
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;
 }
Пример #3
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]);
 }
Пример #4
0
 /**
  * Ajax 验证菜单名称
  * @return array
  */
 public function actionAjaxvalidate()
 {
     if ($id = Yii::$app->request->post('id')) {
         $model = TMenu::findOne($id);
     } else {
         $model = new TMenu();
     }
     if (Yii::$app->request->isAjax) {
         $model->load(Yii::$app->request->post());
         Yii::$app->response->format = Response::FORMAT_JSON;
         return ActiveForm::validate($model, 'menuname');
     }
 }