2015-10-19 167 views
0

我使用FOSUserBundle和Facebook API。我想讓我的用戶使用此API在我的網站上註冊。然後我希望他們可以登錄到我的網站。API Facebook - FOsUserBundle - 哈希密碼

我不知道我怎麼可以做自動生成的密碼的哈希來匹配FOSUserBundle。

我已經在互聯網上找到這下代碼,我不明白我怎麼能與FOSUserBundle匹配這個...提前

如果sombebody能幫助我...謝謝,我的英語水平很抱歉。

/** 
* Loading and ad-hoc creation of a user by an OAuth sign-in provider account. 
* 
* @author Fabian Kiss <[email protected]> 
*/ 

class UserProvider extends FOSUBUserProvider{ 

/** 
* {@inheritDoc} 
*/ 
public function loadUserByOAuthUserResponse(UserResponseInterface $response){ 

    try { 
     return parent::loadUserByOAuthUserResponse($response); 
    } catch (UsernameNotFoundException $e) { 
     if (null === $user = $this->userManager->findUserByEmail($response->getEmail())) { 
      return $this->createUserByOAuthUserResponse($response); 
     } 

     return $this->updateUserByOAuthUserResponse($user, $response); 
    } 
} 

/** 
    * {@inheritDoc} 
    */ 
public function connect(UserInterface $user, UserResponseInterface $response){ 
    $providerName = $response->getResourceOwner()->getName(); 
    $uniqueId = $response->getUsername(); 
    $user->addOAuthAccount($providerName, $uniqueId); 

    $this->userManager->updateUser($user); 
} 

/** 
    * Ad-hoc creation of user 
    * 
    * @param UserResponseInterface $response 
    * 
    * @return User 
    */ 
protected function createUserByOAuthUserResponse(UserResponseInterface $response){ 
    $user = $this->userManager->createUser(); 

    $this->updateUserByOAuthUserResponse($user, $response); 

    // set default values taken from OAuth sign-in provider account 
    if (null !== $email = $response->getEmail()) { 
     $user->setEmail($email); 
    } 

    if (null === $this->userManager->findUserByUsername($response->getNickname())) { 
     $user->setUsername($response->getNickname()); 
    } 

    $user->setUsername(strtolower(str_replace(' ','',$response->getRealName()).''.rand(0,199))); 
    $user->setUsernameCanonical(strtolower(str_replace(' ','',$response->getRealName()).''.rand(0,199))); 
    $user->setEnabled(false); 

    return $user; 
} 

/** 
    * Attach OAuth sign-in provider account to existing user 
    * 
    * @param FOSUserInterface  $user 
    * @param UserResponseInterface $response 
    * 
    * @return FOSUserInterface 
    */ 
protected function updateUserByOAuthUserResponse(FOSUserInterface $user, UserResponseInterface $response){ 
    $providerName = $response->getResourceOwner()->getName(); 
    $providerNameSetter = 'set'.ucfirst($providerName).'Id'; 
    $user->$providerNameSetter($response->getUsername()); 

    if(!$user->getPassword()) { 
     // generate unique token 
     $secret = md5(uniqid(rand(),true)); 
     $user->setPassword($secret); 
    } 

    return $user; 
} 
} 

回答

0

您可以使用encoder_factory和用戶的鹽來編碼密碼。 (我用的容器來獲得服務,使用服務爲您提供一流的構造函數)

$user = $userManager->createUser(); 
    $encoder = $this->container->get('security.encoder_factory')->getEncoder($user); 
    $user->setPlainPassword(
    $encoder->encodePassword(
     $this->container->get('fos_user.util.token_generator')->generateToken(), 
     $user->getSalt() 
     ) 
    ); 
+0

感謝您的答案,我的答案很抱歉有點晚了,但我沒有收到任何通知:(我會嘗試你的解決方案:) – Christophe