SiteController.php 5.2 KB

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