|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\auth\models\forms; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use yii\base\Exception; |
|
7
|
|
|
use yii\web\BadRequestHttpException; |
|
8
|
|
|
use app\models\entity\User; |
|
9
|
|
|
use app\modules\auth\services\Tokenizer; |
|
10
|
|
|
|
|
11
|
|
|
class ResetPasswordForm extends \yii\base\Model |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
public $password; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \app\models\entity\User |
|
19
|
|
|
*/ |
|
20
|
|
|
private $user; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var Tokenizer |
|
23
|
|
|
*/ |
|
24
|
|
|
private $tokenizer; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates a form model given a token. |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $token |
|
30
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties |
|
31
|
|
|
* @throws \yii\base\BadRequestHttpException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($token, Tokenizer $tokenizer, $config = []) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->tokenizer = $tokenizer; |
|
36
|
|
|
|
|
37
|
|
|
if (empty($token) || !is_string($token) || !$this->tokenizer->validate($token)) { |
|
38
|
|
|
throw new BadRequestHttpException(Yii::t('app', 'Invalid link for reset password')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$this->user = User::find()->passwordResetToken($token)->one(); |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
if (!$this->user) { |
|
44
|
|
|
throw new BadRequestHttpException(Yii::t('app', 'Invalid link for reset password')); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
parent::__construct($config); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritdoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function rules() |
|
54
|
|
|
{ |
|
55
|
|
|
return [ |
|
56
|
|
|
['password', 'required'], |
|
57
|
|
|
['password', 'string', 'min' => 6], |
|
58
|
|
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @inheritdoc |
|
63
|
|
|
*/ |
|
64
|
|
|
public function attributeLabels() |
|
65
|
|
|
{ |
|
66
|
|
|
return [ |
|
67
|
|
|
'password' => Yii::t('app', 'Password'), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Resets password |
|
73
|
|
|
* |
|
74
|
|
|
* @throws Exception |
|
75
|
|
|
*/ |
|
76
|
|
|
public function resetPassword(): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->user->setPassword($this->password); |
|
79
|
|
|
$this->user->removePasswordResetToken(); |
|
80
|
|
|
$this->user->updateDateLogin(); |
|
81
|
|
|
|
|
82
|
|
|
if (!$this->user->save()) { |
|
83
|
|
|
throw new Exception(Yii::t('app', 'An error occurred while saving user')); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
Yii::$app->user->login($this->user, 3600 * 24 * 30); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.