2017-04-06 68 views
1

我使用的是SfGuardPlugin和我的网站的后端我有完整的用户列表,我希望能够在用户的前端登录我从列表中选择。Symfony 1.4 - 从后端登录到前端(作为客户端)

我试过这个方法:

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'); 
} 

它重定向我到前端的主页,但我没有登录

更多的挖掘后,我发现,我从后端注销,现在我在前端使用管理员而不是我选择的用户登录。所以sfContext :: switchTo不能正常工作。

回答

0

我找到了解决方法。

  1. 在users表上创建一个新列(login_frontend_key)。
  2. 创建一个新的前端为必需的参数(名为key)
  3. 创建前端控制器女巫的行动路线是负责登录用户,例如:

    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'); 
    } 
    
  4. 使用这将生成交叉应用程序链接http://symfony.com/blog/cross-application-links

  5. 后端创建对象动作用户页面:

    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); 
    }