2014-01-26 17 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