Module based login

You are viewing revision #10 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 (#4)next (#11) »

  1. Update
  2. Update 2

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

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 lines to its main page. For example, for the Customer module, add to Customer/CustomerModule.php Add the following lines to init():

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


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() {

Point sub-domain to module

This tips from the forum URL Subdomain to module

There is a feature called "Parameterizing Hostnames" (described on this pages). add the following lines to main.php

'components' => array(
   'urlManager' => array(
      'rules' => array(
         '' => 'customer/default/index',
         '' => 'customer/default/login',

So "customer/default/index" refers to the module "customer" with controller "default" and action "index".


Redirect to respective module login, if user not logged in to a particular model

add following code to customer module CustomerModule.php in beforeControllerAction


do the same for other module

in SiteController's site/login method

public function actionLogin() {

Update 2

Redirect to respective module login, if user not logged in to a particular model

add following code to customer module CustomerModule.php

public function beforeControllerAction($controller, $action) {
        if (parent::beforeControllerAction($controller, $action)) {
            // this method is called before any module controller action is performed
            // you may place customized code here
            $route = $controller->id . '/' . $action->id;
           // echo $route;
            $publicPages = array(
            if (Yii::app()->user->isGuest && !in_array($route, $publicPages)){            
                return true;
            return false;

do the same for other module

Thanks to ricardograna for sharing some configurations

13 0
Viewed: 87 561 times
Version: Unknown (update)
Category: Tutorials
Written by: suriyansuresh
Last updated by: suriyansuresh
Created on: Oct 23, 2010
Last updated: 12 years ago
Update Article


View all history

Related Articles