Skip to content

Commit a137a15

Browse files
author
igor-chepurnoi
committed
update account action and reset password form
1 parent c459fc3 commit a137a15

File tree

3 files changed

+15
-24
lines changed

3 files changed

+15
-24
lines changed

controllers/SiteController.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,15 @@ public function actionContact()
118118
*/
119119
public function actionAccount()
120120
{
121-
$userModel = Yii::$app->user->identity;
122-
$resetPasswordForm = new ResetPasswordForm($userModel);
121+
$resetPasswordForm = new ResetPasswordForm(Yii::$app->user->identity);
123122

124123
if ($resetPasswordForm->load(Yii::$app->request->post()) && $resetPasswordForm->resetPassword()) {
125124
Yii::$app->session->setFlash('success', Yii::t('user', 'Password has been updated.'));
126125
return $this->refresh();
127126
}
128127

129128
return $this->render('account', [
130-
'resetPasswordForm' => $resetPasswordForm,
131-
'userModel' => $userModel
129+
'resetPasswordForm' => $resetPasswordForm
132130
]);
133131
}
134132
}

models/forms/ResetPasswordForm.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use app\models\UserModel;
66
use Yii;
7-
use yii\base\InvalidConfigException;
87
use yii\base\Model;
98

109
/**
@@ -14,7 +13,7 @@
1413
class ResetPasswordForm extends Model
1514
{
1615
/**
17-
* @var string Password
16+
* @var string password
1817
*/
1918
public $password;
2019

@@ -29,19 +28,15 @@ class ResetPasswordForm extends Model
2928
private $_user;
3029

3130
/**
32-
* Construct
31+
* ResetPasswordForm constructor.
3332
*
3433
* @param UserModel $user
35-
* @param array $config name-value pairs that will be used to initialize the object properties
36-
*
37-
* @throws InvalidConfigException
34+
* @param array $config
3835
*/
39-
public function __construct($user, $config = [])
36+
public function __construct(UserModel $user, $config = [])
4037
{
4138
$this->_user = $user;
42-
if (!$this->_user) {
43-
throw new InvalidConfigException('UserModel must be set.');
44-
}
39+
4540
parent::__construct($config);
4641
}
4742

@@ -77,12 +72,11 @@ public function attributeLabels()
7772
*/
7873
public function resetPassword()
7974
{
80-
$user = $this->_user;
8175
if ($this->validate()) {
82-
$user->setPassword($this->password);
83-
return $user->save(true, ['passwordHash']);
84-
} else {
85-
return false;
76+
$this->_user->setPassword($this->password);
77+
return $this->_user->save(true, ['passwordHash']);
8678
}
79+
80+
return false;
8781
}
8882
}

views/site/account.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use yii\widgets\DetailView;
66

77
/* @var $resetPasswordForm \app\models\forms\ResetPasswordForm */
8-
/* @var $userModel \app\models\UserModel */
98

109
$this->title = Yii::t('user', 'My Account');
1110
?>
@@ -36,15 +35,15 @@
3635
</div>
3736
<div class="table-responsive">
3837
<?php echo DetailView::widget([
39-
'model' => $userModel,
38+
'model' => Yii::$app->user->identity,
4039
'attributes' => [
41-
"username",
42-
"email",
40+
'username',
41+
'email',
4342
'lastLogin:date'
4443
],
4544
]); ?>
4645
</div>
4746
</div>
4847
</div>
4948
</div>
50-
</div>
49+
</div>

0 commit comments

Comments
 (0)