SiteController.php 3.58 KB
Newer Older
1 2
<?php

3
namespace frontend\controllers;
4 5 6

use Yii;
use yii\web\Controller;
7 8
use common\models\LoginForm;
use frontend\models\ContactForm;
9
use common\models\User;
10
use yii\web\BadRequestHttpException;
11
use yii\helpers\Security;
12 13 14

class SiteController extends Controller
{
15 16
	public function behaviors()
	{
17 18
		return [
			'access' => [
19
				'class' => \yii\web\AccessControl::className(),
20
				'only' => ['logout', 'signup'],
21 22
				'rules' => [
					[
23
						'actions' => ['signup'],
24
						'allow' => true,
25 26 27 28
						'roles' => ['?'],
					],
					[
						'actions' => ['logout'],
29
						'allow' => true,
30 31 32 33 34
						'roles' => ['@'],
					],
				],
			],
		];
35 36
	}

37 38
	public function actions()
	{
39 40
		return [
			'error' => [
41
				'class' => 'yii\web\ErrorAction',
42 43
			],
			'captcha' => [
Qiang Xue committed
44
				'class' => 'yii\captcha\CaptchaAction',
45
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
46 47
			],
		];
48 49 50 51
	}

	public function actionIndex()
	{
52
		return $this->render('index');
53 54 55 56
	}

	public function actionLogin()
	{
57 58 59 60
		if (!\Yii::$app->user->isGuest) {
			$this->goHome();
		}

61
		$model = new LoginForm();
62
		if ($model->load($_POST) && $model->login()) {
63
			return $this->goBack();
64
		} else {
65
			return $this->render('login', [
66
				'model' => $model,
67
			]);
68 69 70 71 72
		}
	}

	public function actionLogout()
	{
73
		Yii::$app->user->logout();
Qiang Xue committed
74
		return $this->goHome();
75 76 77 78 79
	}

	public function actionContact()
	{
		$model = new ContactForm;
80
		if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
81
			Yii::$app->session->setFlash('success', 'Thank you for contacting us. We will respond to you as soon as possible.');
82
			return $this->refresh();
83
		} else {
84
			return $this->render('contact', [
85
				'model' => $model,
86
			]);
87 88 89 90 91
		}
	}

	public function actionAbout()
	{
92
		return $this->render('about');
93
	}
94 95 96 97 98 99 100

	public function actionSignup()
	{
		$model = new User();
		$model->setScenario('signup');
		if ($model->load($_POST) && $model->save()) {
			if (Yii::$app->getUser()->login($model)) {
Qiang Xue committed
101
				return $this->goHome();
102 103 104
			}
		}

105
		return $this->render('signup', [
106
			'model' => $model,
107
		]);
108
	}
109

110
	public function actionRequestPasswordReset()
111
	{
112 113 114
		$model = new User();
		$model->scenario = 'requestPasswordResetToken';
		if ($model->load($_POST) && $model->validate()) {
115
			if ($this->sendPasswordResetEmail($model->email)) {
116
				Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.');
Qiang Xue committed
117
				return $this->goHome();
118 119
			} else {
				Yii::$app->getSession()->setFlash('error', 'There was an error sending email.');
120
			}
121
		}
122
		return $this->render('requestPasswordResetToken', [
123
			'model' => $model,
124
		]);
125
	}
126

127 128
	public function actionResetPassword($token)
	{
129
		$model = User::find([
130 131
			'password_reset_token' => $token,
			'status' => User::STATUS_ACTIVE,
132
		]);
133 134

		if (!$model) {
135
			throw new BadRequestHttpException('Wrong password reset token.');
136
		}
137 138 139 140

		$model->scenario = 'resetPassword';
		if ($model->load($_POST) && $model->save()) {
			Yii::$app->getSession()->setFlash('success', 'New password was saved.');
Qiang Xue committed
141
			return $this->goHome();
142 143
		}

144
		return $this->render('resetPassword', [
145
			'model' => $model,
146
		]);
147 148 149 150
	}

	private function sendPasswordResetEmail($email)
	{
151
		$user = User::find([
152 153
			'status' => User::STATUS_ACTIVE,
			'email' => $email,
154
		]);
155 156 157 158 159 160 161

		if (!$user) {
			return false;
		}

		$user->password_reset_token = Security::generateRandomKey();
		if ($user->save(false)) {
162
			return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
163 164 165
				->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
				->setTo($email)
				->setSubject('Password reset for ' . \Yii::$app->name)
166
				->send();
167
		}
168 169

		return false;
170
	}
171
}