[ 'class' => AccessControl::class, 'only' => ['logout', 'signup', 'webhook'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['webhook'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ 'logout' => ['post'], 'webhook' => ['post','head'], ], ], ]; } /** * @inheritdoc */ public function beforeAction($action) { if ($action->id == 'webhook') { $this->enableCsrfValidation = false; } return parent::beforeAction($action); } /** * {@inheritdoc} */ public function actions() { return [ 'error' => [ 'class' => \yii\web\ErrorAction::class, ], 'captcha' => [ 'class' => \yii\captcha\CaptchaAction::class, 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, ], ]; } /** * Displays homepage. * * @return mixed */ public function actionIndex() { return $this->render('index'); } /** * Logs in a user. * * @return mixed */ public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } $model->password = ''; return $this->render('login', [ 'model' => $model, ]); } /** * Logs out the current user. * * @return mixed */ public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } /** * Signs user up. * * @return mixed */ public function actionSignup() { $model = new SignupForm(); if ($model->load(Yii::$app->request->post()) && $model->signup()) { Yii::$app->session->setFlash('success', 'Thank you for registering! Snap your first meal!'); Yii::$app->user->login(User::findByEmail($model->email), 3600 * 24 * 30); return $this->response->redirect(['meal/upload']); } return $this->render('signup', [ 'model' => $model, ]); } public function actionWebhook() { Yii::$app->response->format = Response::FORMAT_JSON; if (Yii::$app->request->isHead) { Yii::$app->response->statusCode = 200; return Yii::$app->response->send(); } /** @var GeminiApiComponent $api */ $api = Yii::$app->sonar; $object = json_decode(Yii::$app->request->getRawBody()); return $api->storeInvoice($api->getInvoice($object->object_id)); } /** * Requests password reset. * * @return mixed */ public function actionRequestPasswordReset() { $model = new PasswordResetRequestForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail()) { Yii::$app->session->setFlash('success', 'Please check your email for further instructions.'); return $this->goHome(); } // Keep the same message as to not leak any data with users Yii::$app->session->setFlash('success', 'Please check your email for further instructions.'); } return $this->render('requestPasswordResetToken', [ 'model' => $model, ]); } /** * Resets password. * * @param string $token * @return mixed * @throws BadRequestHttpException */ public function actionResetPassword($token) { try { $model = new ResetPasswordForm($token); } catch (InvalidArgumentException $e) { throw new BadRequestHttpException($e->getMessage()); } if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) { $uaInfo = \donatj\UserAgent\parse_user_agent(); Yii::$app->queue->push(new EmailJob([ 'templateAlias' => EmailJob::PASSWORD_HAS_BEEN_RESET, 'email' => $model->email, 'templateModel' => [ 'name' => $model->first_name, "operating_system" => $uaInfo[PLATFORM], "browser_name" => $uaInfo[BROWSER], ] ])); Yii::$app->session->setFlash('success', 'New password saved.'); return $this->goHome(); } return $this->render('resetPassword', [ 'model' => $model, ]); } /** * Verify email address * * @param string $token * @throws BadRequestHttpException * @return yii\web\Response */ public function actionVerifyEmail($token) { try { $model = new VerifyEmailForm($token); } catch (InvalidArgumentException $e) { throw new BadRequestHttpException($e->getMessage()); } if ($model->verifyEmail()) { Yii::$app->session->setFlash('success', 'Your email has been confirmed! Upon our approval you will receive a welcome email.'); return $this->goHome(); } Yii::$app->session->setFlash('error', 'Sorry, we are unable to verify your account with provided token.'); return $this->goHome(); } /** * Resend verification email * * @return mixed */ public function actionResendVerificationEmail() { $model = new ResendVerificationEmailForm(); if ($model->load(Yii::$app->request->post())) { $model->sendEmail(); Yii::$app->session->setFlash('success', 'Check your email for further instructions.'); return $this->goHome(); } return $this->render('resendVerificationEmail', [ 'model' => $model ]); } }