Advertisement
Guest User

Untitled

a guest
Apr 8th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. <?php
  2. namespace frontend\controllers;
  3.  
  4. use Yii;
  5. use yii\base\InvalidArgumentException;
  6. use yii\web\BadRequestHttpException;
  7. use yii\web\Controller;
  8. use yii\filters\VerbFilter;
  9. use yii\filters\AccessControl;
  10. use common\models\LoginForm;
  11. use frontend\models\PasswordResetRequestForm;
  12. use frontend\models\ResetPasswordForm;
  13. use frontend\models\SignupForm;
  14. use frontend\models\ContactForm;
  15.  
  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. /**
  53. * {@inheritdoc}
  54. */
  55. public function actions()
  56. {
  57. return [
  58. 'error' => [
  59. 'class' => 'yii\web\ErrorAction',
  60. ],
  61. 'captcha' => [
  62. 'class' => 'yii\captcha\CaptchaAction',
  63. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  64. ],
  65. ];
  66. }
  67.  
  68. /**
  69. * Displays homepage.
  70. *
  71. * @return mixed
  72. */
  73. public function actionIndex()
  74. {
  75. return $this->render('index');
  76. }
  77.  
  78. /**
  79. * Logs in a user.
  80. *
  81. * @return mixed
  82. */
  83. public function actionLogin()
  84. {
  85. if (!Yii::$app->user->isGuest) {
  86. return $this->goHome();
  87. }
  88.  
  89. $model = new LoginForm();
  90. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  91. return $this->goBack();
  92. } else {
  93. $model->password = '';
  94.  
  95. return $this->render('login', [
  96. 'model' => $model,
  97. ]);
  98. }
  99. }
  100.  
  101. /**
  102. * Logs out the current user.
  103. *
  104. * @return mixed
  105. */
  106. public function actionLogout()
  107. {
  108. Yii::$app->user->logout();
  109.  
  110. return $this->goHome();
  111. }
  112.  
  113. /**
  114. * Displays contact page.
  115. *
  116. * @return mixed
  117. */
  118. public function actionContact()
  119. {
  120. $model = new ContactForm();
  121. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  122. if ($model->sendEmail(Yii::$app->params['adminEmail'])) {
  123. Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
  124. } else {
  125. Yii::$app->session->setFlash('error', 'There was an error sending your message.');
  126. }
  127.  
  128. return $this->refresh();
  129. } else {
  130. return $this->render('index', [
  131. 'model' => $model,
  132. ]);
  133. }
  134. }
  135.  
  136. /**
  137. * Displays about page.
  138. *
  139. * @return mixed
  140. */
  141. public function actionAbout()
  142. {
  143. return $this->render('about');
  144. }
  145.  
  146. /**
  147. * Signs user up.
  148. *
  149. * @return mixed
  150. */
  151. public function actionSignup()
  152. {
  153. $model = new SignupForm();
  154. if ($model->load(Yii::$app->request->post())) {
  155. if ($user = $model->signup()) {
  156. if (Yii::$app->getUser()->login($user)) {
  157. return $this->goHome();
  158. }
  159. }
  160. }
  161.  
  162. return $this->render('signup', [
  163. 'model' => $model,
  164. ]);
  165. }
  166.  
  167. /**
  168. * Requests password reset.
  169. *
  170. * @return mixed
  171. */
  172. public function actionRequestPasswordReset()
  173. {
  174. $model = new PasswordResetRequestForm();
  175. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  176. if ($model->sendEmail()) {
  177. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  178.  
  179. return $this->goHome();
  180. } else {
  181. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  182. }
  183. }
  184.  
  185. return $this->render('requestPasswordResetToken', [
  186. 'model' => $model,
  187. ]);
  188. }
  189.  
  190. /**
  191. * Resets password.
  192. *
  193. * @param string $token
  194. * @return mixed
  195. * @throws BadRequestHttpException
  196. */
  197. public function actionResetPassword($token)
  198. {
  199. try {
  200. $model = new ResetPasswordForm($token);
  201. } catch (InvalidArgumentException $e) {
  202. throw new BadRequestHttpException($e->getMessage());
  203. }
  204.  
  205. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  206. Yii::$app->session->setFlash('success', 'New password saved.');
  207.  
  208. return $this->goHome();
  209. }
  210.  
  211. return $this->render('resetPassword', [
  212. 'model' => $model,
  213. ]);
  214. }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement