YII用户模型中修改用户信息时,密码是如何处理的

jerry Yii 2015年08月09日 收藏

当我添加或修改用户记录的时候对于处理确认密码我遇到了一些麻烦,所有我想分享一下我是怎么处理的。

场景是使用的基本的那些(系统自带),你需要有一个数据表(user)并且表中有一个密码字段(password),它使用 sha1、md5或其他加密方式加密用户密码。

面是它的工作流程: 当创建用户的时候密码需要加密并且保存,但当修改用户记录时如果使用同样的场景我们最终就会把用户加密过的密码再次加密,这不是我们想要的。我们想要的时当修改时 首先清空模型对象中的密码,把它保存到一个临时的变量中,然后检查密码是否在表单中提交,如果提交了意味着用户的密码需要修改,我们就需要对密码(现在它是纯文本未加密)加密,如果没有提交意味着我们不需要修改,我们就把临时变量的值保存到数据库。

现在让我们看一下代码 ,模型:

  1. <?php if ( ! defined('YII_PATH')) exit('No direct script access allowed'); 
  2. class User extends CActiveRecord{
  3.     // holds the password confirmation word
  4.     public $repeat_password; 
  5.     //will hold the encrypted password for update actions.
  6.     public $initialPassword; 
  7.     /**
  8.      * @return array validation rules for model attributes.     */
  9.     public function rules()
  10.     {
  11.         // NOTE: you should only define rules for those attributes that
  12.         // will receive user inputs.
  13.         return array(
  14.             //password and repeat password
  15.             array('password, repeat_password', 'required', 'on'=>'resetPassword, insert'),           
  16.              array('password, repeat_password', 'length', 'min'=>6, 'max'=>40),            array('password', 'compare', 'compareAttribute'=>'repeat_password'), 
  17.         );    }
  18.  
  19.  
  20.     public function beforeSave()
  21.     {
  22.         // in this case, we will use the old hashed password.
  23.         if(empty($this->password) && empty($this->repeat_password) && !empty($this->initialPassword))
  24.             $this->password=$this->repeat_password=$this->initialPassword; 
  25.         return parent::beforeSave();    }
  26.  
  27.     public function afterFind()
  28.     {
  29.         //reset the password to null because we don't want the hash to be shown.
  30.         $this->initialPassword = $this->password;        $this->password = null; 
  31.         parent::afterFind();    }
  32.  
  33.     public function saveModel($data=array())
  34.     {
  35.             //because the hashes needs to match
  36.             if(!empty($data['password']) && !empty($data['repeat_password']))
  37.             {
  38.                 $data['password'] = Yii::app()->user->hashPassword($data['password']);                
  39.                 $data['repeat_password'] = Yii::app()->user->hashPassword($data['repeat_password']);            }
  40.  
  41.             $this->attributes=$data; 
  42.             if(!$this->save())
  43.                 return CHtml::errorSummary($this); 
  44.          return true;    }
  45.  }

当添加用户时,我们使用 "insert" 场景,此时密码是必需的。当我们修改它的时候我们使用 "update" 场景,此时密码是可选的,提交时密码字段可以为空,并且验证也不会失败。我们可以从临时变量恢复加密后的密码。

下面时控制器中的方法是怎么做的:

  1. public function actionCreate(){
  2.     $user=new User('insert'); 
  3.     $this->saveModel($user); 
  4.     $this->setViewData(compact('user'));    $this->render('create', $this->getViewData());}
  5.  public function actionUpdate($id){
  6.     $user=$this->loadModel($id);    $user->scenario='update'; 
  7.     $this->saveModel($user); 
  8.     $this->setViewData(compact('user'));    $this->render('update', $this->getViewData());}
  9.  protected function saveModel(User $user){
  10.     if(isset($_POST['User']))
  11.     {
  12.         $this->performAjaxValidation($user);        $msg = $user->saveModel($_POST['User']);        //check $msg here
  13.     }}

下面时视图中表单的代码:

  1. <section>
  2.     <?php echo $form->labelEx($user,'password'); ?>
  3.     <div>
  4.         <?php echo $form->passwordField($user,'password',array('maxlength'=>40)); ?>
  5.         <?php echo $form->passwordField($user,'repeat_password',array('maxlength'=>40)); ?>
  6.     </div>
  7.     <?php echo $form->error($user,'password'); ?></section>