Reputation: 2037
I struggle with yii2 at the moment. Following scenario:
I use yii2 advanced template and have a frontend and a backend with separate user tables and logins.
Now I'm looking for a way that a backend user can log in as a frontend user from backend. Let's say you're in the backend and view a frontend user, you can click "log in as this user".
Is this scenario possible?
I tried to configure a frontend use in backend's config:
'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]));
}
}
Upvotes: 5
Views: 2145
Reputation: 153
in backend :
public function actionLogin($id)
{
///find customer by id
$customer = $this->findModel($id);
//generate new access token to admin for login
$auth_key = $customer->generateAuthKey();
//save customer model changes
$customer->save();
//make url for login in frontend
$url = Yii::$app->params['frontendUrl'] . '/site/magic-login?k='.$auth_key;
return $this->redirect(Yii::$app->params['frontendUrl'] . '/site/magic- login?k='.$auth_key);
}
in forntend :
public function actionMagicLogin()
{
//logout logged user
Yii::$app->user->logout();
//find customer by access token
$customer = Customer::findIdentityByAccessToken($_GET['k']);
//login customer and make sessions
if (Yii::$app->user->login($customer))
{
//expire access token
$customer->generateAuthKey();
//redirect to show customer dashboard
$this->redirect(['customer/account']);
}
else
{
//if login faild redirect to login page
return $this->render('login');
}
}
Upvotes: 0
Reputation: 5207
frontend
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
backend
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
Actually separate front and backend users
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('/');
}
Upvotes: 3