I need to record the time that a user is log in. I extend the blog tutorial, so I have the LoginForm.php (extend CFormModel) model. But, I am not sure how can I store the time to the user model (User.php) from the LoginForm.php.
Thank you for the quick response. But, I am still not sure how to do this. I do not have a Login function on my SiteController. The login is handled by Portlet, UserLogin.php.
class UserLogin extends Portlet
{
public $title='Login';
protected function renderContent()
{
$form=new LoginForm;
if(isset($_POST['LoginForm']))
{
$form->attributes=$_POST['LoginForm'];
if($form->validate())
$this->controller->refresh();
}
$this->render('userLogin',array('form'=>$form));
}
}
While the LoginForm model do not have direct access to User model since its purpose is only for login. Should I add the save attribute in LoginForm model when the authentication is success?
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:
Yii::app()->user->login($identity);
[b]------------------------------->?? but how could I get the user object?[/b]
break;
case UserIdentity::ERROR_USERNAME_INVALID:
$this->addError('username','Username is incorrect.');
break;
case UserIdentity::ERROR_USER_ACCOUNT_DISABLED:
$this->addError('username','User Account is disabled.');
break;
default: // UserIdentity::ERROR_PASSWORD_INVALID
$this->addError('password','Password is incorrect.');
break;
}
}
}
The second one is give me the correct result (in UserIdentity.php), I did not try the first one. But, the timestamp is not correct. My Windows time is 7:17AM but in the LastLogin it is already 8:17AM ;( Even though, I already set the correct time zone in php.ini.