Search code examples
phpyiiyii-components

How to properly create different logins for different Modules in Yii framework


In my Yii 1.x application I defined new Admin module. In the init method of the admin module I defined new user component like this:

$this->setComponents(array(
            'user'=>array(
                'class' => 'CWebUser',
                // enable cookie-based authentication
                'allowAutoLogin'=>true,
                'baseUrl'=>Yii::app()->createUrl("admin/user/login"),
                'stateKeyPrefix' => '_admin',
            ),
        ));

Now, I expect I can do the following:

Yii::app()->getModule("admin")->user->login($this->_identity,$duration)

or

Yii::app()->getModule("admin")->user->logout();

but it is not working.

When I print my module (var_dump(Yii::app()>getModule("admin"))) I can see that user component is not defined.

object(AdminModule)[14]
  public 'defaultController' => string 'default' (length=7)
  public 'layout' => null
  public 'controllerNamespace' => null
  public 'controllerMap' => 
    array (size=0)
      empty
  private '_controllerPath' (CWebModule) => null
  private '_viewPath' (CWebModule) => null
  private '_layoutPath' (CWebModule) => null
  public 'preload' => 
    array (size=0)
      empty
  public 'behaviors' => 
    array (size=0)
      empty
  private '_id' (CModule) => string 'admin' (length=10)
  private '_parentModule' (CModule) => null
  private '_basePath' (CModule) => string '/srv/www/htdocs/public/project/application/protected/modules/admin' (length=71)
  private '_modulePath' (CModule) => null
  private '_params' (CModule) => null
  private '_modules' (CModule) => 
    array (size=0)
      empty
  private '_moduleConfig' (CModule) => 
    array (size=0)
      empty
  private '_components' (CModule) => 
    array (size=0)
      empty
  private '_componentConfig' (CModule) => 
    array (size=1)
      'user' => 
        array (size=4)
          'class' => string 'CWebUser' (length=8)
          'allowAutoLogin' => boolean true
          'baseUrl' => string '/project/application/index.php/admin/user/login' (length=52)
          'stateKeyPrefix' => string '_admin' (length=11)
  private '_e' (CComponent) => null
  private '_m' (CComponent) => null

Solution

  • Please, code from working project. Defined user component in module main class:

    public function init() {
        parent::init();
        $this->setImport(array(
            'admin.models.*',
            'admin.components.*',
        ));
        Yii::app()->setComponents(array(
            'user'=>array(
                'class'=>'AdminWebUser',
                'allowAutoLogin'=>true,
                'loginRequiredAjaxResponse'=>'Dear admin, your session expired, login and try again',
                'stateKeyPrefix'=>'admin_',
                'authTimeout'=>14400,
            ),      
        ), false);
    }
    

    and default user component defined in main config file:

    'user'=>array(
        'class'=>'WebUser',
        'loginRequiredAjaxResponse'=>'Your session expired, login and try again',
        'autoRenewCookie'=>false,
        'allowAutoLogin'=>false,
        'stateKeyPrefix'=>'user_',
        'loginUrl'=>'/home/login',
    ),
    

    So you can "have two sessions for one user" depending on module (in module other session, in root -other). For example var_dump(Yii::app()->user->stateKeyPrefix) out of module gives user_, but in module gives admin_. In fact yii uses the same session file, but sets and gets data depending on stateKeyPrefix (when we use setState() or getState() ).

    So If you keep userName in admin module using Yii::app()->user->setState('userName', 'John'), it puts in session file 'admin_userName' => 'John', it means you can get that value out of admin module (for example in root) by using Yii::app()->session->get('admin_userName'). If you try to get it (out of module) using getState it can not return you the correct value, because it must find user_userName (because in root, for user component 'stateKeyPrefix' => 'user_') instead of admin_userName.

    Thank you and sorry for long expression ))