Two different frontend users

Hello,

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.

Shouldn’t you have two separate applications in these cases?

Could do, but, for just 1-2 separate pages for the new user, I don’t think it’s worth the effort.

It is doable but more complicated than adding another application.

How about adding a second user in frontend config? I’ve tried:




'user2' => [

    'identityClass'   => 'common\models\User2',

    'enableAutoLogin' => true,

],

and later on in login form for this user do:




public function login() {

    if ($this->validate()) {

        return Yii::$app->user2->login($this->getUser(), 1800);

    }

    return false;

}

Right now, by implementing the above, I’m facing a “Setting unknown property: common\models\User2::identityClass” error.

Because user component is not identity.

Would you consider as a good solution to add a second totally different frontend? Is it a valid solution? and if yes, is there any tutorial/how-to guide of what is needed to do this?

thank you.

Yes, adding another frontend-like application is a good solution.

Thank you.

Unfortunately I think in the link above the instructions are not sufficient. I fully copied the frontend folder (ahead with environments/prod and environments/dev folders) to another one "frontend2", I replaced every occurence in uses/namespaces, and added the alias in common\config\bootstrap.php as mentioned in the link.

I configured my apache virtual host as well:


<VirtualHost *:2222>

    DocumentRoot /var/www/mysite/frontend2/web


    <Directory /var/www/mysite/frontend2/web/>

    # Allow FollowSymlinks to enable RewriteRules

    Options FollowSymLinks

    # No option can be overwritten by .htaccess files

    AllowOverride None

    Order allow,deny

    allow from all


    # Use mod_rewrite for pretty URL support

    RewriteEngine on

    # If a directory or a file exists, use the request directly

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    # Otherwise forward the request to index.php

    RewriteRule . index.php

</Directory>


ErrorLog /var/log/apache2/error.log


# Possible values include: debug, info, notice, warn, error, crit,

# alert, emerg.

LogLevel warn


CustomLog /var/log/apache2/access.log combined


</VirtualHost>



Unfortunately the localhost:2222 page is not working. The instructions in the above link are missing something.

Thank you again for your patience.

How exactly it’s not working?

This is Google Chrome’s message:

This site can’t be reached

The webpage at http://localhost:2222/ might be temporarily down or it may have moved permanently to a new web address.

ERR_SOCKET_NOT_CONNECTED

The problem was the "Listen 2222" in apache configuration. Now everything works ok.

Case closed! Thank you a lot!