Difference between #4 and #10 of
Module based login

Revision #10 has been created by suriyansuresh on Nov 1, 2010, 3:03:36 PM with the memo:

another login update
« previous (#4) next (#11) »

Changes

Title unchanged

Module based login

Category unchanged

Tutorials

Yii version unchanged

Tags unchanged

authentication, module

Content changed

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

**Step 2**

For each module, add the following line
s to its main page. For example, for the Customer module, add to Customer/CustomerModule.php Add the following lines to init():
[...]
public function actionLogout() {
Yii::app()->user->logout(false);
$this->redirect(Yii::app()->getModule('
admin')->user->loginUrlcustomer')->user->loginUrl);
 
    }
 
 
 
```
 
**Point sub-domain to module**
 
 
This tips from the forum URL [Subdomain to module](http://www.yiiframework.com/forum/index.php?/topic/7242-subdomain-and-modules/page__p__36698#entry36698 "Subdomain to module")
 
 
There is a feature called "Parameterizing Hostnames" ([described on this pages](http://www.yiiframework.com/doc/guide/1.1/en/topics.url "described on this pages")). add the following lines to main.php
 
 
```php 
'components' => array(
 
   ...
 
   'urlManager' => array(
 
      'rules' => array(
 
         'http://customer.example.com' => 'customer/default/index',
 
         'http://customer.example.com/login' => 'customer/default/login',
 
      ),
 
   ),
 
   ...
 
),
 
```
 
 
 
 
So "customer/default/index" refers to the module "customer" with controller "default" and action "index". 
 
 
**Update**
 
----------
 
 
**Redirect to respective module login, if user not logged in to a particular model**
 
 
add following code to customer module CustomerModule.php in beforeControllerAction 
 
 
 
 
```php 
if(Yii::app()->getModule('customer')->user->isGuest)
 
      Yii::app()->getModule('customer')->user->setReturnUrl('customer/default/login');
 
```
 
do the same for other module
 
 
in SiteController's site/login method
 
 
 
 
 
```php 
public function actionLogin() {
 
         Yii::app()->request->redirect(Yii::app()->createUrl(Yii::app()->user->returnUrl)
); } ``` ```**Update 2**
 
----------
 
 
**Redirect to respective module login, if user not logged in to a particular model**
 
 
add following code to customer module CustomerModule.php 
 
 
 
 
```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(
 
                'default/login',
 
                'default/error',
 
            );
 
            if (Yii::app()->user->isGuest && !in_array($route, $publicPages)){            
 
                Yii::app()->getModule('customer')->user->loginRequired();                
 
            }
 
            else
 
                return true;
 
        }
 
        else
 
            return false;
 
    }
 
```
 
do the same for other module
 
 
 
Thanks to ricardograna for sharing some configurations
13 0
19 followers
Viewed: 59 689 times
Version: 1.1
Category: Tutorials
Written by: suriyansuresh
Last updated by: suriyansuresh
Created on: Oct 23, 2010
Last updated: 7 years ago
Update Article

Revisions

View all history