Changing the response of the request login access rule

Hey everyone,

I have a controller which requires people to be logged in. This all works dandy and it will redirect if the user is not logged in.

The problem is that this controller is actually accessed over AJAX and even in AJAX the logged in access rule returns the HTML login page. Returning this HTMl produces errors in my app so I am trying to work out how I can change the default action of the request login rule to send back an error "You need to login" or something when the user tries to do something that they are not logged in to do. I do not necessarily need it to produce some fancy return which will allow for dynamic login there and then, just a dialog which will tell them they need to login and will then redirect when they say "log me in".

I am using pretty much the default access rules with the RBAM extension on top (but since the RBAM just decides rules etc I don’t think I need to go into which RBAM I am using), like so:




	public function accessRules()

	{

		return array(

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

				'actions'=>array('save', 'delete', 'ajax_validate'),

				'roles'=>array('Admin'),

			),

			array('deny',  // deny all users

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

			),

		);

	}



So I need to know if it is possible (without breaking the upgrade ability of the Yii framework) to change the behaviour of the request login part the admin role in the Yii code.

Thanks,

Take a look at the sourcecode of CAccessControlFilter

You have to generate a descendant of the CAccessControlFilter and override the method accessDenied:

The current code:





/**

         * Denies the access of the user.

         * This method is invoked when access check fails.

         * @param IWebUser $user the current user

         * @param string $message the error message to be displayed

         * @since 1.0.5

         */

        protected function accessDenied($user,$message)

        {

                if($user->getIsGuest())

                        $user->loginRequired();

                else

                        throw new CHttpException(403,$message);

        }



$user->loginRequired does the redirect.

… or you override CWebUser and YourUser::loginRequired

Yep that worked perfect cheers :D