Module based login

You are viewing revision #4 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 or see the changes made in this revision.

« previous (#3)next (#10) »

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

13 0
18 followers
Viewed: 91 732 times
Version: Unknown (update)
Category: Tutorials
Written by: suriyansuresh
Last updated by: suriyansuresh
Created on: Oct 23, 2010
Last updated: 14 years ago
Update Article

Revisions

View all history

Related Articles