通过 symfony2/HWIOAuthBundle 将参数指向 OAuth facebook 登录
To path the parameter to OAuth facebook login by symfony2/HWIOAuthBundle
这个问题与article
相关
我谷歌了很多次,把问题弄得更准确了。
我的问题很简单
我可以从这里传递参数吗(例如:myStatus=1)
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook' }) }}" />facebook</a>
到这个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;
}
在我的 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
<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
服务并从中获取查询字符串参数..
示例:
<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');
这个问题与article
相关我谷歌了很多次,把问题弄得更准确了。
我的问题很简单
我可以从这里传递参数吗(例如:myStatus=1)
<a href="{{ path('hwi_oauth_service_redirect', { 'service' : 'facebook' }) }}" />facebook</a>
到这个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;
}
在我的 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
<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
服务并从中获取查询字符串参数..
示例:
<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');