0

I'm developing an application using the advanced Yii2 template.

I already have two different sessions separated for backend and frontend users. My main.php files are like this:

// frontend/config/main.php
...
'user' => [
    'identityClass'   => 'common\models\User',
    'enableAutoLogin' => true,
],
...

// backend/config/main.php
...
'user' => [
    'identityClass'   => 'backend\models\BEUser',
    'enableAutoLogin' => true,
],
...

I am facing problems when I try to implement a second separate frontend user. Right now, when I login for the different user model, I'm logged in as a regular frontend user as well.

I don't want to use RBAC, because I want the two different kinds of users fully separate. Which means, different database tables, different session names, different login pages, and access to totally different parts and pages of the website.

I think of adding a separate "user2" entry in frontend/config/main.php, but I cannot find any similar guide on the internet. So I assume this is something not possible.

Do you have any suggestions for that? Thank you.

cj_panpet
  • 1
  • 1
  • 1
  • Why not add a second frontend as well? – topher Jan 13 '17 at 14:58
  • I've thought of this. It seems not very worth the effort though. I need the second user only for one or two separate pages. Creating a separate frontend with different configuration, controllers, etc, I don't know, seems too much effort. There must be another way. – cj_panpet Jan 17 '17 at 09:01
  • *'different database tables, different session names, different login pages, and access to totally different parts and pages of the website'*. Seems worth the effort considering all of the above. Easier to test and maintain too. – topher Jan 17 '17 at 09:47
  • OK. Can you suggest a small guide or a small how-to on the internet, about creating a different frontend area in yii2?Thank you. – cj_panpet Jan 17 '17 at 10:48

1 Answers1

0

I haven't really tested this, however you might be able to do it by creating a user2 in your frontend create a LoginForm2 model that instead of using user component it uses user2 component to do the login.

frontend config:

'user2' => [
    'class' => 'yii\web\User',
    'identityClass'   => 'common\models\User2',
    'enableAutoLogin' => true,
],

LoginForm2.php:

public function login() {
    if ($this->validate()) {
        return Yii::$app->user2->login($this->getUser(), 1800);
    }
    return false;
}
marche
  • 1,756
  • 1
  • 14
  • 24
  • By implementing the above, I'm facing a "Setting unknown property: common\models\User2::identityClass" error. Any ideas? – cj_panpet Jan 17 '17 at 09:47
  • Have you tested the code you posted? Unfortunately this does not work either. When I login with a user2 account, I also have access as a regular user in other parts of the website that I don't want to. – cj_panpet Jan 18 '17 at 11:47