モデルの「パスワードの確認」フィールド

4322 ワード

ユーザーレコードを追加または変更したとき、パスワードの確認処理にいくつかのトラブルが発生しました.私がどのように処理したのかを共有したいと思っています.
シーンは、sha 1、md 5、または他の暗号化方法でユーザーパスワードを暗号化するデータテーブル(user)と、sha 1、md 5、または他の暗号化方法でパスワードフィールド(password)が必要です.
面はそのワークフローです.ユーザーを作成するときにパスワードを暗号化して保存する必要がありますが、ユーザーレコードを変更するときに同じシーンを使用すると、最終的にユーザーが暗号化したパスワードを再暗号化します.これは私たちが望んでいるものではありません.私たちが望むときは、修正するときにまずモデルオブジェクトのパスワードを空にして、一時的な変数に保存し、パスワードがフォームにコミットされているかどうかを確認します.コミットされている場合は、ユーザーのパスワードを変更する必要があります.パスワード(現在は純粋なテキストで暗号化されていません)を暗号化する必要があります.コミットされていない場合は、変更する必要はありません.一時変数の値をデータベースに保存します.
コード、モデルを見てみましょう.
<?php if ( ! defined('YII_PATH')) exit('No direct script access allowed');
 
class User extends CActiveRecord
{
    // holds the password confirmation word
    public $repeat_password;
 
    //will hold the encrypted password for update actions.
    public $initialPassword;
 
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            //password and repeat password
            array('password, repeat_password', 'required', 'on'=>'resetPassword, insert'),
            array('password, repeat_password', 'length', 'min'=>6, 'max'=>40),
            array('password', 'compare', 'compareAttribute'=>'repeat_password'),
 
        );
    }
 
 
    public function beforeSave()
    {
        // in this case, we will use the old hashed password.
        if(empty($this->password) && empty($this->repeat_password) && !empty($this->initialPassword))
            $this->password=$this->repeat_password=$this->initialPassword;
 
        return parent::beforeSave();
    }
 
    public function afterFind()
    {
        //reset the password to null because we don't want the hash to be shown.
        $this->initialPassword = $this->password;
        $this->password = null;
 
        parent::afterFind();
    }
 
    public function saveModel($data=array())
    {
            //because the hashes needs to match
            if(!empty($data['password']) && !empty($data['repeat_password']))
            {
                $data['password'] = Yii::app()->user->hashPassword($data['password']);
                $data['repeat_password'] = Yii::app()->user->hashPassword($data['repeat_password']);
            }
 
            $this->attributes=$data;
 
            if(!$this->save())
                return CHtml::errorSummary($this);
 
         return true;
    }
 
}

ユーザーを追加する場合は、「insert」シーンを使用します.パスワードが必要です.変更するときはupdateシーンを使用します.パスワードはオプションで、コミット時にパスワードフィールドが空になり、検証も失敗しません.一時変数から暗号化されたパスワードを復元できます.
次の場合、コントローラの方法はどのようにしますか.
public function actionCreate()
{
    $user=new User('insert');
 
    $this->saveModel($user);
 
    $this->setViewData(compact('user'));
    $this->render('create', $this->getViewData());
}
 
public function actionUpdate($id)
{
    $user=$this->loadModel($id);
    $user->scenario='update';
 
    $this->saveModel($user);
 
    $this->setViewData(compact('user'));
    $this->render('update', $this->getViewData());
}
 
protected function saveModel(User $user)
{
    if(isset($_POST['User']))
    {
        $this->performAjaxValidation($user);
        $msg = $user->saveModel($_POST['User']);
        //check $msg here
    }
}

次に、ビュー内のフォームのコードを示します.
<section>
    <?php echo $form->labelEx($user,'password'); ?>
    <div>
        <?php echo $form->passwordField($user,'password',array('maxlength'=>40)); ?>
        <?php echo $form->passwordField($user,'repeat_password',array('maxlength'=>40)); ?>
    </div>
    <?php echo $form->error($user,'password'); ?>
</section>

これらがあなたに役に立つことを望んでいます.
英文原文:http://www.yiiframework.com/wiki/277/model-password-confirmation-field/