beforeAction() public method

The method will trigger the [[EVENT_BEFORE_ACTION]] event. The return value of the method will determine whether the action should continue to run. In case the action should not run, the request should be handled inside of the beforeAction code by either providing the necessary output or redirecting the request. Otherwise the response will be empty. If you override this method, your code should look like the following: php public function beforeAction($action) { if (!parent::beforeAction($action)) { return false; } your custom code here return true; // or false to not run the action }
public beforeAction ( Action $action ) : boolean
$action Action the action to be executed.
return boolean whether the action should continue to be executed.
Example #1
2
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         // Запретим доступ неавторизованным пользователям
         //            TODO убрать запрет и сделать нормальные поведения
         if (Yii::$app->getUser()->isGuest) {
             throw new ForbiddenHttpException('У вас нет прав просматривать данную страницу.');
         }
         if (isset($action->controller->breadcrumbItems)) {
             $action->controller->addBreadcrumbsItem(['label' => 'Личный кабинет', 'url' => ['/cabinet']]);
             $items = $action->controller->breadcrumbItems[$action->id];
             if ($items === 'not add') {
                 // Просто выходим и разрешаем выолнять Action
                 return true;
             }
             if (is_array($items)) {
                 foreach ($items as $item) {
                     if (isset($item['url'])) {
                         $action->controller->addBreadcrumbsItem(['label' => $item['label'], 'url' => $item['url']]);
                     } else {
                         $action->controller->addBreadcrumbsItem(['label' => $item['label']]);
                     }
                 }
             } else {
                 $action->controller->addBreadcrumbsItem(['label' => $items]);
             }
         } else {
             $action->controller->addBreadcrumbsItem(['label' => 'Личный кабинет']);
         }
         return true;
         // or false if needed
     } else {
         return false;
     }
 }
Example #2
0
 public function beforeAction($action)
 {
     if ($this->beforeAction !== null && !call_user_func($this->beforeAction, $action)) {
         return false;
     }
     return parent::beforeAction($action);
 }
Example #3
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     return true;
 }
Example #4
0
 public function beforeAction($action)
 {
     $action->controller->layout = '//doctor';
     $action->controller->view->registerJsFile('/jsLib/prj/doctor.js', ['position' => \yii\web\View::POS_END]);
     return parent::beforeAction($action);
     // TODO: Change the autogenerated stub
 }
Example #5
0
 /**
  **在请求交由action处理之前,判断用户属性,如果当前用户没有登录,或者登录用户没有管理员权限,那么抛出403异常,即只有管理员才能进入该管理模块.
  * @param \yii\base\Action $action
  * @return bool
  * @throws HttpException
  */
 public function beforeAction($action)
 {
     if (!User::getCurrent() || !Admin::getCurrent()) {
         throw new HttpException(403, 'You are not an admin');
     }
     return parent::beforeAction($action);
 }
 public function beforeAction($action)
 {
     parent::beforeAction($action);
     if (Yii::$app->user->isGuest || Yii::$app->user->status == 'user') {
         Yii::$app->getResponse()->redirect(Yii::$app->user->loginUrl);
     }
 }
Example #7
0
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         return $this->checkAccess($action);
     }
     return false;
 }
Example #8
0
 /**
  **在请求交由action处理之前,判断用户属性,如果当前用户没有登录,则将页面跳转到登录页面,即该模块的所有操作都需要在用户登录状态下进行.
  * @param \yii\base\Action $action
  * @return bool|\yii\web\Response
  * @throws \yii\web\ForbiddenHttpException
  */
 public function beforeAction($action)
 {
     if (!User::getCurrent()) {
         return Yii::$app->user->loginRequired();
     }
     return parent::beforeAction($action);
 }
Example #9
0
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         //update users online information
         $this->updateOnlineStatus($action);
         // register visit by webspider
         if (isset($_SERVER['HTTP_USER_AGENT'])) {
             $spider = YBoardSpider::find()->where(['user_agent' => $_SERVER['HTTP_USER_AGENT']])->one();
         } else {
             $spider = null;
         }
         if ($spider != null) {
             $spider->setScenario('visit');
             $spider->hits++;
             $spider->last_visit = null;
             $spider->save();
         }
         //menu fixed for Views
         $approvals1 = YBoardPost::find()->unapprovedScope()->count();
         $approvals2 = YBoardTopic::find()->andWhere(['approved' => 0])->count();
         $reports = YBoardMessage::find()->reportScope()->unreadScope()->count();
         $this->params['foroMenu'] = [['label' => Yii::t('app', 'Members'), 'url' => ['member/index']], ['label' => Yii::t('app', 'Pending') . ' (' . ($approvals1 + $approvals2) . ')', 'url' => ['moderator/approve'], 'visible' => Yii::$app->user->can('moderator')], ['label' => Yii::t('app', 'Reported') . ' (' . $reports . ')', 'url' => ['moderator/reported'], 'visible' => Yii::$app->user->can('moderator')]];
         return true;
     } else {
         return false;
     }
 }
 /**
  * Runs an action within this controller with the specified action ID and parameters.
  * If the action ID is empty, the method will use [[defaultAction]].
  * @param string $id the ID of the action to be executed.
  * @param array $params the parameters (name-value pairs) to be passed to the action.
  * @return mixed the result of the action.
  * @throws InvalidRouteException if the requested action ID cannot be resolved into an action successfully.
  * @see createAction()
  */
 public function runAction($id, $params = [])
 {
     $action = $this->createAction($id);
     if ($action !== null) {
         Yii::trace("Route to run: " . $action->getUniqueId(), __METHOD__);
         if (Yii::$app->requestedAction === null) {
             Yii::$app->requestedAction = $action;
         }
         $oldAction = $this->action;
         $this->action = $action;
         $result = null;
         $event = new ActionEvent($action);
         Yii::$app->trigger(Application::EVENT_BEFORE_ACTION, $event);
         if ($event->isValid && $this->module->beforeAction($action) && $this->beforeAction($action)) {
             $result = $action->runWithParams($params);
             $this->afterAction($action, $result);
             $this->module->afterAction($action, $result);
             $event = new ActionEvent($action);
             $event->result =& $result;
             Yii::$app->trigger(Application::EVENT_AFTER_ACTION, $event);
         }
         $this->action = $oldAction;
         return $result;
     } else {
         throw new InvalidRouteException('Unable to resolve the request: ' . $this->getUniqueId() . '/' . $id);
     }
 }
 public function beforeAction($action)
 {
     if ($this->getIsInstalled() == true) {
         \Yii::$app->getResponse()->redirect("admin.php");
         return false;
     }
     return parent::beforeAction($action);
 }
Example #12
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     $adm = Adm::register();
     if (!parent::beforeAction($action) || !$adm->user->can('AdmRoot')) {
         return false;
     }
     return true;
 }
Example #13
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     $this->resetGlobalSettings();
     return true;
 }
Example #14
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if ($this->checkAccess()) {
         return parent::beforeAction($action);
     } else {
         throw new ForbiddenHttpException('You are not allowed to access this page.');
     }
 }
Example #15
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     // Block installer, when it's marked as installed
     if (Yii::$app->params['installed']) {
         throw new HttpException(500, 'Application is already installed!');
     }
     Yii::$app->controller->enableCsrfValidation = false;
     return parent::beforeAction($action);
 }
Example #16
0
 public function beforeAction($action)
 {
     $aId = $action->id;
     if ($aId == "register" && !$this->enableRegister) {
         throw new \yii\web\NotFoundHttpException("Page not found");
     } else {
         return parent::beforeAction($action);
     }
 }
Example #17
0
 public function beforeAction($action)
 {
     $this->setPluginViewPath();
     if (!parent::beforeAction($action)) {
         return false;
     }
     return true;
     // or false to not run the action
 }
Example #18
0
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         // Place access checking code here, if needed.
         return true;
     } else {
         return false;
     }
 }
Example #19
0
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         // this method is called before any module controller action is performed
         // you may place customized code here
         return true;
     } else {
         return false;
     }
 }
Example #20
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     $adm = Adm::register();
     //required load adm,if use adm layout
     /*if (!parent::beforeAction($action) || !$adm->user->can('AdmRoot')) {
           throw new ForbiddenHttpException('Access denied');
           return false;
       }*/
     return parent::beforeAction($action);
 }
Example #21
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess($action)) {
         throw new ForbiddenHttpException('You are not allowed to access this page.');
     }
     return true;
 }
Example #22
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     $route = Yii::$app->controller->id . '/' . $action->id;
     if (!$this->checkAccess() && $route !== 'site/error' && $route !== 'site/login' && $route !== 'site/logout') {
         throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to access this page.'));
     } else {
         $this->checkAccess();
         return parent::beforeAction($action);
     }
 }
Example #23
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         $contentType = 'application/json';
         if (!isset(Yii::$app->getRequest()->parsers[$contentType])) {
             Yii::$app->getRequest()->parsers[$contentType] = 'yii\\web\\JsonParser';
         }
         return true;
     }
     return false;
 }
Example #24
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     if (Yii::$app instanceof Application && !$this->checkAccess()) {
         throw new ForbiddenHttpException('You are not allowed to access this page.');
     }
     $this->resetGlobalSettings();
     return true;
 }
Example #25
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     $route = Yii::$app->controller->id . '/' . $action->id;
     if (!$this->checkAccess($route)) {
         throw new ForbiddenHttpException('需要登录');
     }
     if (!$this->checkRole()) {
         throw new ForbiddenHttpException('当前角色没有访问该模块的权限。');
     }
     return parent::beforeAction($action);
 }
Example #26
0
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     if ($this->checkAccess()) {
         $this->resetGlobalSettings();
         return true;
     } else {
         throw new ForbiddenHttpException('You are not allowed to access this page.');
     }
 }
Example #27
0
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (parent::beforeAction($action)) {
         $definitions = (require __DIR__ . '/definitions.php');
         foreach ($definitions as $name => $definition) {
             Yii::$container->set($name, $definition);
         }
         $hooks = (require __DIR__ . '/hooks.php');
         Yii::$app->attachBehaviors(array_combine($hooks, $hooks));
         return true;
     }
     return false;
 }
Example #28
0
 /**
  * 
  */
 public function beforeAction($action)
 {
     parent::beforeAction($action);
     if ($this->getIsConfigured()) {
         return true;
     }
     list($ctrl) = explode('/', $this->configRoute);
     if ($ctrl == \Yii::$app->controller->id) {
         return true;
     } else {
         \Yii::$app->response->redirect(['/' . \Yii::$app->controller->module->id . '/' . $this->configRoute]);
     }
 }
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     if (!parent::beforeAction($action)) {
         return false;
     }
     if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
         throw new ForbiddenHttpException('You are not allowed to access this page.');
     }
     if (!$this->checkUser()) {
         $this->userModule->get('user')->loginRequired();
         return false;
     }
     $this->resetGlobalSettings();
     return true;
 }
Example #30
-1
 /**
  * @inheritdoc
  */
 public function beforeAction($action)
 {
     $adm = Adm::register();
     //required load adm,if use adm layout
     $adm->params['left-menu-active'][] = 'admlivechat';
     return parent::beforeAction($action);
 }