The main problem is that the user cant login the site without the remember the option true. I have modified login function to check the user access and redirect to their particular action but it doesnt work if the user dont choose the remember me option at the time of login.Here is my code of login action.
if(isset($_POST['LoginForm']))
{
$model->attributes=$_POST['LoginForm'];
if($model->validate() && $model->login()){
$user_id = Yii::app()->user->id;
$record=Users::model()->findByPk($user_id);
if($record->masrole->name == 'Admin'){
$this->redirect(array('//users/admin'));}
if($record->masrole->name == 'Merchant'){ $this->redirect(array('//users/Description','user'=>$record->username));
}
}
}
and here is my config file.
'components'=>array(
'user'=>array(
'class' => 'WebUser',
'allowAutoLogin'=>true,
'autoRenewCookie' => true,
'identityCookie' => array('domain' => '.xxxxx.com'),
'loginUrl'=>'http://xxxxx.com/login',
),
'session' => array(
'class' => 'CDbHttpSession',
'cookieParams' => array('domain' => '.xxxxxx.com'),
'timeout' => 3600,
'connectionID' => 'db',
'sessionName' => 'session',
),
And this is my model login function
Public function login()
{
if($this->_identity===null)
{
$this->_identity=new UserIdentity($this->email,$this->password);
$this->_identity->authenticate();
}
if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
{
$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
Yii::app()->user->login($this->_identity,$duration);
return true;
}
else{
return false;
}
}
This is Web User
class WebUser extends CWebUser {
private $_model;
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 1;
}
function isMerchant(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 2;
}
}
// Load user model.
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=Users::model()->findByPk($id);
}
return $this->_model;
}
}
This is User Identity
class UserIdentity extends CUserIdentity
{
private $_id;
public function authenticate()
{
$ctiteria = new CDbCriteria;
$ctiteria->condition = "email = '".$this->username."' OR username = '".$this->username."'";
$record=Users::model()->find($ctiteria);
if($record===null)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if ($record->status != 1)
$this->errorCode=self::ERROR_USERNAME_INVALID;
else if($record->password !== md5($this->password))
$this->errorCode=self::ERROR_PASSWORD_INVALID;
else
{
$this->_id = $record->id;
$this->setState('title', $record->email);
$this->errorCode=self::ERROR_NONE;
}
return !$this->errorCode;
}
public function getId()
{
return $this->_id;
}
}
function isAdmin(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 1;
}
function isMerchant(){
$user = $this->loadUser(Yii::app()->user->id);
return intval($user->mas_role_id) == 2;
}
protected function loadUser($id=null)
{
if($this->_model===null)
{
if($id!==null)
$this->_model=Users::model()->findByPk($id);
}
return $this->_model;
}
}