问题描述
该问题与此文章有关
我已经搜索了很多次,并且使问题更精确了.
I have googled many times and made the problem more precisely.
我的问题很简单.
我可以从此处传递参数吗(例如:myStatus = 1)
Can I pass the parameter from here (for example : myStatus=1)
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook' }) }}" />facebook</a>
上这堂课吗?
FOSUBUserProvider类MyUserBundle \ Security \ Core \ User
FOSUBUserProvider class MyUserBundle\Security\Core\User
class FOSUBUserProvider extends BaseClass
{
protected $container;
public function __construct(UserManagerInterface $userManager, $container, array $properties)
{
$this->userManager = $userManager;
$this->container = $container;
$this->properties = $properties;
}
/**
* {@inheritDoc}
*/
public function connect(UserInterface $user, UserResponseInterface $response)
{
$property = $this->getProperty($response);
$username = $response->getUsername();
//I WANT TO USE THE myStatus PARAMTER HERE!!!
//on connect - get the access token and the user ID
$service = $response->getResourceOwner()->getName();
$setter = 'set'.ucfirst($service);
$setter_id = $setter.'Id';
$setter_token = $setter.'AccessToken';
//we "disconnect" previously connected users
if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
$previousUser->$setter_id(null);
$previousUser->$setter_token(null);
$this->userManager->updateUser($previousUser);
}
//we connect current user
$user->$setter_id($username);
$user->$setter_token($response->getAccessToken());
$this->userManager->updateUser($user);
}
/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$username = $response->getUsername();
$user_details= $response->getResponse();
$user = $this->userManager->findUserBy(array('email' => $user_details['email']));
//when the user is registering
if (null === $user) {
$em = $this->container->get('doctrine')->getManager();
$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
$avatar = $response->getProfilePicture();
$user->setUsername($username);
$user->setFirstName($user_details['first_name']);
$user->setLastName($user_details['last_name']);
$user->setUserKey($this->generateRandomString());
$user->setEmail($user_details['email']);
$user->setPassword($this->generateRandomString());
$user->setEnabled(true);
$this->userManager->updateUser($user);
return $user;
}
$serviceName = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($serviceName) . 'AccessToken';
//update access token
$user->$setter($response->getAccessToken());
return $user;
}
在我的config.yml中
in my config.yml
hwi_oauth:
# name of the firewall in which this bundle is active, this setting MUST be set
connect:
account_connector: my_user_provider
fosub:
username_iterations: 30
properties:
# these properties will be used/redefined later in the custom FOSUBUserProvider service.
facebook: facebook_id
firewall_name: main
resource_owners:
facebook:
type: facebook
client_id: ******
client_secret: *******
scope: "email,public_profile"
options:
display: popup
我的services.xml
my services.xml
<services>
<service id="my_user_provider" class="Acme\UserBundle\Security\Core\User\FOSUBUserProvider">
<argument type="service" id="fos_user.user_manager"/>
<argument type="service" id="service_container" />
<argument type="collection">
<argument key="facebook">facebook_id</argument>
<argument key="google">google_id</argument>
<argument key="twitter">twitter_id</argument>
</argument>
</service>
推荐答案
我从未使用过HWIOAuthBundle,但我假设您正在使用$container
参数注入服务容器.因此,我们可以访问服务容器,只需使用它即可访问request_stack
服务并从中获取查询字符串参数.
I have never worked with HWIOAuthBundle but I assume that you're injecting service container with $container
argument. So, we have an access to service container we can simply access to request_stack
service with it and get query string paramters from it..
示例:
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook', 'myStatus':1 }) }}" />facebook</a>
连接方法:
public function connect(UserInterface $user, UserResponseInterface $response)
{
$property = $this->getProperty($response);
$username = $response->getUsername();
$request = $this->container->get('request_stack')->getCurrentRequest();
$myStatus = $request->query->get('myStatus');
这篇关于通过symfony2/HWIOAuthBundle将参数设置为OAuth facebook登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!