Yii Framework Forum: How To Set Session Timeout Per User At Login Time? - Yii Framework Forum

Jump to content

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

How To Set Session Timeout Per User At Login Time? Rate Topic: -----

#1 User is offline   DCameronMauch 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 05-February 13

Posted 22 March 2013 - 08:38 AM

I am using an external authentication system. If I don't see a cookie, I redirect to another site, which sends them back after. When I do see the cookie, I connect to a Java process running on a local socket to decrypt the cookie and get the userid and timeout. Did that in the SiteController file. The UserIdentity only checks to see if the user is in the database. If they are not in the user table, they get an unauthorized view. All this works great.

The issue is I want to have the php session timeout the same time their authentication cookie timeout. I have seen the option for setting a global value for everyone. But I need to set it for each specific person at login time based on the timeout from the cookie. I have tried searching for this information and couldn't find anything.

Thanks for any help.
0

#2 User is offline   DCameronMauch 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 05-February 13

Posted 22 March 2013 - 10:00 AM

I think I have found part of my solution. In the actionLogin in the SiteController, I am passing a timeout value to the $model->login method, which I modified to accept and override the duration if set. So a cookie is being created, with the proper expiration time, but after it expires, nothing special happens. User does not need to be reauthenticated. ??? The allowAutoLogin is set to true in the config.
0

#3 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 22 March 2013 - 10:34 AM

Dear Friend

We can create a behavior for main application where we can put some logic.

components/ApplicationBehavior.php
class ApplicationBehavior extends CBehavior
{
    public function events()
    {
        return array(
           'onBeginRequest'=>'allotAuthTimeout',
        );
    }

    public function allotAuthTimeout()
    {   
        $owner=$this->owner;
        $user=$owner->user;
        if(!$user->isGuest)
        { 
            if($user->id=="seenivasan") //HERE I HAVE ARBITRARILY ASSIGNED authTimeout.
                $user->authTimeout=300; //YOU HAVE TO APPLY YOUR OWN LOGIC.
            if(!$user->id=="seenivasan")//YOU CAN PARSE authTimeout FROM COOKIES AND ASSIGN IT HERE.
                $user->authTimeout=60;
            
	    $expires=$user->getState(CWebUser::AUTH_TIMEOUT_VAR);
			
	    if ($expires!==null && $expires < time())
		 $user->logout(false);
	    else
		 $user->setState(CWebUser::AUTH_TIMEOUT_VAR,time()+$user->authTimeout);
		
        }
    }
}



Then you can attach this behavior to the application as a property.
main.php
'behaviors'=>array(
    'appBehav'=>'ApplicationBehavior',    
    ),


I hope it will help you a bit.

Regards.
0

#4 User is offline   DCameronMauch 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 05-February 13

Posted 22 March 2013 - 01:48 PM

I have to admit that I am a little confused. What is the point of the CWebUser class having a login method where the max logged in time can be set if it doesn't actually do anything?

Using the method you described above, how would I pass the correct expiration time to it from the SiteController's actionLogin function where I have that data?
0

#5 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 24 March 2013 - 12:46 AM

Dear Friend

Whenever we assign authtimeout a value, YII checks the authTimeout at every request and reset the expiry time or user is logged out.

For an example, let us assign the authTimeout in SiteController.

public function actionLogin()
{
	$model=new LoginForm;

	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())
		{  
                    if(!Yii::app()->user->id=="admin")
		        Yii::app()->user->authTimeout=100;
		    Yii::app()->user->setState(CWebUser::AUTH_TIMEOUT_VAR,time()+Yii::app()->user->authTimeout);

		    $this->redirect(Yii::app()->user->returnUrl);
		}

            $this->render('login',array('model'=>$model));
        }
}


What happens in the next request?

In immediate next request, the user component is again reconfigured from main.php.
There by default authTimeout is set as null.
If authTimeout is not set, YII assings the authTimeout to php session expiry.
Thus whatever we configure in SiteController is not taken into account in subsequent requests.

We may declare the authTimeout in main.php.
'components'=>array(
		'user'=>array(
			// enable cookie-based authentication
			'allowAutoLogin'=>true,
			'authTimeout'=>60, //ONE MINUTE.
		),
........................................................


Here we can not dynamically assign authTimeout.

That forced me to use application behavior.
In this approach,

1.We are assigning authTimeout to each individuals at every request.
2.We are also checking out expiry time. If there is timeout, user is logged out.
0

#6 User is offline   waterloomatt 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 09-April 10

Posted 24 March 2013 - 03:04 AM

Somebody correct me if I'm wrong, but I believe duration parameter in the login method, is auto-login duration; not auto-logout duration. Meaning, the cookie will be used to validate the user the next time they visit your site. The cookie will be valid for the duration that you specify in the login method.

Matt
0

#7 User is offline   Robert McKenney 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 18-June 13

Posted 12 February 2014 - 09:50 AM

Dear seenivasan,

Thank you for this example. It was well explained and really helped me. I have adapted this basic idea to suit the needs of our application.

Much appreciated!!

Cheers,RLM.
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