Module based login

You are viewing revision #3 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#4) »

If you want to add module based login to a site without using RBAC, please follow these guidelines.

Consider the situation where we want to add 3 types of login in a site: customer, dealer, and admin.

Start by generating three modules using gii. (To do this, please refer to this guide for module generation http://yiiframework.com/doc/guide/1.1/en/basics.module#creating-module)

Step 1

Copy the UserIdentity component to the module/components folder for each module. We will validate each module against its table. For example, customer validation is done using customer table, and admin validation against the admin table

For each module, change its UserIdentity authenticate() function to perform the appropriate validation.

Step 2

For each module, add the following line to its main page. For example, for the Customer module, add to Customer/CustomerModule.php Add the following line to init():

$this->setComponents(array(
            'errorHandler' => array(
                'errorAction' => 'customer/default/error'),
            'user' => array(
                'class' => 'CWebUser',             
                'loginUrl' => Yii::app()->createUrl('customer/default/login'),
            )
        ));

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

Step 3

Create the login/logout action in each module's DefaultController.

For example, for the Customer module:

Create the actionLogin() in Customer/controllers/DefaultController.php.

Also, create actionLogout() as follows:

public function actionLogout() {
        Yii::app()->user->logout(false);
        $this->redirect(Yii::app()->getModule('admin')->user->loginUrl);
    }


Thanks to ricardograna for sharing some configurations