Here are the steps to force login for whole application in yii.
Add following code in config/main.php
'behaviors' => array(
'onBeginRequest' => array('class' => 'application.components.RequireLogin')
),
Create a file RequireLogin.php in protected/components/RequireLogin.php with following code
class RequireLogin extends CBehavior
{
public function attach($owner){
$owner->attachEventHandler('onBeginRequest', array($this, 'handleBeginRequest'));
}
public function handleBeginRequest($event){
if (Yii::app()->user->isGuest && !strstr($_SERVER['REQUEST_URI'], "/site/login")) {
Yii::app()->user->loginRequired();
}
}
}
And check code in loginRequired Method framework/web/auth/CWebUser.php, it should be like this
$app=Yii::app();
$request=$app->getRequest();
if(!$request->getIsAjaxRequest())
$this->setReturnUrl($request->getUrl());
if(($url=$this->loginUrl)!==null)
{
if(is_array($url))
{
$route=isset($url[0]) ? $url[0] : $app->defaultController;
$url=$app->createUrl($route,array_splice($url,1));
}
$request->redirect($url);
}
else
throw new CHttpException(403,Yii::t('yii','Login Required'));
That’s it.