1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace app\controllers; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\filters\VerbFilter; |
7
|
|
|
use yii\filters\AccessControl; |
8
|
|
|
use yii\web\ForbiddenHttpException; |
9
|
|
|
use yii\web\NotFoundHttpException; |
10
|
|
|
use app\services\SocialAuth; |
11
|
|
|
use app\services\ConfirmEmail; |
12
|
|
|
use app\models\forms\LoginForm; |
13
|
|
|
use app\models\forms\SignupForm; |
14
|
|
|
use app\models\forms\SignupProviderForm; |
15
|
|
|
use app\models\forms\PasswordResetRequestForm; |
16
|
|
|
use app\models\forms\ResetPasswordForm; |
17
|
|
|
|
18
|
|
|
class IndexController extends \yii\web\Controller |
19
|
|
|
{ |
20
|
|
|
private $confirmEmail; |
21
|
|
|
|
22
|
45 |
|
public function __construct($id, $module, ConfirmEmail $confirmEmail, $config = []) |
23
|
|
|
{ |
24
|
45 |
|
$this->confirmEmail = $confirmEmail; |
25
|
|
|
|
26
|
45 |
|
parent::__construct($id, $module, $config); |
27
|
45 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
45 |
|
public function behaviors() |
33
|
|
|
{ |
34
|
|
|
return [ |
35
|
45 |
|
'access' => [ |
36
|
|
|
'class' => AccessControl::class, |
37
|
|
|
'only' => [ |
38
|
|
|
'auth-social', |
39
|
|
|
'logout', |
40
|
|
|
'signup', |
41
|
|
|
'signup-provider', |
42
|
|
|
'confirm-request', |
43
|
|
|
'request-password-reset', |
44
|
|
|
], |
45
|
|
|
'rules' => [ |
46
|
|
|
[ |
47
|
|
|
'actions' => [ |
48
|
|
|
'auth-social', |
49
|
|
|
'signup', |
50
|
|
|
'signup-provider', |
51
|
|
|
'request-password-reset', |
52
|
|
|
], |
53
|
|
|
'allow' => true, |
54
|
|
|
'roles' => ['?'], |
55
|
|
|
], |
56
|
|
|
[ |
57
|
|
|
'actions' => [ |
58
|
|
|
'logout', |
59
|
|
|
'confirm-request' |
60
|
|
|
], |
61
|
|
|
'allow' => true, |
62
|
|
|
'roles' => ['@'], |
63
|
|
|
], |
64
|
|
|
], |
65
|
|
|
], |
66
|
|
|
'verbs' => [ |
67
|
|
|
'class' => VerbFilter::class, |
68
|
|
|
'actions' => [ |
69
|
|
|
'logout' => ['post'], |
70
|
|
|
], |
71
|
|
|
], |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
45 |
|
public function actions() |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
45 |
|
'error' => [ |
82
|
|
|
'class' => 'yii\web\ErrorAction', |
83
|
|
|
], |
84
|
|
|
'auth-social' => [ |
85
|
45 |
|
'class' => 'yii\authclient\AuthAction', |
86
|
45 |
|
'successCallback' => [$this, 'successCallback'], |
87
|
45 |
|
'successUrl' => 'signup-provider' |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function successCallback($client) |
93
|
|
|
{ |
94
|
|
|
Yii::$app->session['authClient'] = $client; |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
public function goHome() |
98
|
|
|
{ |
99
|
6 |
|
Yii::$app->session['authClient'] = null; |
100
|
6 |
|
return Yii::$app->getResponse()->redirect(Yii::$app->getHomeUrl()); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
public function actionIndex() |
104
|
|
|
{ |
105
|
10 |
|
return $this->render('index'); |
106
|
|
|
} |
107
|
|
|
|
108
|
16 |
|
public function actionLogin() |
109
|
|
|
{ |
110
|
16 |
|
if (!Yii::$app->user->isGuest) { |
111
|
1 |
|
return $this->goHome(); |
112
|
|
|
} |
113
|
|
|
|
114
|
16 |
|
$model = new LoginForm(); |
115
|
16 |
|
if ($model->load(Yii::$app->request->post()) && $model->login()) { |
116
|
3 |
|
return $this->goBack(); |
117
|
|
|
} |
118
|
16 |
|
return $this->render('login', [ |
119
|
16 |
|
'model' => $model, |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
13 |
|
public function actionSignup() |
124
|
|
|
{ |
125
|
13 |
|
$model = new SignupForm(); |
126
|
13 |
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
127
|
2 |
|
$model->signup(); |
128
|
|
|
|
129
|
2 |
|
Yii::$app->session->setFlash( |
130
|
2 |
|
'success', |
131
|
2 |
|
Yii::t( |
132
|
2 |
|
'app.msg', |
133
|
2 |
|
'Please activate your account. A letter for activation was sent to {email}', |
134
|
2 |
|
['email' => $model->email] |
135
|
|
|
) |
136
|
|
|
); |
137
|
|
|
|
138
|
2 |
|
return $this->goHome(); |
139
|
|
|
} |
140
|
|
|
|
141
|
13 |
|
return $this->render('signup', [ |
142
|
13 |
|
'model' => $model, |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
public function actionSignupProvider() |
147
|
|
|
{ |
148
|
|
|
try { |
149
|
|
|
$socialAuth = \Yii::$container->get(SocialAuth::class, [Yii::$app->session['authClient']]); |
150
|
|
|
} catch (\Throwable $e) { |
151
|
|
|
Yii::error($e); |
152
|
|
|
return $this->goHome(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$user = $socialAuth->prepareUser(); |
156
|
|
|
if ($user === null) { |
157
|
|
|
return $this->goHome(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$model = new SignupProviderForm($user); |
161
|
|
|
|
162
|
|
|
if (!$user->isNewRecord && $user->isActive() === false) { |
163
|
|
|
$session->setFlash('error', $user->getStatusDescription()); |
|
|
|
|
164
|
|
|
return $this->goHome(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (!$user->isNewRecord && $user->isActive()) { |
168
|
|
|
$model->login(); |
169
|
|
|
return $this->goHome(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if ($model->validate() || ($model->load(Yii::$app->request->post()) && $model->validate())) { |
173
|
|
|
$model->signup(); |
174
|
|
|
$model->sendEmail(); |
175
|
|
|
Yii::$app->session->setFlash( |
176
|
|
|
'success', |
177
|
|
|
Yii::t( |
178
|
|
|
'app.msg', |
179
|
|
|
'Please activate your account. A letter for activation was sent to {email}', |
180
|
|
|
['email' => $model->email] |
181
|
|
|
) |
182
|
|
|
); |
183
|
|
|
return $this->goHome(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $this->render('signupProvider', [ |
187
|
|
|
'model' => $model |
188
|
|
|
]); |
189
|
|
|
} |
190
|
|
|
|
191
|
2 |
|
public function actionConfirmRequest() |
192
|
|
|
{ |
193
|
2 |
|
$user = Yii::$app->user->identity; |
194
|
2 |
|
if ($user->isConfirmed()) { |
195
|
1 |
|
throw new ForbiddenHttpException(Yii::t('app.msg', 'Access Denied')); |
196
|
|
|
} // @codeCoverageIgnore |
197
|
|
|
|
198
|
1 |
|
$this->confirmEmail->sendEmail($user); |
199
|
|
|
|
200
|
1 |
|
Yii::$app->session->setFlash( |
201
|
1 |
|
'success', |
202
|
1 |
|
Yii::t('app.msg', 'A letter for activation was sent to {email}', ['email' => $user->email]) |
203
|
|
|
); |
204
|
|
|
|
205
|
1 |
|
return $this->goHome(); |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
public function actionConfirmEmail($token) |
209
|
|
|
{ |
210
|
3 |
|
$this->confirmEmail->setConfirmed($token); |
211
|
|
|
|
212
|
1 |
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'Your account is successfully activated')); |
213
|
1 |
|
return $this->goHome(); |
214
|
|
|
} |
215
|
|
|
|
216
|
7 |
|
public function actionRequestPasswordReset() |
217
|
|
|
{ |
218
|
7 |
|
$model = new PasswordResetRequestForm(); |
219
|
|
|
|
220
|
7 |
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
221
|
1 |
|
$model->sendEmail(); |
222
|
|
|
|
223
|
1 |
|
Yii::$app->session->setFlash( |
224
|
1 |
|
'success', |
225
|
1 |
|
Yii::t('app.msg', 'We\'ve sent you an email with instructions to reset your password') |
226
|
|
|
); |
227
|
|
|
|
228
|
1 |
|
return $this->goHome(); |
229
|
|
|
} |
230
|
|
|
|
231
|
7 |
|
return $this->render('requestPasswordResetToken', [ |
232
|
7 |
|
'model' => $model, |
233
|
|
|
]); |
234
|
|
|
} |
235
|
|
|
|
236
|
6 |
|
public function actionResetPassword($token) |
237
|
|
|
{ |
238
|
6 |
|
$model = new ResetPasswordForm($token); |
239
|
|
|
|
240
|
6 |
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) { |
241
|
1 |
|
$model->resetPassword(); |
242
|
1 |
|
Yii::$app->session->setFlash('success', Yii::t('app.msg', 'New password was saved')); |
243
|
|
|
} |
244
|
|
|
|
245
|
6 |
|
return $this->render('resetPassword', [ |
246
|
6 |
|
'model' => $model, |
247
|
|
|
]); |
248
|
|
|
} |
249
|
|
|
|
250
|
1 |
|
public function actionLogout() |
251
|
|
|
{ |
252
|
1 |
|
Yii::$app->user->logout(); |
253
|
1 |
|
return $this->goHome(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** @see commands/MaintenanceController **/ |
257
|
2 |
|
public function actionMaintenance() |
258
|
|
|
{ |
259
|
2 |
|
if (!Yii::$app->catchAll) { |
260
|
1 |
|
throw new NotFoundHttpException(Yii::t('app.msg', 'Page not found')); |
261
|
|
|
} // @codeCoverageIgnore |
262
|
|
|
|
263
|
1 |
|
$this->layout = 'maintenance'; |
264
|
1 |
|
return $this->render('maintenance'); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: