public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     Yii::$app->response->format = Response::FORMAT_JSON;
     return true;
 }
Esempio n. 2
0
 /**
  * This method is used to valide the user's authority with token.
  * This method is invoked right before an action is executed.
  *
  * The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method
  * will determine whether the action should continue to run.
  *
  * If you override this method, your code should look like the following:
  *
  * ```php
  * public function beforeAction($action)
  * {
  *     if (parent::beforeAction($action)) {
  *         // your custom code here
  *         return true;  // or false if needed
  *     } else {
  *         return false;
  *     }
  * }
  * ```
  *
  * @param Action $action the action to be executed.
  * @return boolean whether the action should continue to run.
  * @author Harry Sun
  */
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         $this->attachBehavior('ControllerBehavior', new ControllerBehavior());
         $token = $this->getAccessToken();
         return $this->checkAuth($this->module, $token);
     }
     throw new HttpException(400, "Fail to resolve the action.");
 }
Esempio n. 3
0
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     return $this->encryptDataBeforeAction();
     // your custom code here
     //return true; // or false to not run the action
 }
Esempio n. 4
0
 /**
  * @param \yii\base\Action $action
  * @return bool
  * @throws \yii\web\BadRequestHttpException
  */
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         $this->request = Yii::$app->request;
         Yii::info('请求地址:' . $this->request->absoluteUrl, 'request');
         Yii::info('请求数据:' . $this->request->rawBody, 'request');
         return true;
     } else {
         return false;
     }
 }
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     foreach ($this->validateNested as $action) {
         if ($this->action->id === $action) {
             if ($this->indexDataProvider()->getTotalCount() === 0) {
                 throw new NotFoundHttpException("Object not found");
             }
         }
     }
     return true;
 }
Esempio n. 6
0
 /**
  * @param \yii\base\Action $event
  * @return bool
  */
 public function beforeAction($event)
 {
     try {
         $valid = parent::beforeAction($event);
         if ($valid && in_array($event->id, ['create']) && !preg_match('~' . Response::FORMAT_JSON . '~', Yii::$app->request->contentType)) {
             $this->showError(406, "Content type must be '" . Response::FORMAT_JSON . "'");
             return false;
         }
         if ($valid && in_array($event->id, ['update', 'delete'])) {
             $this->showError(403, "Access denied to action '" . $event->id . "'");
             return false;
         }
         return $valid;
     } catch (Exception $e) {
         $this->showError(405, $e->getMessage());
         return false;
     }
 }
Esempio n. 7
0
 public function beforeAction($action)
 {
     $request = Yii::$app->request;
     $paras = $request->isPost ? $request->post() : $request->get();
     if (!isset($paras['access_token'])) {
         Yii::$app->getResponse()->content = array('token_status' => -1, 'last_login' => '');
         return parent::beforeAction($action);
     }
     $user1 = \app\models\User::find()->where(['access_token' => $paras['access_token']])->one();
     $user2 = \app\models\User::find()->where(['old_token' => $paras['access_token']])->one();
     $token_status = 0;
     $last_login = null;
     if (!is_null($user1)) {
         $last_login = $user1->last_login;
         $token_status = 1;
     }
     if (!is_null($user2)) {
         $last_login = $user2->last_login;
         $token_status = 2;
     }
     Yii::$app->getResponse()->content = array('token_status' => $token_status, 'last_login' => $last_login);
     //Yii::$app->getResponse()->send();
     return parent::beforeAction($action);
 }
Esempio n. 8
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     /** @var ContentNegotiator $negotiator */
     if (($negotiator = $this->getBehavior('contentNegotiator')) !== null) {
         $negotiator->negotiate();
         if (Yii::$app->response->format !== Response::FORMAT_HTML) {
             $this->enableCsrfValidation = false;
         }
     }
     return parent::beforeAction($action);
 }
 public function beforeAction($action)
 {
     $this->enableCsrfValidation = false;
     return parent::beforeAction($action);
 }
 public function beforeAction($event)
 {
     return parent::beforeAction($event);
 }
Esempio n. 11
0
 public function beforeAction($action)
 {
     $this->page_size = Yii::$app->request->get('per-page') ? Yii::$app->request->get('per-page') : $this->page_size;
     if ($action->id == 'create' || $action->id == 'update') {
         if ($this->auto_filter_user && $this->user_identifier_column) {
             $params = Yii::$app->getRequest()->getBodyParams();
             $params[$this->user_identifier_column] = strval(Yii::$app->user->id);
             Yii::$app->getRequest()->setBodyParams($params);
         }
     }
     return $this->handleBeforeActionEvent($action) && parent::beforeAction($action);
 }