да в общем то все утыкается в то что после каждого аплейта пароль и соль пересчитываются заново. не могу понять как подебить
Код: Выделить всё
<?php
/**
* This is the model class for table "vgok_user".
*
* The followings are the available columns in table 'vgok_user':
* @property integer $u_id
* @property string $u_username
* @property string $u_password
* @property string $u_salt
* @property integer $role_r_id
* @property string $u_comment
* @property string $u_created
* @property string $u_ban_date
* @property integer $u_dep
* @property integer $u_sector
* @property integer $ban
*
* The followings are the available model relations:
* @property Engine[] $engines
* @property Role $roleR
* @property Department $uDep
* @property Department $uSector
*/
class User extends CActiveRecord
{
public $new_password;
/**
* @return string the associated database table name
*/
public function tableName()
{
return 'vgok_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('u_username, u_password, u_created', 'required'),
array('role_r_id, u_dep, u_sector, ban', 'numerical', 'integerOnly' => true),
array('u_username', 'match', 'pattern' => '#^[a-zA-Z0-9_\.-]+$#', 'message' => 'Логин содержит запрещённые символы'),
array('u_username', 'unique', 'caseSensitive' => false),
array('u_username', 'length', 'max' => 45),
array('u_password, u_salt', 'length', 'max' => 255),
array('u_comment, u_ban_date', 'safe'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('u_id, u_username, u_password, u_salt, role_r_id, u_comment, u_created, u_ban_date, u_dep, u_sector, ban', '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(
'engines' => array(self::HAS_MANY, 'Engine', 'user_u_id'),
'roleR' => array(self::BELONGS_TO, 'Role', 'role_r_id'),
'uDep' => array(self::BELONGS_TO, 'Department', 'u_dep'),
'uSector' => array(self::BELONGS_TO, 'Department', 'u_sector'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'u_id' => 'ид',
'u_username' => 'логин',
'u_password' => 'пароль',
'u_salt' => 'соль',
'role_r_id' => 'роль',
'u_comment' => 'комментарий',
'u_created' => 'дата создания',
'u_ban_date' => 'дата блокировки',
'u_dep' => 'отдел',
'u_sector' => 'участок',
'ban' => 'бан',
);
}
protected function afterFind()
{
$result = Role::model()->findByPk($this->role_r_id);
if ($result)
$this->role_r_id = $result->r_name;
$new_password = $this->u_password;
}
//регистрация
//регистрация
protected function beforeSave()
{
//echo '<pre>';
//var_dump($_POST['User']['u_password']);
if ($this->ban == 1)
$this->u_ban_date = date('Y-m-d H:i:s');
else
{
$this->u_ban_date = '';
}
if ($this->isNewRecord || $new_password != $_POST['User']['u_password'])
{
$this->u_salt = self::generateSalt();
$this->u_password = $this->hashPassword($this->u_password, $this->u_salt);
}
return parent::beforeSave();
}
// После валидации присваиваем пароль и соль.
/* public function afterValidate()
{
$this->u_password = self::hashPassword($this->u_password, $this->u_salt);
if($this->isNewRecord) {
$salt = self::generateSalt();
$this->u_password = self::hashPassword($this->u_password, $salt);
$this->u_salt = $salt;
//$this->role = 'user';
}
return true;
}
*/
/**
* Checks if the given password is correct.
* @param string the password to be validated
* @return boolean whether the password is valid
*/
public function validatePassword($password)
{
return $this->hashPassword($password, $this->u_salt) === $this->u_password;
}
/**
* Generates the password hash.
* @param string password
* @param string salt
* @return string hash
*/
public function hashPassword($password, $salt)
{
return md5($salt . $password);
}
/**
* Generates a salt that can be used to generate a password hash.
* @return string the salt
*/
protected function generateSalt()
{
return uniqid('', true);
}
/////////////
/**
* Retrieves a list of models based on the current search/filter conditions.
*
* Typical usecase:
* - Initialize the model fields with values from filter form.
* - Execute this method to get CActiveDataProvider instance which will filter
* models according to data in model fields.
* - Pass data provider to CGridView, CListView or any similar widget.
*
* @return CActiveDataProvider the data provider that can return the models
* based on the search/filter conditions.
*/
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('u_id', $this->u_id);
$criteria->compare('u_username', $this->u_username, true);
$criteria->compare('u_password', $this->u_password, true);
$criteria->compare('u_salt', $this->u_salt, true);
$criteria->compare('role_r_id', $this->role_r_id);
$criteria->compare('u_comment', $this->u_comment, true);
$criteria->compare('u_created', $this->u_created, true);
$criteria->compare('u_ban_date', $this->u_ban_date, true);
$criteria->compare('u_dep', $this->u_dep);
$criteria->compare('u_sector', $this->u_sector);
$criteria->compare('ban', $this->ban);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
/**
* Returns the static model of the specified AR class.
* Please note that you should have this exact method in all your CActiveRecord descendants!
* @param string $className active record class name.
* @return User the static model class
*/
public static function model($className = __CLASS__)
{
return parent::model($className);
}
}