Commit 6ad0d72a by Alexander Makarov

Extracted password-related code into User model methods

parent 71fba077
...@@ -97,6 +97,32 @@ class User extends ActiveRecord implements IdentityInterface ...@@ -97,6 +97,32 @@ class User extends ActiveRecord implements IdentityInterface
} }
/** /**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Security::generatePasswordHash($password);
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Security::generateRandomKey();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = '';
}
/**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
......
...@@ -3,7 +3,6 @@ namespace common\models\forms; ...@@ -3,7 +3,6 @@ namespace common\models\forms;
use common\models\User; use common\models\User;
use yii\base\Model; use yii\base\Model;
use yii\helpers\Security;
/** /**
* Password reset request form * Password reset request form
...@@ -31,6 +30,7 @@ class PasswordResetRequestForm extends Model ...@@ -31,6 +30,7 @@ class PasswordResetRequestForm extends Model
*/ */
public function sendEmail() public function sendEmail()
{ {
/** @var User $user */
$user = User::find([ $user = User::find([
'status' => User::STATUS_ACTIVE, 'status' => User::STATUS_ACTIVE,
'email' => $this->email, 'email' => $this->email,
...@@ -40,7 +40,7 @@ class PasswordResetRequestForm extends Model ...@@ -40,7 +40,7 @@ class PasswordResetRequestForm extends Model
return false; return false;
} }
$user->password_reset_token = Security::generateRandomKey(); $user->generatePasswordResetToken();
if ($user->save()) { if ($user->save()) {
return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user]) return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot']) ->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
......
...@@ -4,7 +4,6 @@ namespace common\models\forms; ...@@ -4,7 +4,6 @@ namespace common\models\forms;
use common\models\User; use common\models\User;
use yii\base\InvalidParamException; use yii\base\InvalidParamException;
use yii\base\Model; use yii\base\Model;
use yii\helpers\Security;
use Yii; use Yii;
/** /**
...@@ -27,6 +26,7 @@ class ResetPasswordForm extends Model ...@@ -27,6 +26,7 @@ class ResetPasswordForm extends Model
*/ */
public function __construct($token) public function __construct($token)
{ {
parent::__construct();
if (empty($token) || !is_string($token)) { if (empty($token) || !is_string($token)) {
throw new InvalidParamException('Password reset token cannot be blank.'); throw new InvalidParamException('Password reset token cannot be blank.');
} }
...@@ -58,8 +58,8 @@ class ResetPasswordForm extends Model ...@@ -58,8 +58,8 @@ class ResetPasswordForm extends Model
{ {
$user = $this->_user; $user = $this->_user;
if ($user->validate()) { if ($user->validate()) {
$user->password_hash = Security::generatePasswordHash($this->password); $user->setPassword($this->password);
$user->password_reset_token = ''; $user->removePasswordResetToken();
return $user->save(); return $user->save();
} else { } else {
return false; return false;
......
...@@ -3,7 +3,6 @@ namespace common\models\forms; ...@@ -3,7 +3,6 @@ namespace common\models\forms;
use common\models\User; use common\models\User;
use yii\base\Model; use yii\base\Model;
use yii\helpers\Security;
use Yii; use Yii;
/** /**
...@@ -45,10 +44,10 @@ class SignupForm extends Model ...@@ -45,10 +44,10 @@ class SignupForm extends Model
$user = new User(); $user = new User();
$user->username = $this->username; $user->username = $this->username;
$user->email = $this->email; $user->email = $this->email;
$user->password_hash = Security::generatePasswordHash($this->password); $user->setPassword($this->password);
$user->auth_key = Security::generateRandomKey(); $user->generatePasswordResetToken();
$user->role = User::ROLE_USER; $user->role = User::ROLE_USER;
$user->status = USer::STATUS_ACTIVE; $user->status = User::STATUS_ACTIVE;
if ($user->save()) { if ($user->save()) {
return $user; return $user;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment