Problem with returnUrl in app module

Hi people,

I’m was developing a application, with a admin module.

In this application, i choose to separate the login from admin module and application.

To do this, i followed this article in wiki: Module Based Login

The login of admin module and application was separate with success, but then i had another problem.

When i’m logging in the application, the returnUrl go to the returnUrl of the module.

By example:

The returnUrl of application is: /site/index

The returnUrl of module is: /admin_emc/default/index

When i’m logging in the module, the returnUrl works normally, but when i efetuate login in the application, i’m redirected to the returnUrl of admin module.

Here go the parts of my code.

Function init of the admin module


 public function init() {

        // this method is called when the module is being created

        // you may place code here to customize the module or the application

        // import the module-level models and components


        Yii::app()->user->setStateKeyPrefix('_admin');


        $this->setImport(array(

            'admin_emc.models.*',

            'admin_emc.components.*',

        ));


        $this->setComponents(array(

            'errorHandler' => array(

                'errorAction' => 'admin_emc/default/error'),

            'user' => array(

                'class' => 'CWebUser',

                'allowAutoLogin' => FALSE,

                'loginUrl' => array('/admin_emc/default/login'),

                'returnUrl' => array('/admin_emc/default/index'),

            )


        ));


        

        

        

    }

In the config file:


// application components

	'components'=>array(

		'user'=>array(

			// enable cookie-based authentication

			'allowAutoLogin'=>false,

                        'loginUrl'=>array('/site/login'),

                        'returnUrl'=>array('/site/index'),

		),

		// uncomment the following to enable URLs in path-format

                ....

		

In action login of both i used the redirect function to returnUrl:

Admin module: $this->redirect(Yii::app()->getModule(‘admin_emc’)->user->returnUrl);

Application: $this->redirect(Yii::app()->user->returnUrl);

Well, i tried everything, and don’t find nothing like this in the forum.

Hope you can help me.

Abraços

Well, i’ll sleep now, but feel free to make questions, or indicate my error.

abraço.

someone?

I am not sure what you are trying to do.

  1. After logging in, user goes to fixed location.(frontend user goes to site/index, backend user goes to admin_emc/default/index)

or

  1. After logging in, send user back to where he/she was.

which one is it?

Hi sayan,

The first option.

I used a function named returnUrl to this. But don’t works, cause the frontend go to returnUrl of the backend.

Abraço

OK… hmm if it is first option, I would not use returnUrl.

Anyway, you like to use returnUrl, so let’s try some.

Change your logout action in default controller in admin_emc module like this.

public function actionLogout()

{

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

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

$this->redirect(Yii::app()->getModule(‘admin’)->user->loginUrl);

}

how about now? try it.

Hi,

returnUrl property is always set when your actions require login.

Example:

suppose you want to access http://example.com/site/stats and that access requires authorization, then CWebUser, after checking permissions, calls loginRequired method where it sets returnUrl property with




Yii::app()->getRequest()->getUrl();



which is current (http://example.com/site/stats) address. After that you are redirected to the login page where, after successful login, you are once again redirected, but this time to the returnUrl address which is http://example.com/site/stats.

If you want to use always the same address do not use returnUrl.

Cheers

Sayan,

My problem is when i use the login function. Logout is working sucessfully. And the false within logout is to avoid the SESSION was destroyed. So, just the module session gonna be destroyed. This is essencial to separate the login from module and app frontend. But thanks by help.

Drylko,

So you mean that, the returnUrl is used just when i want to let the return page, dynamic?

If that, i have to redirect manually? Or have another property to use in this case?

Abraço

It was the testing purpose, because I need to see whether your module is sharing returnUrl with frontend or not. I know you use different prefix for module session, but your frontend read it when it check the returnUrl.

Like drylko wrote, if you always use the same address for return after login, returnUrl is not good for it(You need check yii source code to see how returnUrl works ;) ), but you can force it to look like the way what you want.

sayan

Well, i learn a bit, and see thats true.

I just replace the returnUrl by the sample Url, switch i want to redirect.

$this->redirect(’./index’);

It seems working.

thanks sayan.

Abraço