2

我正在创建一个 Intranet,因此所有用户都有自动为他们创建的帐户。然后他们会转到一个 URL 来创建他们的密码。所有这些都很好。我现在正试图让他们将他们的 Facebook 帐户“附加”到他们的用户帐户。这就是我的麻烦开始的地方......

在使用此方法登录 facebook 后,我已成功让用户正确进行身份验证SecurityController

public function loginCheckFacebookAction()
{

    $request = $this->getRequest();
    $session = $request->getSession();
    $response = new Response();


    $sessionUser = $this->getUser();

    $facebook = $this->get('facebook');

    $fbUser = $facebook->getUser(); // facebook id


    if ($fbUser) {
      try {

          $em = $this->getDoctrine()->getManager();
          $repo = $em->getRepository("IntranetBundle:User");
          $user = $repo->findUserByFBID($fbUser);


          if($user){

              $targetPath = $session->get("_target_path");

               $firewall = "secured_area";

               $roles = $user->getRoles();

               $rolesArray = array();

    foreach($roles as $role):
        $rolesArray[] = $role->getRole();
    endforeach;

               $token = new UsernamePasswordToken($user, $user->getPassword(), $firewall, $rolesArray);

               $event = new InteractiveLoginEvent($this->getRequest(), $token);
               $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);

               $this->get('security.context')->setToken($token);


              /*
              If I uncomment out either of the following redirects
              the redirect happens but the user is not authenticated properly
              If I comment them out I'm just rendering my error template.  The user
              however, is authenticated properly.
              */  
              if(isset($targetPath)){
                  //return $this->redirect($targetPath);
              } else {
                  //return $this->redirect($this->generateUrl("ssla_intranet_homepage"));
              }


          }

      } catch (FacebookApiException $e) {

          $user = null;

      }

    // I just render this template to be able to view debug bar.  
    //User is authenticated properly.

    $pageParameters = $this->getPageParameters();
    $pageParameters["page"] = "error";
    $pageParameters["error"] = array();
    $pageParameters["error"]["message"] = "Something went wrong with authentication";

    return $this->render('IntranetBundle:Error:index.html.twig', $pageParameters);
}

我是否需要为所有interactive_login事件注册一个侦听器,然后处理所有post-login重定向,而不是尝试在同一方法中重定向?

第一个使用 Symfony 的项目,这让我很生气。有什么想法吗?

顺便说一句,我查看了FOSFacebookBundle,但希望能够使用多种不同的方法进行身份验证,包括教义、gmail、facebook 和 twitter。

4

0 回答 0