forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPasswordForm.php
More file actions
executable file
·77 lines (54 loc) · 1.6 KB
/
Copy pathPasswordForm.php
File metadata and controls
executable file
·77 lines (54 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace app\models\account;
use app\models\Account;
use Yii;
class PasswordForm extends Account
{
public $old_password;
public $new_password;
public function rules()
{
return [
['old_password', 'required', 'message' => '原始密码不能为空', 'on' => 'home'],
['old_password', 'validatePassword', 'on' => 'home'],
['new_password', 'required', 'message' => '登录密码不能为空'],
];
}
/**
* 验证密码是否正确
* @param $attribute
*/
public function validatePassword($attribute)
{
$account = Account::findModel($this->id);
if(!$account->id || !$account->validatePassword($this->old_password))
{
$this->addError($attribute, '原始密码验证失败');
}
}
public function store()
{
if (!$this->validate()) {
return false;
}
// 开启事务
$transaction = Yii::$app->db->beginTransaction();
$account = Account::findModel($this->id);
$account->setPassword($this->new_password);
$account->generateAuthKey();
$account->updated_at = date('Y-m-d H:i:s');
if(!$account->save())
{
$this->addError($account->getErrorLabel(), $account->getErrorMessage());
$transaction->rollBack();
return false;
}
// 事务提交
$transaction->commit();
// 重新登录
if($this->scenario == 'home'){
Yii::$app->user->logout();
}
return true;
}
}