Best Place To Force A User To Log-Out?

Hi, where is the best place in a Yii application to run a custom check to decide if the current logged-in user should be forced to log-out. The custom check logic may take into account, for example, if the user record still exists in the database.

I’d like to do something like this:


if (!Yii::app()->user->isGuest) // if user is logged-in

{

	if (ForceLogout()) // custom check to decide if the current user should be logged-out

	{

		Yii::app()->user->logout();

		$this->redirect(Yii::app()->homeUrl);

	}

}

But I don’t know where to place this code.

From my experience with other programming languages, I would place something like this in the “Load” event of the application but I’m not sure about an equivalent location for a Yii application.

Any ideas will be appreciated. Thank you.

You must override you CController And than inherid new class in action,what you need. In new Controller class add this




class MyController extends CController

{

    protected function beforeAction($action)

    {

    if (!Yii::app()->user->isGuest) // if user is logged-in

    {

	if (ForceLogout()) // custom check to decide if the current user should be logged-out

	{

		Yii::app()->user->logout();

		$this->redirect(Yii::app()->homeUrl);

    	}

    }

        return parent::beforeAction($action);

    }

}




Thank you xahgmah!

I did that in the extended controller class that comes by default with a generated Yii application, located in protected/components/Controller.php


class Controller extends CController

Now I have to make sure that all my application controllers extend from that class, e.g.


class SiteController extends Controller

It’s working as expected.