如何在Yii2中将哈希格式的用户密码保存到数据库中

4 yii yii-components yii2

我需要创建新用户.我想在DB中将密码保存为哈希格式.但我失败了好几次.

这是我的代码:

调节器

public function actionCreate()
{
    $model = new User();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}
Run Code Online (Sandbox Code Playgroud)

用户模型

public function validatePassword($password)
{
    return Security::validatePassword($password, $this->password_hash);
}


/**
 * Generates password hash from password and sets it to the model
 *
 * @param string $password
 */


public function setPassword($password)
{
    $this->password_hash = Security::generatePasswordHash($password);
}
Run Code Online (Sandbox Code Playgroud)

用户表规则

public function rules()
{
    return [
        [['c_authkey', 'inserttime'], 'required'],
        [['c_branch', 'c_roleid', 'c_active', 'c_status'], 'integer'],
        [['c_expdate', 'inserttime', 'updatetime'], 'safe'],
        [['username', 'c_desig', 'insertuser', 'updateuser'], 'string', 'max' => 50],
        [['password'], 'string', 'max' => 32],
        [['c_authkey'], 'string', 'max' => 256],
        [['c_name'], 'string', 'max' => 100],
        [['c_cellno'], 'string', 'max' => 15],
        [['username'], 'unique']
    ];
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么?什么是解决方案?

小智 5

很少有事情不清楚,但我希望给你足够的信息来解决你的问题,我没有足够的代表来添加评论.

使用时,setPassword您将获得一个大于VARCHAR(32)数据库表中当前设置的字符串.至少做到这一点VARCHAR(64).将返回的示例字符串是$2a$12$FbbEBjZVTLPZg4D/143KWu0ptEXL7iEcXpxJ.MNMl8/6L0SV5FR6u.在模型中也进行调整以使验证工作.

保存模型时,您需要告诉模型使用密码散列.

这个例子来自Yii2/Advanced应用程序.作为用户创建控制器的一部分.他们将请求发送到$model->signup()高级应用程序(Frontend Sitecontroller示例)

public function actionSignup()
{
    $model = new SignupForm();
    if ($model->load(Yii::$app->request->post())) {
        if ($user = $model->signup()) {
            if (Yii::$app->getUser()->login($user)) {
                return $this->goHome();
            }
        }
    }

    return $this->render('signup', [
        'model' => $model,
    ]);
}
Run Code Online (Sandbox Code Playgroud)

在保存或添加完整的新功能之前调整当前模型以散列密码,他们在示例中使用"注册"(模型用于注册示例).模型字段验证成功后,他们处理请求.根据您的表格更改以下字段.

public function signup()
{
    if ($this->validate()) {
        $user = new User();
        $user->username = $this->username;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->save();
        return $user;
    }

    return null;
}
Run Code Online (Sandbox Code Playgroud)

对于调试验证错误,您可以print_r($model->getErrors());在任何后面添加$model->save();

希望这能为您提供足够的信息来解决您的问题.


top*_*her 5

您正在保存未使用的密码password而不是password_hash.这可以通过ActiveRecord::beforeSave()将密码值设置为来完成password_hash.您还应该在表单中使用不同的密码字段名称password_field.

public function beforeSave($insert) {
    if(isset($this->password_field)) 
        $this->password = Security::generatePasswordHash($this->password_field);
    return parent::beforeSave($insert);
}
Run Code Online (Sandbox Code Playgroud)