此刻我与yii2斗争。以下情景:
我使用yii2高级模板,并有一个前端和一个后端,具有单独的用户表和登录。
现在我正在寻找一种方法,后端用户可以从后端作为前端用户登录。假设您在后端并查看前端用户,您可以单击“以此用户身份登录”。
这种情况可能吗?
我尝试在后端的配置中配置前端使用:
'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]));
}
}
答案 0 :(得分:3)
<强>前端强>
'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('/');
}
答案 1 :(得分:1)
在后端:
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');
}
}