models/User.php
Код: Выделить всё
class User extends CActiveRecord
{
public $user;
public $email;
public $password;
public $rememberMe;
public $verifyCode;
...
public function rules()
{
return array(
array('email', 'required', 'on'=>'login', 'message' => 'Е-майл не был введён'),
array('password', 'required', 'on'=>'login', 'message' => 'Пароль не был введён'),
array('password','length','max'=>20, 'on'=>'login', 'message' => 'Пароль слишком длинный, должно не более 20 символов'),
array('password', 'authenticate', 'on'=>'login'),
array('username, password', 'length', 'max'=>128, 'min' => 3, 'on'=>'registration'),
array('username', 'match', 'pattern' => '/^[A-Za-z0-9\s,]+$/u','message' => 'Логин содержит недопустимые символы.', 'on'=>'registration'),
array('username, password', 'required', 'on'=>'registration'),
array('email', 'required', 'message' => 'Е-майл не был введён', 'on'=>'registration'),
array('email', 'email', 'on'=>'registration'),
);
}
...
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$identity=new UserIdentity($this->email,$this->password);
$identity->authenticate();
switch($identity->errorCode)
{
case UserIdentity::ERROR_NONE:
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($identity,$duration);
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('email','Email is incorrect.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
public function validatePassword($password)
{
return $this->hashPassword($password)===$this->password;
}
public function hashPassword($password)
{
return md5($password);
}
protected function generateSalt()
{
return uniqid('',true);
}
components/UserIdentity.php
Код: Выделить всё
class UserIdentity extends CUserIdentity
{
protected $_id;
//protected $email;
public function authenticate()
{
$record = User::model()->find("email=? AND password=?", array($this->email,User::hashPassword($this->password) ));
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if(!$record->validatePassword($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id=$record->id;
$this->username=$record->username;
$this->email=$record->email;
$this->errorCode=self::ERROR_NONE;
}
return $this->errorCode==self::ERROR_NONE;
}
public function getId()
{
return $this->_id;
}
}
также прописал в шаблоне
Код: Выделить всё
<input name="LoginForm[email]" type="text">
Не определено свойство "UserIdentity.email".
я добавил в классе UserIdentity
protected $email;
после этого стало выдавать такую ошибку:
указывая на строчкуCDbCommand не удалось исполнить SQL-запрос: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined. The SQL statement executed was: SELECT * FROM `tbl_user` `t` WHERE email=? AND password=? LIMIT 1. Bound with :0=NULL, :1='21232f297a57a5a743894a0e4a801fc3'
полагаю что свойство $this->email имеет пустое значение, тоесть из формы не попадают данные веденные пользователем?$record = User::model()->find("email=? AND password=?", array($this->email,User::hashPassword($this->password) ));