如何编写HWIOAuthBundle的用户提供程序

如何编写HWIOAuthBundle的用户提供程序

本文介绍了如何编写HWIOAuthBundle的用户提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过社交网络编写登录功能.
如果未登录用户,则将其持久保存到数据库中;如果该用户存在,则将该用户登录.我应该写什么给我的提供者?文件状态:

I want to write a login feature via social networks.
if the user is not logged it persists it into the database, if the user exists, log the user in.What should I write into my provider?Docs state :

这就是我写的,然后被卡住了

So this is what I wrote and then got stuck

<?php
namespace ng\MyBundle\Controller\Listeners;

use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;

class OAuthUserProvider implements OAuthAwareUserProviderInterface
{

}

您能告诉我应该使用什么方法吗?
有人可以给我一个不使用FOSuserBundle的提供程序示例吗?
谢谢

Can you tell me what are the methods That I should use ?
Can anybody give me a provider example not using FOSuserBundle ?
Thanks

推荐答案

如果打开OAuthAwareUserProviderInterface,则只能看到一种方法:

If you open the OAuthAwareUserProviderInterface you can see it has only one method :

/**
 * Loads the user by a given UserResponseInterface object.
 *
 * @param UserResponseInterface $response
 *
 * @return UserInterface
 *
 * @throws UsernameNotFoundException if the user is not found
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response);

下面有一个有关如何实现它的示例,当然,在您的情况下,您应该致电您的实体经理,并以设计它的方式访问用户.

Below there is an example on how to implement it, ofcourse in your case, you should call your entity managers, and access users the way you have designed it.

/**
 * {@inheritdoc}
 */
 public function loadUserByOAuthUserResponse(UserResponseInterface $response)
 {
   $username = $response->getUsername();
   $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
   //when the user is registrating
   if (null === $user) {
       $service = $response->getResourceOwner()->getName();
       $setter = 'set'.ucfirst($service);
       $setter_id = $setter.'Id';
       $setter_token = $setter.'AccessToken';
       // create new user here
       $user = $this->userManager->createUser();
       $user->$setter_id($username);
       $user->$setter_token($response->getAccessToken());
       //I have set all requested data with the user's username
       //modify here with relevant data
       $user->setUsername($username);
       $user->setEmail($username);
       $user->setPassword($username);
       $user->setEnabled(true);
       $this->userManager->updateUser($user);
       return $user;
    }

    //if user exists - go with the HWIOAuth way
    $user = parent::loadUserByOAuthUserResponse($response);

    $serviceName = $response->getResourceOwner()->getName();
    $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

    //update access token
    $user->$setter($response->getAccessToken());

    return $user;
}

这篇关于如何编写HWIOAuthBundle的用户提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:11