first you have to override id using getId() function , I post here working code of components folder , UserIdentity file ,
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$record=Employee::model()->findByAttributes(array('E_EMAIL'=>$this->username)); // here I use Email as user name which comes from database
if($record===null)
{
$this->_id='user Null';
$this->errorCode=self::ERROR_USERNAME_INVALID;
}
else if($record->E_PASSWORD!==$this->password) // here I compare db password with passwod field
{ $this->_id=$this->username;
$this->errorCode=self::ERROR_PASSWORD_INVALID;
}
else if($record['E_STATUS']!=='Active') // here I check status as Active in db
{
$err = "You have been Inactive by Admin.";
$this->errorCode = $err;
}
else
{
$this->_id=$record['E_NAME'];
$this->setState('title', $record['E_NAME']);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
step : 2
here is code for models folder LoginForm file ,
public function authenticate($attribute,$params)
{
if(!$this->hasErrors())
{
$this->_identity=new UserIdentity($this->username,$this->password);
if(!$this->_identity->authenticate())
$this->addError('password','Incorrect username or password.');
}
}
step : 3
code for controller file ,
public function actionLogin()
{
$model=new LoginForm;
$empModel = new Employee; // for validating different user to redirect different pages
if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login())
{
$empData = $empModel->getEmpLoginData($_POST['LoginForm']['username']); // get employee data from database comparing username ( in my case email )
$eid = $empData[0]['E_ID'];
if($empData[0]['E_ROLE']=='Admin'){ // checking user is Admin or not
$this->redirect(array("admin_home_page_here")); // redirect to admin page
}else{
$this->redirect(array("user_page_here")); // redirect normal user page
}
}
}
$this->render('login',array('model'=>$model));
}