Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. <?php
  2. namespace frontend\controllers;
  3.  
  4. use Yii;
  5. use yii\base\InvalidParamException;
  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('contact', [
  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. $data = new \stdClass();
  145. $data->name = 'Noxter';
  146. $data->url = 'http://simpla-addons.org';
  147. return $this->render('about.tpl', ['data'=>$data]);
  148. }
  149.  
  150. /**
  151. * Signs user up.
  152. *
  153. * @return mixed
  154. */
  155. public function actionSignup()
  156. {
  157. $model = new SignupForm();
  158. if ($model->load(Yii::$app->request->post())) {
  159. if ($user = $model->signup()) {
  160. if (Yii::$app->getUser()->login($user)) {
  161. return $this->goHome();
  162. }
  163. }
  164. }
  165.  
  166. return $this->render('signup', [
  167. 'model' => $model,
  168. ]);
  169. }
  170.  
  171. /**
  172. * Requests password reset.
  173. *
  174. * @return mixed
  175. */
  176. public function actionRequestPasswordReset()
  177. {
  178. $model = new PasswordResetRequestForm();
  179. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  180. if ($model->sendEmail()) {
  181. Yii::$app->session->setFlash('success', 'Check your email for further instructions.');
  182.  
  183. return $this->goHome();
  184. } else {
  185. Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
  186. }
  187. }
  188.  
  189. return $this->render('requestPasswordResetToken', [
  190. 'model' => $model,
  191. ]);
  192. }
  193.  
  194. /**
  195. * Resets password.
  196. *
  197. * @param string $token
  198. * @return mixed
  199. * @throws BadRequestHttpException
  200. */
  201. public function actionResetPassword($token)
  202. {
  203. try {
  204. $model = new ResetPasswordForm($token);
  205. } catch (InvalidParamException $e) {
  206. throw new BadRequestHttpException($e->getMessage());
  207. }
  208.  
  209. if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->resetPassword()) {
  210. Yii::$app->session->setFlash('success', 'New password saved.');
  211.  
  212. return $this->goHome();
  213. }
  214.  
  215. return $this->render('resetPassword', [
  216. 'model' => $model,
  217. ]);
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement