问题
This question is related to this article
I have googled many times and made the problem more precisely.
My question is very simple.
Can I pass the parameter from here (for example : myStatus=1)
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook' }) }}" />facebook</a>
to this class?
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;
}
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
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>
回答1:
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..
example:
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook', 'myStatus':1 }) }}" />facebook</a>
connect method:
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');
来源:https://stackoverflow.com/questions/30294363/to-path-the-parameter-to-oauth-facebook-login-by-symfony2-hwioauthbundle