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
}
/**
* 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
*/
public function rules()
......
......@@ -3,7 +3,6 @@ namespace common\models\forms;
use common\models\User;
use yii\base\Model;
use yii\helpers\Security;
/**
* Password reset request form
......@@ -31,6 +30,7 @@ class PasswordResetRequestForm extends Model
*/
public function sendEmail()
{
/** @var User $user */
$user = User::find([
'status' => User::STATUS_ACTIVE,
'email' => $this->email,
......@@ -40,7 +40,7 @@ class PasswordResetRequestForm extends Model
return false;
}
$user->password_reset_token = Security::generateRandomKey();
$user->generatePasswordResetToken();
if ($user->save()) {
return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])
->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])
......
......@@ -4,7 +4,6 @@ namespace common\models\forms;
use common\models\User;
use yii\base\InvalidParamException;
use yii\base\Model;
use yii\helpers\Security;
use Yii;
/**
......@@ -27,6 +26,7 @@ class ResetPasswordForm extends Model
*/
public function __construct($token)
{
parent::__construct();
if (empty($token) || !is_string($token)) {
throw new InvalidParamException('Password reset token cannot be blank.');
}
......@@ -58,8 +58,8 @@ class ResetPasswordForm extends Model
{
$user = $this->_user;
if ($user->validate()) {
$user->password_hash = Security::generatePasswordHash($this->password);
$user->password_reset_token = '';
$user->setPassword($this->password);
$user->removePasswordResetToken();
return $user->save();
} else {
return false;
......
......@@ -3,7 +3,6 @@ namespace common\models\forms;
use common\models\User;
use yii\base\Model;
use yii\helpers\Security;
use Yii;
/**
......@@ -45,10 +44,10 @@ class SignupForm extends Model
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->password_hash = Security::generatePasswordHash($this->password);
$user->auth_key = Security::generateRandomKey();
$user->setPassword($this->password);
$user->generatePasswordResetToken();
$user->role = User::ROLE_USER;
$user->status = USer::STATUS_ACTIVE;
$user->status = User::STATUS_ACTIVE;
if ($user->save()) {
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