3

I developing a site with two different registrations, and I have 2 different table, Im using RbacDB, and in the web config in the components section I have user configuration, according to this I want to know how I can use 2 different fields in the config file?

config :

'components' => [
    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => '213h2i3121h12osiajls',
    ],
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'user' => [
        'identityClass' => 'app\models\User',
        'enableAutoLogin' => true,
    ],
    // Here after user I need to add another config user-two
    'user-two' => [
        'identityClass' => 'app\models\SecondUser',
        'enableAutoLogin' => true,
    ],

when I do it, shows this error enter image description here

Thanks!

root_milka
  • 76
  • 1
  • 6

3 Answers3

7

Try to set a class property in the user-two component:

'user-two' => [
    'class' => 'yii\web\User'
    'identityClass' => 'app\models\SecondUser',
    'enableAutoLogin' => true,
],

or create new class inherited from the yii\web\User class and set like this:

'user-two' => [
    'class' => 'app\models\NewClassInheritedFromUserClass'
    ....
]

Maybe this will help you.

Andrew Bu
  • 191
  • 1
  • 5
  • I tried both variants, error is gone, Tnx! but user stil not logged in – root_milka Feb 07 '16 at 17:10
  • 2
    the better way is create new class inherited from the yii\web\User class. In this class you should override `public $identityCookie = ['name' => '_user_two_identity', 'httpOnly' => true];`, because the user and the user-two should have different cookies for authorization. after entering login and password look in the cookies. do you see the _user_two_identity cookie? by the way, somewhere in your code (in the LoginForm, i think) you should call `\Yii::$app->user-two->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);` (not Yii::$app->user->login(...)) – Andrew Bu Feb 07 '16 at 18:10
  • Could someone provide a concrete example with code that uses two identities? I'm trying this, but I cannot seem to work it out. It seems that the second `yii\web\User` subclass works, but when I try to user AccessRule for a AdminController action, it fails, it does not understand that the user has already logged in, but using another identityClass and not the default one. – Vasilis Lourdas Jan 31 '18 at 18:58
1

You have to create a web user class for the second identity

namespace app\components;

class UserTwo extends \yii\web\User{
}

than specify the class name in your config

'user-two' => [
        'class'=> 'app\components\UserTwo'
        'identityClass' => 'app\models\SecondUser',
        'enableAutoLogin' => true,
    ],
Nader
  • 594
  • 4
  • 8
1

I have gone through the yii2 framework internals. As I have understood, You can make N Identities following below technique;


  • Above solutions are just the suggestions which are partial answers and some what helpful. kindly Follow my below changes in depth and you can create N identities as desired.
  • N identities are very useful when you do not want to implement Complex RBAC (Role Based Access Control) and just want to Filter Access at controller's request.

  • Let us Assume I have to create yet another Identity called 'Franchise' other than existing User which is nicely coupled inside Yii2 Framework.


DB Migrations

  1. Create a new migration file using command

    yii migrate/create create_franchise
    
  2. Copy paste the content of already available migration file at location PROJECT_NAME\console\migrations something like 'm170311_105858_create_user.php' and rename table name from 'user' to 'franchise'.

  3. Now, run migration command

    yii/migrate
    
    • You must get something like this on command prompt

        Apply the above migrations? (yes|no) [no]:yes
      
        applying m170311_105950_create_franchise
        create table {{%franchise}} ... done (time: 1.304s)
        applied m170311_105950_create_franchise (time: 1.350s)
      
  4. check DB whether DB is created. (I assume you have made DB settings in PROJECT_NAME\common\config\main-local.php)

  5. Please note that whatever may be the Identity class, it shall now use above 'Franchise' Table.

Creating Franchise Model

  1. Just goto 'Gii' Module and create a model for newly create franchise table.

  2. Model location must be PROJECT_NAME\common\models\Franchise.php

  3. Make Sure that the Model class implements IdentityInterface and also implements mandatory methods of IdentityInterface


Identity Class

  1. If you go to location PROJECT_NAME\vendor\yiisoft\yii2\web\User.php. This is the class that is referred everywhere in your project as Yii::$app->user. Copy paste the content of this class and create a new File called PROJECT_NAME\vendor\yiisoft\yii2\web\Franchise.php and paste the content in it. Make below changes in the file.

    • Find 'user' and replace it with 'franchise'.
    • Find 'User' and replace it with 'Franchise'.
    • Find $loginUrl = ['site/login']; and replace it with $loginUrl = ['franchise/login']; as you are going to have different controller to handle Franchise related actions.
    • Find $identityCookie = ['name' => '_identity', 'httpOnly' => true]; and replace the 'name' as '_fidentity' (you can see difference, identity cookie must be unique)
    • Find $authTimeoutParam = '__expire'; and replace it with $authTimeoutParam = '_f_expire';

PROJECT_NAME\vendor\yiisoft\yii2\web\Application.php

  1. In Application.php add below method,

    public function getFranchise()
    {
        return $this->get('franchise');
    }
    
  2. Also find method coreComponents() and add one more entry as below,

    'Franchise' => ['class' => 'yii\web\Franchise'],
    

PROJECT_NAME\frontend\config\main.php

  1. Inside components add below entry just after 'user' entry,

     'franchise' => [
            'identityClass' => 'common\models\Franchise',
            'enableAutoLogin' => true,
            'class' => 'yii\web\Franchise',
            'identityCookie' => ['name' => '_fidentity-frontend', 'httpOnly' => true],
        ],
    
Ankur Soni
  • 4,597
  • 4
  • 32
  • 63
  • 2
    If you modify PROJECT_NAME\vendor\yiisoft\yii2\web\Application.php, then on the next `composer update` you will lose any changes done in this file. – Vasilis Lourdas Feb 01 '18 at 15:24