0
0

SiteController.php 5.2 KB

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