How to manage multiple user session?

Hi, in my application I have clients in a model called UserClient and backoffice clients in the UserAdmin model. In config/web.php I do this:





        /*'user' => [

            'identityClass' => 'app\models\User',

            'enableAutoLogin' => true,

        ],*/

        'userClient' => [

            'class'=>'yii\web\User',

            'identityClass' => 'app\modules\customers\models\UserClient',

            'enableAutoLogin' => true,

            'loginUrl'=>'/customers/user/login',

            'returnUrl'=>'/customers/customer/index',

        ],

        'userAdmin' => [

            'class'=>'yii\web\User',

            'identityClass' => 'app\modules\backoffice\models\UserAdmin',

            'enableAutoLogin' => true,

            'loginUrl'=>'/backoffice/user/login',

            'returnUrl'=>'/backoffice/user/index',

        ],



But, when I login in one and next I login in the other, the second one overwrites the first.

In UserController in customers module, I have this:




    public function behaviors()

    {

        return [

            'access' => [

            	'user' => 'userAdmin',

                'class' => AccessControl::className(),

                'only' => ['logout','index'],

                'rules' => [

                    [

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }



In UserController in backoffice module, I have this:




    public function behaviors()

    {

        return [

            'access' => [

            	'user' => 'userClient',

                'class' => AccessControl::className(),

                'only' => ['logout'],

                'rules' => [

                    [

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'logout' => ['post'],

                ],

            ],

        ];

    }



Somebody can help me? It’s possible have 2 access areas for 2 kind of users of different models?

Note: I am using Yii 2.0.3 (simple template)

Thanks!

I found the solution!!!, I had to use ‘idParam’ to assign different session variable name for each case… something like this:




        'userClient' => [

            'class'=>'yii\web\User',

            'identityClass' => 'app\modules\customers\models\UserClient',

            'idParam'=>'userClient',

            'enableAutoLogin' => true,

            'loginUrl'=>'/customers/user/login',

            'returnUrl'=>'/customers/customer/index',

        ],

        'userAdmin' => [

            'class'=>'yii\web\User',

            'identityClass' => 'app\modules\backoffice\models\UserAdmin',

            'idParam'=>'UserAdmin',

            'enableAutoLogin' => true,

            'loginUrl'=>'/backoffice/user/login',

            'returnUrl'=>'/backoffice/user/index',

        ],

[size=2]

[/size]

2 Likes