Друзья, уже 2 день не могу разобраться с авторизацией. Почему не получается залогиниться?
Вот код модельки:
class User extends CActiveRecord
{
const SCENARIO_REGISTER = 'register';
const SCENARIO_LOGIN = 'login';
private $_identity;
public $password_repeat;
/**
* @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('username, password', 'required'),
array('username', 'unique'),
array('username', 'length', 'min'=>5, 'max'=>30),
array('username', 'match', 'pattern'=>'/^[A-z][\w]+$/'),
array('password', 'length', 'min'=>6, 'max'=>30),
array('password', 'authenticate', 'on'=>'login'),
array('password_repeat, email', 'required', 'on'=>'register'),
array('password_repeat', 'length', 'min'=>6, 'max'=>30),
array('password', 'compare', 'compareAttribute'=>'password_repeat', 'on'=>'register'),
array('email', 'email', 'on'=>'register'),
array('email', 'length', 'min'=>6, 'max'=>30),
array('email', 'filter', 'filter'=>'mb_strtolower'),
array('id, username, password, email, dtime_register, cookie', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
return array();
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'username' => 'Username',
'password' => 'Password',
'password_repeat' => 'Repeat password',
'email' => 'Email',
);
}
public function search()
{
// @todo Please modify the following code to remove attributes that should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('username',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('dtime_register',$this->dtime_register,true);
$criteria->compare('cookie',$this->cookie,true);
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);
}
protected function beforeSave() {
if(parent::beforeSave()) {
if($this->getIsNewRecord()) {
$this->dtime_register = time();
$this->password = md5($this->password);
}
return true;
}
return false;
}
public function authenticate() {
if(!$this->hasErrors()) {
$this->_identity = new UserIdentity($this->username, $this->password);
if(!$this->_identity->authenticate()) {
$this->addError('password', 'Incorrect username or password');
}
}
}
public function login() {
if($this->_identity === null) {
$this->_identity = new UserIdentity($this->username, $this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode === UserIdentity::ERROR_NONE) {
$duration = 3600*24*30;
Yii::app()->user->login($this->_identity, $duration);
return true;
}
else {
return false;
}
}
public function logout() {
// ...
}
}
UserIdentity:
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record = User::model()->findByAttributes(array('username'=>$this->username));
if($record === null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if(md5($this->password) !== $record->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->id;
$this->setState('username', $record->username );
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
и сам экшен а контроллере:
public function actionLogin() {
$user = new User(User::SCENARIO_LOGIN);
if(isset($_POST['User'])) {
$user->attributes = $_POST['User'];
if($user->login()) {
$this->redirect('/');
}
}
$this->render('login', array('model'=>$user));
}