|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\auth\controllers; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\web\ForbiddenHttpException; |
|
7
|
|
|
use app\modules\auth\services\ConfirmEmail; |
|
8
|
|
|
use app\modules\auth\models\forms\{ |
|
9
|
|
|
LoginForm, |
|
10
|
|
|
SignupForm, |
|
11
|
|
|
PasswordResetRequestForm, |
|
12
|
|
|
ResetPasswordForm |
|
13
|
|
|
}; |
|
14
|
|
|
|
|
15
|
|
|
class IndexController extends \yii\web\Controller |
|
16
|
|
|
{ |
|
17
|
|
|
private $confirmEmail; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct($id, $module, ConfirmEmail $confirmEmail, $config = []) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->confirmEmail = $confirmEmail; |
|
22
|
|
|
|
|
23
|
|
|
parent::__construct($id, $module, $config); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @inheritdoc |
|
28
|
|
|
*/ |
|
29
|
|
|
public function behaviors() |
|
30
|
|
|
{ |
|
31
|
|
|
return [ |
|
32
|
|
|
'access' => [ |
|
33
|
|
|
'class' => 'yii\filters\AccessControl', |
|
34
|
|
|
'rules' => [ |
|
35
|
|
|
[ |
|
36
|
|
|
'actions' => [ |
|
37
|
|
|
'login', |
|
38
|
|
|
'signup', |
|
39
|
|
|
'request-password-reset', |
|
40
|
|
|
], |
|
41
|
|
|
'allow' => true, |
|
42
|
|
|
'roles' => ['?'], |
|
43
|
|
|
], |
|
44
|
|
|
[ |
|
45
|
|
|
'actions' => [ |
|
46
|
|
|
'confirm-request', |
|
47
|
|
|
], |
|
48
|
|
|
'allow' => true, |
|
49
|
|
|
'roles' => ['@'], |
|
50
|
|
|
], |
|
51
|
|
|
], |
|
52
|
|
|
], |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function actionLogin() |
|
57
|
|
|
{ |
|
58
|
|
|
if (!Yii::$app->user->isGuest) { |
|
59
|
|
|
return $this->redirect(['/']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$model = new LoginForm(); |
|
63
|
|
|
|
|
64
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
|
65
|
|
|
return $this->redirect(['/']); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $this->render('login', [ |
|
69
|
|
|
'model' => $model, |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function actionSignup() |
|
74
|
|
|
{ |
|
75
|
|
|
$model = new SignupForm(); |
|
76
|
|
|
|
|
77
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
|
78
|
|
|
$model->signup(); |
|
79
|
|
|
|
|
80
|
|
|
Yii::$app->session->setFlash( |
|
81
|
|
|
'success', |
|
82
|
|
|
Yii::t( |
|
83
|
|
|
'app.msg', |
|
84
|
|
|
'Please activate your account. A letter for activation was sent to {email}', |
|
85
|
|
|
['email' => $model->email] |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
return $this->redirect(['/']); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $this->render('signup', [ |
|
92
|
|
|
'model' => $model, |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function actionConfirmRequest() |
|
97
|
|
|
{ |
|
98
|
|
|
$user = Yii::$app->user->identity; |
|
99
|
|
|
if ($user->isConfirmed()) { |
|
100
|
|
|
throw new ForbiddenHttpException(Yii::t('app.msg', 'Access Denied')); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$this->confirmEmail->sendEmail($user); |
|
104
|
|
|
|
|
105
|
|
|
Yii::$app->session->setFlash( |
|
106
|
|
|
'success', |
|
107
|
|
|
Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $user->email]) |
|
108
|
|
|
); |
|
109
|
|
|
return $this->redirect(['/']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function actionConfirmEmail($token) |
|
113
|
|
|
{ |
|
114
|
|
|
$this->confirmEmail->setConfirmed($token); |
|
115
|
|
|
|
|
116
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Your account is successfully activated')); |
|
117
|
|
|
return $this->redirect(['/']); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function actionRequestPasswordReset() |
|
121
|
|
|
{ |
|
122
|
|
|
$model = new PasswordResetRequestForm(); |
|
123
|
|
|
|
|
124
|
|
|
if (Yii::$app->request->isPost) { |
|
125
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
|
126
|
|
|
$model->sendEmail(); |
|
127
|
|
|
|
|
128
|
|
|
Yii::$app->session->setFlash( |
|
129
|
|
|
'success', |
|
130
|
|
|
Yii::t('app.msg', 'We\'ve sent you an email with instructions to reset your password') |
|
131
|
|
|
); |
|
132
|
|
|
return $this->redirect(['/']); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $this->render('requestPasswordReset', [ |
|
137
|
|
|
'model' => $model, |
|
138
|
|
|
]); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function actionResetPassword($token) |
|
142
|
|
|
{ |
|
143
|
|
|
$model = new ResetPasswordForm($token); |
|
144
|
|
|
|
|
145
|
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
|
146
|
|
|
$model->resetPassword(); |
|
147
|
|
|
|
|
148
|
|
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'New password was saved')); |
|
149
|
|
|
return $this->redirect(['/']); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->render('resetPassword', [ |
|
153
|
|
|
'model' => $model, |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|