/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = DcmdGroupRepeatCmd::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'gid' => $this->gid, 'repeat_cmd_id' => $this->repeat_cmd_id, 'utime' => $this->utime, 'ctime' => $this->ctime, 'opr_uid' => $this->opr_uid]);
     return $dataProvider;
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params, $qstr = NULL)
 {
     ///非系统用户只能查看授权的操作
     if (Yii::$app->user->getIdentity()->admin != 1) {
         $gstr = " gid in (0";
         $query = DcmdUserGroup::find()->andWhere(['uid' => Yii::$app->user->getId()])->asArray()->all();
         if ($query) {
             foreach ($query as $item) {
                 $gstr .= "," . $item['gid'];
             }
         }
         $gstr .= ")";
         $query = DcmdGroupRepeatCmd::find()->where($gstr)->asArray()->all();
         if ($qstr == NULL) {
             $qstr = " repeat_cmd_id in (0";
         } else {
             $qstr .= " and repeat_cmd_id in (0";
         }
         foreach ($query as $item) {
             $qstr .= "," . $item['repeat_cmd_id'];
         }
         $qstr .= ")";
     }
     if ($qstr) {
         $query = DcmdOprCmdRepeatExec::find()->andWhere($qstr)->orderBy('repeat_cmd_name');
     } else {
         $query = DcmdOprCmdRepeatExec::find()->orderBy('repeat_cmd_name');
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pagesize' => 20]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['repeat_cmd_id' => $this->repeat_cmd_id, 'timeout' => $this->timeout, 'repeat' => $this->repeat, 'cache_time' => $this->cache_time, 'ip_mutable' => $this->ip_mutable, 'arg_mutable' => $this->arg_mutable, 'utime' => $this->utime, 'ctime' => $this->ctime, 'opr_uid' => $this->opr_uid]);
     $query->andFilterWhere(['like', 'repeat_cmd_name', $this->repeat_cmd_name])->andFilterWhere(['like', 'opr_cmd', $this->opr_cmd])->andFilterWhere(['like', 'run_user', $this->run_user])->andFilterWhere(['like', 'ip', $this->ip])->andFilterWhere(['like', 'arg', $this->arg]);
     return $dataProvider;
 }
 public function actionRun($id, $ips = "")
 {
     ///判断用户权限
     if (Yii::$app->user->getIdentity()->admin != 1) {
         $gstr = " gid in (0";
         $query = DcmdUserGroup::find()->andWhere(['uid' => Yii::$app->user->getId()])->asArray()->all();
         foreach ($query as $item) {
             $gstr .= "," . $item['gid'];
         }
         $gstr .= ")";
         $query = DcmdGroupRepeatCmd::find()->where($gstr)->asArray()->all();
         if (count($query) == 0) {
             Yii::$app->getSession()->setFlash('error', '对不起, 你没有权限!');
             return $this->redirect(['dcmd-opr-cmd-repeat-exec/index']);
         }
     }
     $opr = $this->findModel($id);
     $change = true;
     if ($opr->arg_mutable == "0") {
         $change = false;
     }
     $arg = $this->getArg($opr->arg, $change, $opr->opr_cmd);
     if ($opr->ip_mutable == "1" && $ips != "") {
         $opr->ip = $ips;
     }
     return $this->render('run', ['opr' => $opr, 'arg' => $arg]);
 }
 public function actionAddCmd()
 {
     if (Yii::$app->user->getIdentity()->admin != 1 || Yii::$app->user->getIdentity()->sa != 1) {
         Yii::$app->getSession()->setFlash('success', NULL);
         Yii::$app->getSession()->setFlash('error', "对不起,你没有权限!");
         return $this->redirect(array('dcmd-group/index'));
     }
     $gid = Yii::$app->request->post()["gid"];
     $gname = Yii::$app->request->post()["gname"];
     $uid_array = array();
     $success_msg = "未选择操作!";
     if (array_key_exists("selection", Yii::$app->request->post())) {
         $success_msg = "添加成功:";
         $tm = date('Y-m-d H:i:s');
         foreach (Yii::$app->request->post()["selection"] as $k => $v) {
             $dcmd_group_repeat_cmd = new DcmdGroupRepeatCmd();
             $dcmd_group_repeat_cmd->repeat_cmd_id = $v;
             $dcmd_group_repeat_cmd->gid = $gid;
             $dcmd_group_repeat_cmd->utime = $tm;
             $dcmd_group_repeat_cmd->ctime = $tm;
             $dcmd_group_repeat_cmd->opr_uid = Yii::$app->user->getId();
             $dcmd_group_repeat_cmd->save();
         }
     }
     Yii::$app->getSession()->setFlash('success', $success_msg);
     return $this->redirect(array('dcmd-group/view', 'id' => $gid, 'show_div' => 'dcmd-group-repeat-cmd'));
 }
 /**
  * Deletes an existing DcmdGroup model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  * @param integer $id
  * @return mixed
  */
 public function actionDelete($id)
 {
     /*$node = DcmdUserGroup::find()->where(['gid' => $id])->one();
       if($node) {
         Yii::$app->getSession()->setFlash('error', '用户组内有用户,不可删除!');
       }else {
         $this->findModel($id)->delete();
         Yii::$app->getSession()->setFlash('success', '删除成功!');
       }*/
     if (Yii::$app->user->getIdentity()->admin != 1 || Yii::$app->user->getIdentity()->sa != 1) {
         Yii::$app->getSession()->setFlash('success', NULL);
         Yii::$app->getSession()->setFlash('error', "Sorry, You don't have priority!");
         return $this->redirect(array('index'));
     }
     DcmdUserGroup::deleteAll("gid=" . $id);
     DcmdGroupCmd::deleteAll('gid=' . $id);
     DcmdGroupRepeatCmd::deleteAll('gid=' . $id);
     $model = $this->findModel($id);
     $this->oprlog(3, "delete group:" . $model->gname);
     $model->delete();
     Yii::$app->getSession()->setFlash('success', '删除成功!');
     return $this->redirect(['index']);
 }