|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yiicod\auth\controllers\behaviors; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* Auth behavior with event for controller action |
|
7
|
|
|
* @author Orlov Alexey <[email protected]> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\ActionEvent; |
|
12
|
|
|
use yii\base\Behavior; |
|
13
|
|
|
use yii\base\Event; |
|
14
|
|
|
use yii\web\BadRequestHttpException; |
|
15
|
|
|
use yiicod\auth\actions\webUser\LoginAction; |
|
16
|
|
|
use yiicod\auth\actions\webUser\LogoutAction; |
|
17
|
|
|
use yiicod\auth\actions\webUser\RequestPasswordResetAction; |
|
18
|
|
|
use yiicod\auth\actions\webUser\ResetPasswordAction; |
|
19
|
|
|
use yiicod\auth\actions\webUser\SignupAction; |
|
20
|
|
|
use yiicod\auth\events\LoginEvent; |
|
21
|
|
|
use yiicod\auth\events\RequestPasswordResetEvent; |
|
22
|
|
|
use yiicod\auth\events\ResetPasswordEvent; |
|
23
|
|
|
use yiicod\auth\events\SignupEvent; |
|
24
|
|
|
|
|
25
|
|
|
class AuthUserBehavior extends Behavior |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Assign all events |
|
29
|
|
|
*/ |
|
30
|
|
|
public function init() |
|
31
|
|
|
{ |
|
32
|
|
|
// Signup |
|
33
|
|
|
Event::on(SignupAction::class, SignupAction::EVENT_BEFORE_SIGNUP, [$this, 'beforeSignup']); |
|
34
|
|
|
Event::on(SignupAction::class, SignupAction::EVENT_AFTER_SIGNUP, [$this, 'afterSignup']); |
|
35
|
|
|
Event::on(SignupAction::class, SignupAction::EVENT_ERROR_SIGNUP, [$this, 'errorSignup']); |
|
36
|
|
|
|
|
37
|
|
|
// Login |
|
38
|
|
|
Event::on(LoginAction::class, LoginAction::EVENT_BEFORE_LOGIN, [$this, 'beforeLogin']); |
|
39
|
|
|
Event::on(LoginAction::class, LoginAction::EVENT_AFTER_LOGIN, [$this, 'afterLogin']); |
|
40
|
|
|
Event::on(LoginAction::class, LoginAction::EVENT_ERROR_LOGIN, [$this, 'errorLogin']); |
|
41
|
|
|
|
|
42
|
|
|
// Logout |
|
43
|
|
|
Event::on(LogoutAction::class, LogoutAction::EVENT_BEFORE_LOGOUT, [$this, 'beforeLogout']); |
|
44
|
|
|
Event::on(LogoutAction::class, LogoutAction::EVENT_AFTER_LOGOUT, [$this, 'afterLogout']); |
|
45
|
|
|
|
|
46
|
|
|
// ResetPassword |
|
47
|
|
|
Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_BEFORE_RESET_PASSWORD, [$this, 'beforeResetPassword']); |
|
48
|
|
|
Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_AFTER_RESET_PASSWORD, [$this, 'afterResetPassword']); |
|
49
|
|
|
Event::on(ResetPasswordAction::class, ResetPasswordAction::EVENT_ERROR_RESET_PASSWORD, [$this, 'errorResetPassword']); |
|
50
|
|
|
|
|
51
|
|
|
// RequestPasswordReset |
|
52
|
|
|
Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_BEFORE_REQUEST_PASSWORD_RESET, [$this, 'beforeRequestPasswordReset']); |
|
53
|
|
|
Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_AFTER_REQUEST_PASSWORD_RESET, [$this, 'afterRequestPasswordReset']); |
|
54
|
|
|
Event::on(RequestPasswordResetAction::class, RequestPasswordResetAction::EVENT_ERROR_REQUEST_PASSWORD_RESET, [$this, 'errorRequestPasswordReset']); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* After login action event |
|
59
|
|
|
* |
|
60
|
|
|
* @param LoginEvent $event |
|
61
|
|
|
*/ |
|
62
|
|
|
public function afterLogin($event) |
|
63
|
|
|
{ |
|
64
|
|
|
$event->action->controller->goHome(); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
Yii::$app->end(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* After signup action event |
|
71
|
|
|
* |
|
72
|
|
|
* @param SignupEvent $event |
|
73
|
|
|
*/ |
|
74
|
|
|
public function afterSignUp($event) |
|
75
|
|
|
{ |
|
76
|
|
|
if (Yii::$app->getUser()->login($event->user)) { |
|
|
|
|
|
|
77
|
|
|
$event->action->controller->goHome(); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
Yii::$app->end(); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* After RequestPasswordReset action event |
|
85
|
|
|
* |
|
86
|
|
|
* @param RequestPasswordResetEvent $event |
|
87
|
|
|
*/ |
|
88
|
|
|
public function afterRequestPasswordReset($event) |
|
89
|
|
|
{ |
|
90
|
|
|
$mailerViewPath = Yii::$app->mailer->viewPath; |
|
91
|
|
|
|
|
92
|
|
|
Yii::$app->mailer->viewPath = '@yiicod/yii2-auth/mail'; |
|
93
|
|
|
Yii::$app->mailer->compose('passwordResetToken', ['action' => $event->action, 'user' => $event->model->findUser()]) |
|
94
|
|
|
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name . ' robot']) |
|
95
|
|
|
->setTo($event->model->email) |
|
96
|
|
|
->setSubject('Password reset for ' . Yii::$app->name) |
|
97
|
|
|
->send(); |
|
98
|
|
|
|
|
99
|
|
|
Yii::$app->getSession()->setFlash('success', 'Check your email for further instructions.'); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
Yii::$app->mailer->viewPath = $mailerViewPath; |
|
102
|
|
|
|
|
103
|
|
|
$event->action->controller->goHome(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
Yii::$app->end(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* After ResetPassword action event |
|
110
|
|
|
* |
|
111
|
|
|
* @param ResetPasswordEvent $event |
|
112
|
|
|
*/ |
|
113
|
|
|
public function afterResetPassword($event) |
|
114
|
|
|
{ |
|
115
|
|
|
Yii::$app->getSession()->setFlash('success', 'New password was saved.'); |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
$event->action->controller->goHome(); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
Yii::$app->end(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Before login action event |
|
124
|
|
|
* |
|
125
|
|
|
* @param LoginEvent $event |
|
126
|
|
|
* |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
public function beforeLogin($event) |
|
130
|
|
|
{ |
|
131
|
|
|
if (false === Yii::$app->user->isGuest) { |
|
132
|
|
|
$event->action->controller->goHome(); |
|
|
|
|
|
|
133
|
|
|
Yii::$app->end(); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Before signup action event |
|
139
|
|
|
* |
|
140
|
|
|
* @param SignupEvent $event |
|
141
|
|
|
*/ |
|
142
|
|
|
public function beforeSignup($event) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Before RequestPasswordReset action event |
|
148
|
|
|
* |
|
149
|
|
|
* @param RequestPasswordResetEvent $event |
|
150
|
|
|
*/ |
|
151
|
|
|
public function beforeRequestPasswordReset($event) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Before ResetPassword action event |
|
157
|
|
|
* |
|
158
|
|
|
* @param ResetPasswordEvent $event |
|
159
|
|
|
*/ |
|
160
|
|
|
public function beforeResetPassword($event) |
|
|
|
|
|
|
161
|
|
|
{ |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* error ResetPassword action event |
|
166
|
|
|
* |
|
167
|
|
|
* @param ResetPasswordEvent $event |
|
168
|
|
|
* |
|
169
|
|
|
* @throws BadRequestHttpException |
|
170
|
|
|
*/ |
|
171
|
|
|
public function errorResetPassword($event) |
|
172
|
|
|
{ |
|
173
|
|
|
throw new BadRequestHttpException($event->e->getMessage()); |
|
|
|
|
|
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Error RequestPasswordReset action event |
|
178
|
|
|
* |
|
179
|
|
|
* @param RequestPasswordResetEvent $event |
|
180
|
|
|
*/ |
|
181
|
|
|
public function errorRequestPasswordReset($event) |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
Yii::$app->getSession()->setFlash('error', 'Sorry, we are unable to reset password for email provided.'); |
|
|
|
|
|
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param ActionEvent $event |
|
188
|
|
|
*/ |
|
189
|
|
|
public function afterLogout($event) |
|
|
|
|
|
|
190
|
|
|
{ |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param ActionEvent $event |
|
195
|
|
|
*/ |
|
196
|
|
|
public function beforeLogout($event) |
|
|
|
|
|
|
197
|
|
|
{ |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @param LoginEvent $event |
|
202
|
|
|
*/ |
|
203
|
|
|
public function errorLogin($event) |
|
|
|
|
|
|
204
|
|
|
{ |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param SignupEvent $event |
|
209
|
|
|
*/ |
|
210
|
|
|
public function errorSignup($event) |
|
|
|
|
|
|
211
|
|
|
{ |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
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: