Проблема в следующем. Для хеширования пароля у меня используется crypt. При добавлении нового пользователя введенный пароль преобразуется. Это норм. Но если зайти в экшен update, то мы увидим в поле Password - хешированный код, и при изменении его и последующем сохранении-в базу он заносится как обычные символы.
Как сделать, чтобы в экшене update в поле Password выводилось перекодированный пароль их crypt в нормальный, и при обновлении его-в базу заносился криптованный.
Привожу код модели User.php
Код: Выделить всё
<?php
/**
* This is the model class for table "user".
*
* The followings are the available columns in table 'user':
* @property integer $id
* @property string $login
* @property string $username
* @property string $password
* @property string $email
* @property string $role
* @property integer $lpu_id
*/
class User extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return User the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'user';
}
/**
* @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(
array('login, username, password, email, role, lpu_id', 'required'),
array('lpu_id', 'numerical', 'integerOnly'=>true),
array('login, username, password, email, role', 'length', 'max'=>128),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, login, username, password, email, role, lpu_id', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'login' => 'Login',
'username' => 'Username',
'password' => 'Password',
'email' => 'Email',
'role' => 'Role',
'lpu_id' => 'Lpu_id',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('login',$this->login,true);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('role',$this->role,true);
$criteria->compare('lpu_id',$this->lpu_id);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
protected function beforeSave()
{
if(parent::beforeSave())
{
if($this->isNewRecord)
{
// Хешировать пароль
$hash_pass = crypt($this->password);
$this->password = $hash_pass;
}
return true;
}
return false;
}
}
Код user/update.php
Код: Выделить всё
<?php
/* @var $this UserController */
/* @var $model User */
$this->breadcrumbs=array(
'Users'=>array('index'),
$model->id=>array('view','id'=>$model->id),
'Update',
);
if(Yii::app()->user->checkAccess('admin')):
echo $this->renderPartial('_form', array('model'=>$model));
$this->menu=array(
array('label'=>'List User', 'url'=>array('index')),
array('label'=>'Create User', 'url'=>array('create')),
array('label'=>'View User', 'url'=>array('view', 'id'=>$model->id)),
array('label'=>'Manage User', 'url'=>array('admin')),
);
endif;
?>
<h1>Update User <?php echo $model->id; ?></h1>
Код: Выделить всё
<?php
/* @var $this UserController */
/* @var $model User */
/* @var $form CActiveForm */
?>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'login'); ?>
<?php echo $form->textField($model,'login',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'login'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'email'); ?>
<?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'email'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'role'); ?>
<?php echo $form->textField($model,'role',array('size'=>60,'maxlength'=>128)); ?>
<?php echo $form->error($model,'role'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'lpu_id'); ?>
<?php echo $form->textField($model,'lpu_id'); ?>
<?php echo $form->error($model,'lpu_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->