That’s strange but, Yii::app()->user->isGuest is always true in my application.
LoginForm extends CFormModel:
public $username;
public $password;
public $rememberMe;
/**
* Declares the validation rules.
* The rules state that username and password are required,
* and password needs to be authenticated.
*/
public function rules()
{
return array(
// username and password are required
array('username, password', 'required'),
// password needs to be authenticated
array('password', 'authenticate'),
);
}
/**
* Authenticates the password.
* This is the 'authenticate' validator as declared in rules().
*/
public function authenticate($attribute,$params)
{
if(!$this->hasErrors()) // we only want to authenticate when no input errors
{
$identity=new UserIdentity($this->username,$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('username','Username is incorrect.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
UserIdentity extends CUserIdentity:
class UserIdentity extends CUserIdentity
{
private $_id;
/**
* Authenticates a user.
* The example implementation makes sure if the username and password
* are both 'demo'.
* In practical applications, this should be changed to authenticate
* against some persistent user identity storage (e.g. database).
* @return boolean whether authentication succeeds.
*/
public function authenticate()
{
$username = strtolower($this->username);
$password = WSEncrypt::password($this->username, $this->password);
$record = User::model()->findByAttributes(array('username'=>$this->username));
if ($record === null)
{
$this->errorCode = self::ERROR_USERNAME_INVALID;
}
else if ($password != $record->password)
{
$this->errorCode = self::ERROR_PASSWORD_INVALID;
}
else {
$this->_id = $record->IDUser;
$this->errorCode = self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $thid->_id;
}
}
Maybe I work too much and do not see something obvious?
Yii::app()->user->isGuest always = 1 even after success Yii::app()->user->login($identity,$duration);