Yii2: Can I open the external interface from the backend?

I am currently struggling with yii2. The following scenario:

I use the extended yii2 template and have an interface and backend with separate user tables and inputs.

Now I'm looking for a way in which a backend user can log in as an external user from a backend. Let's say that you are in the backend and viewing the user of the interface, you can click "login as this user".

Is this scenario possible?

I tried to configure the use of the interface in the backend configuration:

'user' => [
         'identityClass' => 'backend\models\BackendUser',
         'enableAutoLogin' => false,
 ],
 'frontendUser' => [
        'class' => 'yii\web\User',
        'identityClass' => 'common\models\User',
        'enableAutoLogin' => false,
  ],

and in my controller I tried this:

if (Yii::$app->frontendUser->login($user_group->user, 0)) {
    return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
}

EDIT after Sergey's answer:

backend config

'user' => [
            'identityClass' => 'backend\models\BackendUser',
            'enableAutoLogin' => true,
            'identityCookie' => [
                'name' => '_backendUser', // unique for backend
            ]
        ],

frontend config:

'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'loginUrl' => ['message/welcome'], // weil beim SessionTimeout darauf umgeleitet wird,
            'authTimeout' => 1800,
            'identityCookie' => [
                'name' => '_frontendUser', // unique for frontend
            ]
        ],

Controller function:

public function actionLoginAs($id)
    {
        $user_group = UserGroup::findOne($id);
        if (is_null($user_group)) {
            return $this->redirect(['site/index']);
        }

        $group = $user_group->group;
        $client = $group->client;

        $yiiuser = new yii\web\User([
                'identityClass' => 'common\models\User',
                'identityCookie' => [
                        'name' => '_frontendUser', // unique for frontend
                ]
        ]);
        $user = $user_group->user;

        if ($yiiuser->login($user, 15 * 60)) {
            return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
        }

    }
+4
1
  • cookie auth:

'user' => [
  'identityClass' => 'common\models\User',
  'enableAutoLogin' => true,
  'identityCookie' => [
  'name' => '_frontendUser', // unique for frontend
  ]
],

'user' => [
  'identityClass' => 'backend\models\BackendUser',
  'enableAutoLogin' => true,
  'identityCookie' => [
  'name' => '_backendUser', // unique for backend
  ]
],

  1. , , admin/auth/loginUser

AuthController

public function actionLoginUser($login) {
    // check admin is loggin in
    $yiiuser = new yii\web\User([
        'identityClass' => 'common\models\User',
        'identityCookie' => [
            'name' => '_frontendUser', // unique for frontend
        ]
    ]);
    $user = common\models\User::findByUsername($login);
    // check user exists
    $yiiuser->login($user, false, 15 * 60); // 15 min
    return $this->redirect('/');
}
+3

Source: https://habr.com/ru/post/1610373/


All Articles