Yii Not Trhowing 403 Exception When Login Required

I am calling an action which have access rule as to be accessed only by logged in user.


public function accessRules()

	{

		return array(

			array('allow',  // allow all users to perform 'index' and 'view' actions

				'actions'=>array('index','view'),

				'users'=>array('*'),

			),

			array('allow', // allow authenticated user to perform 'create' and 'update' actions

				'actions'=>array('create','update','test'),

				'users'=>array('@'),

			),

			array('allow', // allow admin user to perform 'admin' and 'delete' actions

				'actions'=>array('admin','delete'),

				'users'=>array('admin'),

			),

			array('deny',  // deny all users

				'users'=>array('*'),

			),

		);

	}

And when i call this action without login by ajax call as.


<a href="#" onclick="call()"> check error</a>

<script>

    function call(){

         $.ajax({

            url: '/user/test',

            type: "POST",

            data: {},

            dataType: "html",

            beforeSend: function() {

                $('#loading').show();

            },

            success: function() {

                console.log('succesbox');

            },

            error: function(jqXHR,error, errorThrown) {  

               if(jqXHR.status&&jqXHR.status==404){

                    alert(jqXHR.responseText); 

               }else if(jqXHR.status&&jqXHR.status==403){

                   alert(jqXHR.responseText+"login required"); 

               }else{

                   alert("Something went wrong");

               }

            }

        });

    }

Expected response should be error code 403 but is shows the response as attched image

can anyone please tell me what can be the reason?

I have solved this problem by override


loginRequired()

method in WebUser,


public function loginRequired() {

        if (Yii::app()->request->getIsAjaxRequest()) {

            throw new CHttpException(403, 'Forbidden');

            Yii::app()->end();

        } else {

            parent::loginRequired();

        }

    }

Is this the right way to achieve it?