SiteController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace frontend\controllers;
  3. use frontend\models\ResendVerificationEmailForm;
  4. use frontend\models\VerifyEmailForm;
  5. use Yii;
  6. use yii\base\InvalidArgumentException;
  7. use yii\web\BadRequestHttpException;
  8. use yii\web\Controller;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use common\models\LoginForm;
  12. use frontend\models\PasswordResetRequestForm;
  13. use frontend\models\ResetPasswordForm;
  14. use frontend\models\SignupForm;
  15. use frontend\models\ContactForm;
  16. /**
  17. * Site controller
  18. */
  19. class SiteController extends Controller
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function behaviors()
  25. {
  26. return [
  27. 'access' => [
  28. 'class' => AccessControl::className(),
  29. 'only' => ['logout', 'signup'],
  30. 'rules' => [
  31. [
  32. 'actions' => ['signup'],
  33. 'allow' => true,
  34. 'roles' => ['?'],
  35. ],
  36. [
  37. 'actions' => ['logout'],
  38. 'allow' => true,
  39. 'roles' => ['@'],
  40. ],
  41. ],
  42. ],
  43. 'verbs' => [
  44. 'class' => VerbFilter::className(),
  45. 'actions' => [
  46. 'logout' => ['post'],
  47. ],
  48. ],
  49. ];
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function actions()
  55. {
  56. return [
  57. 'error' => [
  58. 'class' => 'yii\web\ErrorAction',
  59. ],
  60. 'captcha' => [
  61. 'class' => 'yii\captcha\CaptchaAction',
  62. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  63. ],
  64. ];
  65. }
  66. /**
  67. * Displays homepage.
  68. *
  69. * @return mixed
  70. */
  71. public function actionIndex()
  72. {
  73. return $this->render('index');
  74. }
  75. /**
  76. * Logs in a user.
  77. *
  78. * @return mixed
  79. */
  80. public function actionLogin()
  81. {
  82. if (!Yii::$app->user->isGuest) {
  83. return $this->goHome();
  84. }
  85. $model = new LoginForm();
  86. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  87. return $this->goBack();
  88. }
  89. $model->password = '';
  90. return $this->render('login', [
  91. 'model' => $model,
  92. ]);
  93. }
  94. /**
  95. * Logs out the current user.
  96. *
  97. * @return mixed
  98. */
  99. public function actionLogout()
  100. {
  101. Yii::$app->user->logout();
  102. return $this->goHome();
  103. }
  104. /**
  105. * Displays contact page.
  106. *
  107. * @return mixed
  108. */
  109. public function actionContact()
  110. {
  111. $model = new ContactForm();
  112. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  113. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  114. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  115. } else {
  116. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  117. }
  118. return $this->refresh();
  119. }
  120. return $this->render('contact', [
  121. 'model' => $model,
  122. ]);
  123. }
  124. /**
  125. * Displays about page.
  126. *
  127. * @return mixed
  128. */
  129. public function actionAbout()
  130. {
  131. return $this->render('about');
  132. }
  133. /**
  134. * Signs user up.
  135. *
  136. * @return mixed
  137. */
  138. public function actionSignup()
  139. {
  140. $model = new SignupForm();
  141. if ($model->load(Yii::$app->request->post()) && $model->signup()) {
  142. Yii::$app->session->setFlash('success', 'Thank you for registration. Please check your inbox for verification email.');
  143. return $this->goHome();
  144. }
  145. return $this->render('signup', [
  146. 'model' => $model,
  147. ]);
  148. }
  149. /**
  150. * Requests password reset.
  151. *
  152. * @return mixed
  153. */
  154. public function actionRequestPasswordReset()
  155. {
  156. $model = new PasswordResetRequestForm();
  157. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  158. if ($model->sendEmail()) {
  159. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  160. return $this->goHome();
  161. }
  162. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  163. }
  164. return $this->render('requestPasswordResetToken', [
  165. 'model' => $model,
  166. ]);
  167. }
  168. /**
  169. * Resets password.
  170. *
  171. * @param string $token
  172. * @return mixed
  173. * @throws BadRequestHttpException
  174. */
  175. public function actionResetPassword($token)
  176. {
  177. try {
  178. $model = new ResetPasswordForm($token);
  179. } catch (InvalidArgumentException $e) {
  180. throw new BadRequestHttpException($e->getMessage());
  181. }
  182. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  183. Yii::$app->session->setFlash('success', 'New password saved.');
  184. return $this->goHome();
  185. }
  186. return $this->render('resetPassword', [
  187. 'model' => $model,
  188. ]);
  189. }
  190. /**
  191. * Verify email address
  192. *
  193. * @param string $token
  194. * @throws BadRequestHttpException
  195. * @return yii\web\Response
  196. */
  197. public function actionVerifyEmail($token)
  198. {
  199. try {
  200. $model = new VerifyEmailForm($token);
  201. } catch (InvalidArgumentException $e) {
  202. throw new BadRequestHttpException($e->getMessage());
  203. }
  204. if (($user = $model->verifyEmail()) && Yii::$app->user->login($user)) {
  205. Yii::$app->session->setFlash('success', 'Your email has been confirmed!');
  206. return $this->goHome();
  207. }
  208. Yii::$app->session->setFlash('error', 'Sorry, we are unable to verify your account with provided token.');
  209. return $this->goHome();
  210. }
  211. /**
  212. * Resend verification email
  213. *
  214. * @return mixed
  215. */
  216. public function actionResendVerificationEmail()
  217. {
  218. $model = new ResendVerificationEmailForm();
  219. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  220. if ($model->sendEmail()) {
  221. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  222. return $this->goHome();
  223. }
  224. Yii::$app->session->setFlash('error', 'Sorry, we are unable to resend verification email for the provided email address.');
  225. }
  226. return $this->render('resendVerificationEmail', [
  227. 'model' => $model
  228. ]);
  229. }
  230. }