public function beforeAction(Event $event)
 {
     $controller = $event->sender->controller;
     if ($controller instanceof \yii\debug\controllers\DefaultController) {
         return false;
     }
     try {
         \Yii::$app->db->open();
     } catch (Exception $e) {
         \Yii::$app->session->setFlash('error', Elements::header(Elements::icon('warning sign') . ' Внимание! Отсутствует соединение с базой данных!'));
         echo $event->sender->controller->render('@app/views/site/warning', ['exception' => $e]);
         \Yii::$app->end();
     }
 }
 /**
  * This method is used to register new user account. If Module::enableConfirmation is set true, this method
  * will generate new confirmation token and use mailer to send it to the user.
  * @return bool
  * @throws \Exception
  * @throws \yii\db\Exception
  */
 public function register()
 {
     if ($this->getIsNewRecord() == false) {
         throw new \RuntimeException('Calling "' . __CLASS__ . '::' . __METHOD__ . '" on existing user');
     }
     if ($this->module->enableConfirmation == false) {
         $this->confirmed_at = time();
     } else {
         $this->confirmation_sent_at = time();
         $this->confirmation_token = \Yii::$app->security->generateRandomString();
     }
     $this->trigger(self::EVENT_INIT_REGISTER);
     $transaction = Users::getDb()->beginTransaction();
     try {
         if ($this->save()) {
             $this->trigger(self::EVENT_DONE_REGISTER);
             \Yii::getLogger()->log("User '{$this->username}' has been registered", Logger::LEVEL_INFO);
             $transaction->commit();
             if ($this->module->enableConfirmation) {
                 if ($this->sendConfirmation()) {
                     Yii::$app->session->setFlash('success', Elements::header(Elements::icon('thumbs up') . 'Поздравляем! На ваш email отправлено письмо с дальнейшими инструкциями.'));
                 } else {
                     Yii::$app->session->setFlash('error', Elements::header(Elements::icon('warning sign') . 'Не удалось отправить письмо для подтверждения регистрации!'));
                     return false;
                 }
                 return true;
             }
             if ($this->module->enableAutoLogon) {
                 if ($this->login()) {
                     \Yii::$app->session->setFlash('success', Elements::header(Elements::icon('thumbs up') . 'Добро пожаловать! Вы были успешно зарегистрированы и авторизованы.'));
                 } else {
                     \Yii::$app->session->setFlash('error', Elements::header(Elements::icon('warning sign') . 'Не удалось войти в аккаунт!'));
                     return false;
                 }
                 return true;
             }
             \Yii::$app->session->setFlash('success', Elements::header(Elements::icon('thumbs up') . 'Поздравляем! Аккаунт успешно создан.'));
             return true;
         }
         Yii::$app->session->setFlash('error', Elements::header(Elements::icon('warning sign') . 'Внимание! При регистрации возникла ошибка!'));
         \Yii::getLogger()->log('An error occurred while registering user account', Logger::LEVEL_ERROR);
         return false;
     } catch (\Exception $e) {
         $transaction->rollBack();
         throw $e;
     }
 }
 /**
  * Displays page where user can reset password.
  *
  * @param $id
  * @param $token
  * @return string
  * @throws \yii\web\NotFoundHttpException
  */
 public function actionReset($id, $token)
 {
     /* @var $user \users\models\Users */
     $model = new RecoveryForm(['scenario' => 'reset']);
     $user = Users::find()->where(['id' => $id, 'recovery_token' => $token])->one();
     if ($user === null || $user->getIsAttributePeriodExpired('recovery_sent_at')) {
         return $this->render('invalidToken');
     }
     if ($model->load(\Yii::$app->getRequest()->post())) {
         if ($user->resetPassword($model->password)) {
             \Yii::$app->session->setFlash('success', Elements::header(Elements::icon('thumbs up') . 'Поздравляем! Пароль успешно сброшен.'));
             return $this->render('resetFinish');
         } else {
             \Yii::$app->session->setFlash('error', Elements::header(Elements::icon('warning sign') . 'Не удалось сбросить пароль!'));
         }
     }
     return $this->render('resetPassword', ['model' => $model]);
 }