2014-01-26 46 views
0

因此,我打算使用Google提供身份驗證,除了用戶ID和角色之外,我的身邊不會有任何憑據或帳戶特定信息。有誰知道如何去做一個用戶出現驗證symfony只有身份證?我見過自定義的用戶提供者,但他們需要密碼,鹽,散列算法以及哪些不是我沒有/需要的。PHP Symfony認證/授權登錄{Google/Facebook/etc}

回答

1

爲了實現谷歌/ Facebook的登錄,HWIOAuth包是一個不錯的選擇。它支持20多個服務提供商。實施Facebook和Google身份驗證的整個過程有點過長,無法在此處發佈。 您可以在這裏閱讀一步一步的說明:http://praveesh4u.github.io/blog/2014/01/23/oauth-in-symfony/

+0

感謝,OAuthUser和OAuthUserProvider有我正在尋找的代碼。實際上我之前看了一下HWIOAuth軟件包,但由於缺少文檔而丟棄了它,並且不想瀏覽所有的代碼。沒有太長時間才能爲Google啓動OAuth並運行,只需要弄清楚如何與交響樂集成只有一個ID。 – James

0

我用下面的代碼此:

/** 
* Sign in with Facebook 
* @return redirect 
*/ 
public function signInAction() { 
    $request = $this->get('request'); 
    $session = $this->get('session'); 
    // Facebook init 
    $facebook = $this->get('bw.user.social')->getFacebook(); 

    if ($request->query->has('code')) { 
     if ($facebook->getUser()) { 
      $userProfile = $facebook->api('/me/'); 
      if ($userProfile) { 
       $user = $this->getDoctrine() 
         ->getRepository('BWUserBundle:User') 
         ->loadUserBySocialId('facebookId', $userProfile['id']); 
       if ($user == NULL) { 
        // Email existence check 
        $isEmailExists = $this->getDoctrine() 
          ->getRepository('BWUserBundle:User') 
          ->isEmailExists($userProfile['email']); 
        if ($isEmailExists) { 
         $session->getFlashBag()->add('danger', '<b>Error!</b> e-mail "'. $userProfile['email'] .'" already exists.'); 
         return $this->redirect($this->generateUrl('home')); 
        } 

        $i = 0; 
        do { 
         $username = $userProfile['username'] . ($i ? '_'. $i : ''); 
         $isUsernameExists = $this->getDoctrine() 
           ->getRepository('BWUserBundle:User') 
           ->isUsernameExists($username); 
         $i++; 
        } while ($isUsernameExists); 
        // Создание нового пользователя 
        $em = $this->getDoctrine()->getManager(); 
        $role = $em->getRepository('BWUserBundle:Role')->findOneBy(array(
         'role' => 'ROLE_USER', 
        )); 
        $user = new User(); 
        $user 
          ->setFacebookId($userProfile['id']) 
          ->setEmail($userProfile['email']) 
          ->setUsername($username) 
          ->setActive(TRUE) 
          ->setConfirm(TRUE) 
          ->setHash('') 
          ->addRole($role) 
          ->generatePassword() 
         ; 
        $em->persist($user); 
        $em->flush(); 
       } 
       if ($user) { 

        $this->authorizeUser($user); // return TRUE if success 
       } 
      } 
     } 
    } else { 
     $params = array(
      'scope' => $this->get('service_container')->getParameter('social')['facebook']['scopes'], 
      'redirect_uri' => $this->get('router')->generate('user_facebook_sign_in', array(), TRUE), 
     ); 

     return $this->redirect($facebook->getLoginUrl($params)); 
    } 

    return $this->redirect($this->generateUrl('home')); 
} 

/** 
* User auth 
* @param \BW\UserBundle\Entity\User $user 
* @return boolean 
*/ 
protected function authorizeUser(User $user) { 
    $request = $this->getRequest(); 

    /* Social User auth in Symfony */ 
    $token = new UsernamePasswordToken($user, $user->getPassword(), 'auth', $user->getRoles()); 
    $this->get('security.context')->setToken($token); 

    // Fire the login event 
    // Logging the user in above the way we do it doesn't do this automatically 
    $event = new InteractiveLoginEvent($request, $token); 
    $this->get('event_dispatcher')->dispatch('security.interactive_login', $event); 

    return TRUE; 
} 

,還可以使用facebook PHP SDK