我正在使用 SfGuardPlugin ,在我网站的后端,我有完整的用户列表,我希望能够使用我从列表中选择的用户登录前端。
我尝试过这种方法:
public function executeListLogin(sfWebRequest $request) {
// client that I've selected from list
$client = $this->getRoute()->getObject();
// create instance if doesn't exist
if(!sfContext::hasInstance('frontend')){
sfContext::createInstance(ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false));
}
// switch to frontend
sfContext::switchTo('frontend');
// also tried with: sfContext::getInstance('frontend')
sfContext::getInstance()->getUser()->signin($client->getSfGuardUser(), true);
// redirect to frontend homepage
$this->redirect('@homepage');
}
它将我重定向到前端主页,但我还没有登录。
经过多次挖掘后,我发现我已从后端登出,现在我已经使用admin而不是我选择的用户登录前端。所以sfContext :: switchTo无法正常工作。
答案 0 :(得分:0)
我找到了解决方法。
在前端控制器中创建操作女巫负责登录用户,例如:
public function executeLoginClientKey(sfWebRequest $request){
// get the client by key
$this->forward404Unless($user = ClientPeer::getByLoginFrontendKey($request->getParameter("key")));
// if you already logged in with another user, signout
if($this->getUser()->isAuthenticated())
{
$this->getUser()->signOut();
}
// signin with the new user
$this->getUser()->signIn($user->getsfGuardUser(), false);
$user->setLoginFrontendKey(null); // delete the key from DB
$user->save();
$this->redirect('@homepage');
}
使用此选项生成交叉应用程序链接http://symfony.com/blog/cross-application-links
在后端用户页面上创建对象操作:
public function executeListLogin(sfWebRequest $request)
{
// get the selected client
$client = $this->getRoute()->getObject();
$key = $client->generateLoginFrontendKey(); // generate a random key
$client->setLoginFrontendKey($key); // store the key in DB
$client->save();
// generate the frontend url for login
$url = $this->getContext()->getConfiguration()->generateFrontendUrl('login_frontend_key', array('key' => $key, 'sf_culture' => 'nb'));
$this->redirect($url);
}