|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace yii2mod\user\models; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Model; |
|
7
|
|
|
use yii2mod\user\models\enums\UserStatus; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class PasswordResetRequestForm |
|
11
|
|
|
* |
|
12
|
|
|
* @package yii2mod\user\models |
|
13
|
|
|
*/ |
|
14
|
|
|
class PasswordResetRequestForm extends Model |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string email field for password reset |
|
18
|
|
|
*/ |
|
19
|
|
|
public $email; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @inheritdoc |
|
23
|
|
|
*/ |
|
24
|
|
|
public function rules() |
|
25
|
|
|
{ |
|
26
|
|
|
return [ |
|
27
|
|
|
['email', 'filter', 'filter' => 'trim'], |
|
28
|
|
|
['email', 'required'], |
|
29
|
|
|
['email', 'email'], |
|
30
|
|
|
['email', 'exist', |
|
31
|
|
|
'targetClass' => Yii::$app->user->identityClass, |
|
32
|
|
|
'message' => Yii::t('yii2mod.user', 'User with this email is not found.'), |
|
33
|
|
|
], |
|
34
|
|
|
['email', 'exist', |
|
35
|
|
|
'targetClass' => Yii::$app->user->identityClass, |
|
36
|
|
|
'filter' => ['status' => UserStatus::ACTIVE], |
|
37
|
|
|
'message' => Yii::t('yii2mod.user', 'Your account has been deactivated, please contact support for details.'), |
|
38
|
|
|
], |
|
39
|
|
|
]; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @inheritdoc |
|
44
|
|
|
*/ |
|
45
|
|
|
public function attributeLabels() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
'email' => Yii::t('yii2mod.user', 'Email'), |
|
49
|
|
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Sends an email with a link, for resetting the password. |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool whether the email was send |
|
56
|
|
|
*/ |
|
57
|
|
|
public function sendEmail() |
|
58
|
|
|
{ |
|
59
|
|
|
$user = UserModel::findOne(['status' => UserStatus::ACTIVE, 'email' => $this->email]); |
|
60
|
|
|
|
|
61
|
|
|
if (!empty($user)) { |
|
62
|
|
|
$user->generatePasswordResetToken(); |
|
63
|
|
|
if ($user->save()) { |
|
64
|
|
|
return Yii::$app->mailer->compose('passwordResetToken', ['user' => $user]) |
|
65
|
|
|
->setFrom([Yii::$app->params['adminEmail'] => Yii::$app->name]) |
|
66
|
|
|
->setTo($this->email) |
|
67
|
|
|
->setSubject(Yii::t('yii2mod.user', 'Password reset for {0}', Yii::$app->name)) |
|
68
|
|
|
->send(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|