Yii Framework Forum: Problem-Users Cant Login Without Setting The Remember Me Option - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Problem-Users Cant Login Without Setting The Remember Me Option Rate Topic: -----

#1 User is offline   marjss 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 16-January 13

Posted 21 April 2013 - 11:55 PM

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;
    }
}

0

#2 User is offline   artificial 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 22 April 2013 - 01:41 AM

It looks like your issue is most directly related to this line.

$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days


When the rememberMe field is set, the duration is set to 30 days, when not set, 0 days. That's fine in itself, as if a value greater then 0 is passed through (remember me set), a cookie based authentication will be used, and if not, a session based authentication will be used. Seeing as how you've seemingly overrode some of the session configuration in your config file, I'd double check sessions are all working properly, as I'm betting that's where the problem is.
Alex Berriman
www.fishvision.com
0

#3 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,707
  • Joined: 04-March 10
  • Location:UK

Posted 22 April 2013 - 04:28 AM

Try clearing all of your browser's cookies for the domains that you've specified and any that you've used to access the site. If you've changed the cookie domain, old cookies may cause conflicts.
0

#4 User is offline   marjss 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 16-January 13

Posted 22 April 2013 - 04:35 AM

View PostKeith, on 22 April 2013 - 04:28 AM, said:

Try clearing all of your browser's cookies for the domains that you've specified and any that you've used to access the site. If you've changed the cookie domain, old cookies may cause conflicts.

Already cleared the cookies and and i didnt changed the cookie domain..I've already tried on another machine (to make sure there is no cookie or cache conflict).
0

#5 User is offline   marjss 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 16-January 13

Posted 22 April 2013 - 04:38 AM

View PostArtificial, on 22 April 2013 - 01:41 AM, said:

It looks like your issue is most directly related to this line.

$duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days


When the rememberMe field is set, the duration is set to 30 days, when not set, 0 days. That's fine in itself, as if a value greater then 0 is passed through (remember me set), a cookie based authentication will be used, and if not, a session based authentication will be used. Seeing as how you've seemingly overrode some of the session configuration in your config file, I'd double check sessions are all working properly, as I'm betting that's where the problem is.

This is my session setting.Why its not working??
 'session' => array(
                    'class' => 'CDbHttpSession',
                    'cookieParams' => array('domain' => '.xxxxxx.com'),
                    'timeout' => 3600,
                'connectionID' => 'db',
                    'sessionName' => 'session',
                 ),

0

#6 User is offline   artificial 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 22 April 2013 - 05:16 AM

You're using the CDbHttpSession class. Are you checking to make sure sessions are being stored correctly in your database?
Alex Berriman
www.fishvision.com
0

#7 User is offline   marjss 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 16-January 13

Posted 22 April 2013 - 05:27 AM

View PostArtificial, on 22 April 2013 - 05:16 AM, said:

You're using the CDbHttpSession class. Are you checking to make sure sessions are being stored correctly in your database?

Sessions are stored in YiiSession table
0

#8 User is offline   artificial 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 107
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 22 April 2013 - 05:31 AM

Have you actually verified whether sessions are working properly? Try setting a session value with:

Yii::app()->session['hello'] = 'world';


And see if you can store and retrieve it properly across your application.
Alex Berriman
www.fishvision.com
0

#9 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 22 April 2013 - 07:46 AM

View Postmarjss, on 21 April 2013 - 11:55 PM, said:

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;
    }
}




Are you saying that you can't login from a login page without selecting a remember me option. is it so ?? If yes then could you please confirm your login form validation rules also. Might be remember is required on this form. So, what i mean check your validation rules for login model first. Is it working correctly or not.
CodeSutra
0

#10 User is offline   semirkaric 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 13-May 12

Posted 06 June 2013 - 03:37 AM

Just to reply if someone is having the same problem yet.
It could well be just that you have a space in your sessions name in config/main.php

Remove spaces in name and set 'cookieMode' => 'allow' or 'cookieMode' => 'only'.
Then clear your cookies and it should be working without remember me.
0

#11 User is offline   marjss 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 16-January 13

Posted 11 June 2013 - 01:37 AM

View Postcodesutra, on 22 April 2013 - 07:46 AM, said:

Are you saying that you can't login from a login page without selecting a remember me option. is it so ?? If yes then could you please confirm your login form validation rules also. Might be remember is required on this form. So, what i mean check your validation rules for login model first. Is it working correctly or not.

The validation rules are fine nothing an finally i got the solution..This is from the hosting server.They informed me that the cookies are not saving due to their technical problems.
The code is working fine now.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users