Yii2:是否可以从后端打开前端会话?
Yii2: Is it possible to open a frontend session from backend?
我现在正在为 yii2 苦苦挣扎。以下场景:
我使用 yii2 高级模板,有一个前端和一个后端,带有单独的用户表和登录名。
现在我正在寻找一种后端用户可以从后端作为前端用户登录的方法。假设你在后台查看一个前台用户,你可以点击"log in as this user".
这种情况可能吗?
我尝试在后端配置中配置前端使用:
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => false,
],
'frontendUser' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
在我的控制器中我尝试了这个:
if (Yii::$app->frontendUser->login($user_group->user, 0)) {
return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
}
在谢尔盖回答后编辑:
后端配置
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
前端配置:
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['message/welcome'], // weil beim SessionTimeout darauf umgeleitet wird,
'authTimeout' => 1800,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
控制器函数:
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]));
}
}
- 您必须单独验证 cookie 名称:
前端
'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
]
],
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('/');
}
在后端:
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);
}
在前端:
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');
}
}
我现在正在为 yii2 苦苦挣扎。以下场景:
我使用 yii2 高级模板,有一个前端和一个后端,带有单独的用户表和登录名。
现在我正在寻找一种后端用户可以从后端作为前端用户登录的方法。假设你在后台查看一个前台用户,你可以点击"log in as this user".
这种情况可能吗?
我尝试在后端配置中配置前端使用:
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => false,
],
'frontendUser' => [
'class' => 'yii\web\User',
'identityClass' => 'common\models\User',
'enableAutoLogin' => false,
],
在我的控制器中我尝试了这个:
if (Yii::$app->frontendUser->login($user_group->user, 0)) {
return $this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/index', 'client' => $client->login_address]));
}
在谢尔盖回答后编辑:
后端配置
'user' => [
'identityClass' => 'backend\models\BackendUser',
'enableAutoLogin' => true,
'identityCookie' => [
'name' => '_backendUser', // unique for backend
]
],
前端配置:
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
'loginUrl' => ['message/welcome'], // weil beim SessionTimeout darauf umgeleitet wird,
'authTimeout' => 1800,
'identityCookie' => [
'name' => '_frontendUser', // unique for frontend
]
],
控制器函数:
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]));
}
}
- 您必须单独验证 cookie 名称:
前端
'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
]
],
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('/');
}
在后端:
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);
}
在前端:
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');
}
}