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