コード例 #1
0
 /**
  * 保存数据
  *
  * @return array
  */
 public function actionSave()
 {
     $rule_id = \Yii::$app->request->post('rule_id');
     $url_id = \Yii::$app->request->post('url_id');
     $param_name = \Yii::$app->request->post('param_name');
     $rule = \Yii::$app->request->post('rule');
     $note = \Yii::$app->request->post('note');
     $status = \Yii::$app->request->post('status');
     if (!DpAdminMenuUrl::getByUrlId($url_id)) {
         return $this->renderError('菜单URL不存在!');
     }
     $saveData = ['url_id' => $url_id, 'param_name' => $param_name, 'rule' => $rule, 'note' => $note, 'status' => $status];
     if ($rule_id) {
         $obj = DpAdminMenuUrlRule::find()->findByRuleId($rule_id)->one();
         if (!$obj) {
             return $this->renderError('保存失败,记录不存在!');
         }
     } else {
         $obj = new DpAdminMenuUrlRule();
     }
     $obj->setAttributes($saveData);
     if (!$obj->save()) {
         foreach ($obj->getErrors() as $error) {
             foreach ($error as $message) {
                 return ['success' => false, 'msg' => $message];
             }
         }
     }
     return $this->renderSuccess('保存成功');
 }
コード例 #2
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  * @param string $formName the form name to be used for loading the data into the model.
  *
  * @return ActiveDataProvider
  */
 public function search($params, $formName = null)
 {
     $query = DpAdminMenuUrl::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params, $formName);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['url_id' => $this->url_id, 'enable_rule' => $this->enable_rule, 'status' => $this->status]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'route', $this->route])->andFilterWhere(['like', 'method', $this->method])->andFilterWhere(['like', 'host', $this->host])->andFilterWhere(['like', 'note', $this->note]);
     return $dataProvider;
 }
コード例 #3
0
 public function beforeAction($action)
 {
     $route = Yii::$app->requestedRoute;
     if ($this->identity) {
         if ($this->identity->is_super) {
             $allowAccess = true;
         } else {
             // 权限验证
             $userMenuIdList = [];
             $groupMenuIdList = [];
             if ($this->identity->is_user_access) {
                 // 用户权限列表
                 $userMenuIdList = DpAdminUserMenuRelation::getAllMenuIdArrByUserId($this->identity->user_id);
             }
             if ($this->identity->is_group_access) {
                 // 用户组权限列表
                 $groupMenuIdList = DpAdminGroup::getMenuIdArrByGroupIdArr($this->identity->getGroupIdArr());
             }
             $this->menuIdList = array_merge($userMenuIdList, $groupMenuIdList);
             $routeWhiteList = ['', 'admin/common/tree', 'admin/common/urls', 'admin/public/logout'];
             $allowAccess = in_array($route, $routeWhiteList);
             if (!$allowAccess) {
                 $queryParams = Yii::$app->request->queryParams;
                 $method = Yii::$app->request->method;
                 $urlRule = DpAdminMenuUrl::getUrlRuleByMenuIdArr($this->menuIdList);
                 $allowAccess = !!array_filter($urlRule, function ($item) use($route, $queryParams, $method) {
                     if (strpos($item['route'], '/') === 0) {
                         $ruleRoute = substr($item['route'], 1, strlen($item['route']));
                     } else {
                         $ruleRoute = $item['route'];
                     }
                     if ($ruleRoute == $route) {
                         // 请求方法验证
                         if (!in_array($method, StringHelper::explode($item['method'], ',', true, true))) {
                             return false;
                         }
                         if ($item['enable_rule']) {
                             // get参数规则验证
                             foreach ($queryParams as $qk => $qv) {
                                 if (isset($item['rule'][$qk])) {
                                     $pattern = '/' . $item['rule'][$qk] . '/';
                                     if (preg_match($pattern, $qv)) {
                                         return true;
                                     }
                                 }
                             }
                             return false;
                         } else {
                             return true;
                         }
                     }
                     return false;
                 });
             }
         }
         if (!$allowAccess) {
             // 权限不足
             $response = Yii::$app->response;
             $response->format = Response::FORMAT_JSON;
             $response->data = ['success' => false, 'msg' => '权限不足', 'code' => 2];
             return false;
         } else {
             return parent::beforeAction($action);
         }
     } else {
         $routeWhiteList = ['', 'admin/public/login', 'admin/public/logout'];
         $allowAccess = in_array($route, $routeWhiteList);
         if (!$allowAccess) {
             // 未登录
             $response = Yii::$app->response;
             $response->format = Response::FORMAT_JSON;
             $response->data = ['success' => false, 'msg' => '请先登录系统', 'code' => 1];
             return false;
         }
     }
     return parent::beforeAction($action);
 }
コード例 #4
0
 /**
  * 获取菜单url的关联数据
  *
  * @return \yii\db\ActiveQuery
  */
 public function getMenuUrl()
 {
     return $this->hasOne(DpAdminMenuUrl::className(), ['url_id' => 'url_id']);
 }
コード例 #5
0
 /**
  * 保存数据
  *
  * @return array
  */
 public function actionSave()
 {
     $link_id = \Yii::$app->request->post('link_id');
     $menu_id = \Yii::$app->request->post('menu_id');
     $url_id = \Yii::$app->request->post('url_id');
     $status = \Yii::$app->request->post('status');
     if (!DpAdminMenuUrl::getByUrlId($url_id)) {
         return $this->renderError('菜单URL不存在!');
     }
     $saveData = ['menu_id' => $menu_id, 'url_id' => $url_id, 'status' => $status];
     if ($link_id) {
         $obj = DpAdminMenuUrlRelation::find()->findByLinkId($link_id)->one();
         if (!$obj) {
             return $this->renderError('保存失败,记录不存在!');
         }
     } else {
         $obj = new DpAdminMenuUrlRelation();
     }
     $obj->setAttributes($saveData);
     if (!$obj->save()) {
         foreach ($obj->getErrors() as $error) {
             foreach ($error as $message) {
                 return ['success' => false, 'msg' => $message];
             }
         }
     }
     return $this->renderSuccess('保存成功');
 }
コード例 #6
0
 /**
  * 更新状态
  *
  * @return array
  */
 public function actionUpdateStatus()
 {
     $ids = \Yii::$app->request->post('ids');
     $status = intval(\Yii::$app->request->post('status'));
     if ($status != 0) {
         $status = 1;
     }
     foreach (StringHelper::explode($ids, ',', true, true) as $id) {
         $obj = DpAdminMenuUrl::find()->findByUrlId($id)->one();
         if ($obj) {
             $obj->status = $status;
             $obj->save();
         }
     }
     return $this->renderSuccess('状态更新成功');
 }