Advertisement
Guest User

Untitled

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