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