Hi Everyone,
I’ve been studying ‘Agile Yii Development’ from Jeffrey Winesett and it has turned out to be a great resource.
I’ve been facing a small problem in Chapter 7, for which I haven’t found any solution yet. The author is explaining the ‘user authentication workflow’ and he shows the code for UserIdentity::authenticate() twice. Both the times the code is different. I fail to understand what is right and I feel there is some mistake in the book.
Now, initially, while explaining the components/UserIdentity.php class, he shows that the authenticate() method looks like this,
public function authenticate()
{
$users=array(
// username => password
'demo'=>'demo',
'admin'=>'admin',
);
if(!isset($users[$this->username]))
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($users[$this->username]!==$this->password)
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
$this->errorCode=self::ERROR_NONE;
return !$this->errorCode;
}
This, as we all know, is correct.
In the next few pages, while explaining how LoginForm::authenticate() works, he mentions that an instance of UserIdentity is created in it and then the :authenticate() method of UserIdentity is called as shown below,
public function authenticate($attribute,$params)
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
This is also correct, isn’t it? Finally, he says that the above :authenticate method which is called inside LoginForm::authenticate(), looks like this,
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;
}
}
}
I have not found this method anywhere in the demo application or Yii root classes. The above method again creates an instance of UserIdentity and calls :authenticate? The method is called inside <b>itself</b>?
I haven’t been able to understand the authentication work flow because of this. I strongly feel the explanation of this topic in the book is not correct.
Would really appreciate if this problem is cleared.
Cheers.