working with twitter oauth
This commit is contained in:
parent
a26fe90062
commit
4a4a0b8596
|
@ -5,7 +5,7 @@
|
|||
<span>{{ error }}</span>
|
||||
{% endif %}
|
||||
|
||||
<a class="btn btn-default btn-block btn-{{ "twitter" }}" href="{{ hwi_oauth_login_url("twitter") }}">
|
||||
<a class="btn btn-default btn-{{ "twitter" }}" href="{{ hwi_oauth_login_url("twitter") }}">
|
||||
<i class="fa fa-{{ "twitter" }}"></i>
|
||||
{{ "twitter" | trans({}, 'HWIOAuthBundle') }}
|
||||
</a>
|
||||
|
|
|
@ -117,7 +117,7 @@
|
|||
{#Via {{ 'disqus' | trans({}, 'HWIOAuthBundle') }}#}
|
||||
{#</a>#}
|
||||
<h1 class="text-right">
|
||||
<a class="btn btn-default btn-block btn-{{ "twitter" }}"
|
||||
<a class="btn btn-default btn-{{ "twitter" }}"
|
||||
href="{{ hwi_oauth_login_url("twitter") }}">
|
||||
<i class="fa fa-{{ "twitter" }}"></i>
|
||||
{{ "twitter" | trans({}, 'HWIOAuthBundle') }}
|
||||
|
|
|
@ -86,6 +86,8 @@ fos_user:
|
|||
hwi_oauth:
|
||||
# list of names of the firewalls in which this bundle is active, this setting MUST be set
|
||||
firewall_names: [secured_area]
|
||||
connect:
|
||||
account_connector: my.custom.user_provider
|
||||
fosub:
|
||||
username_iterations: 30
|
||||
properties:
|
||||
|
|
|
@ -4,10 +4,12 @@ parameters:
|
|||
#parameter_name: value
|
||||
|
||||
services:
|
||||
em:
|
||||
alias: doctrine.orm.entity_manager
|
||||
# default configuration for services in *this* file
|
||||
_defaults:
|
||||
# automatically injects dependencies in your services
|
||||
autowire: true
|
||||
autowire: false
|
||||
# automatically registers your services as commands, event subscribers, etc.
|
||||
autoconfigure: true
|
||||
# this means you cannot fetch services directly from the container via $container->get()
|
||||
|
@ -39,7 +41,11 @@ services:
|
|||
arguments:
|
||||
- '@fos_user.user_manager'
|
||||
- ['@fos_user.user_manager', twitter: twitter_id]
|
||||
- 'doctrine.orm.default_entity_manager'
|
||||
|
||||
my.custom.user_provider:
|
||||
class: AppBundle\Security\Core\User\MyFOSUBUserProvider
|
||||
arguments: ['@fos_user.user_manager', {twitter: twitter_id}]
|
||||
arguments:
|
||||
- '@fos_user.user_manager'
|
||||
- ['@fos_user.user_manager', twitter: twitter_id]
|
||||
- '@doctrine.orm.default_entity_manager'
|
||||
|
|
|
@ -25,11 +25,12 @@ class User extends BaseUser {
|
|||
* @ORM\Column(name="twitter_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
private $twitterId;
|
||||
|
||||
private $twitterAccessToken;
|
||||
/**
|
||||
* @ORM\Column(name="disqus_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
private $disqusId;
|
||||
private $disqusAccessToken;
|
||||
/**
|
||||
* @ORM\Column(name="google_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
|
@ -126,6 +127,34 @@ class User extends BaseUser {
|
|||
$this->googleAccessToken = $googleAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDisqusAccessToken() {
|
||||
return $this->disqusAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $disqusAccessToken
|
||||
*/
|
||||
public function setDisqusAccessToken( $disqusAccessToken ) {
|
||||
$this->disqusAccessToken = $disqusAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getTwitterAccessToken() {
|
||||
return $this->twitterAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $twitterAccessToken
|
||||
*/
|
||||
public function setTwitterAccessToken( $TwitterAccessToken ) {
|
||||
$this->twitterAccessToken = $TwitterAccessToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
|
|
|
@ -25,11 +25,12 @@ class User extends BaseUser {
|
|||
* @ORM\Column(name="twitter_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
private $twitterId;
|
||||
|
||||
private $twitterAccessToken;
|
||||
/**
|
||||
* @ORM\Column(name="disqus_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
private $disqusId;
|
||||
private $disqusAccessToken;
|
||||
/**
|
||||
* @ORM\Column(name="google_id", type="string", length=255, nullable=true)
|
||||
*/
|
||||
|
@ -314,4 +315,48 @@ class User extends BaseUser {
|
|||
public function getActiveFestival() {
|
||||
return $this->activeFestival;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set twitterId.
|
||||
*
|
||||
* @param string|null $twitterId
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function setTwitterId( $twitterId = null ) {
|
||||
$this->twitterId = $twitterId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get twitterId.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getTwitterId() {
|
||||
return $this->twitterId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set disqusId.
|
||||
*
|
||||
* @param string|null $disqusId
|
||||
*
|
||||
* @return User
|
||||
*/
|
||||
public function setDisqusId( $disqusId = null ) {
|
||||
$this->disqusId = $disqusId;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get disqusId.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function getDisqusId() {
|
||||
return $this->disqusId;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,15 +3,39 @@
|
|||
namespace AppBundle\Security\Core\User;
|
||||
|
||||
use AppBundle\Entity\User;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use FOS\UserBundle\Model\UserManagerInterface;
|
||||
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
|
||||
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseFOSUBProvider;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
|
||||
class MyFOSUBUserProvider extends BaseFOSUBProvider {
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* MyFOSUBUserProvider constructor.
|
||||
*
|
||||
* @param UserManagerInterface $userManager
|
||||
* @param array $properties
|
||||
* @param EntityManager $em
|
||||
*/
|
||||
public function __construct(
|
||||
UserManagerInterface $userManager,
|
||||
array $properties,
|
||||
EntityManager $em
|
||||
) {
|
||||
$this->em = $em;
|
||||
parent::__construct( $userManager, $properties );
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function connect( UserInterface $user, UserResponseInterface $response ) {
|
||||
public
|
||||
function connect(
|
||||
UserInterface $user,
|
||||
UserResponseInterface $response
|
||||
) {
|
||||
// get property from provider configuration by provider name
|
||||
// , it will return `facebook_id` in that case (see service definition below)
|
||||
$property = $this->getProperty( $response );
|
||||
|
@ -33,24 +57,54 @@ class MyFOSUBUserProvider extends BaseFOSUBProvider {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadUserByOAuthUserResponse( UserResponseInterface $response ) {
|
||||
public
|
||||
function loadUserByOAuthUserResponse(
|
||||
UserResponseInterface $response
|
||||
) {
|
||||
$userEmail = $response->getEmail();
|
||||
$user = $this->userManager->findUserByEmail( $userEmail );
|
||||
$username = $response->getRealName();
|
||||
$user = null;
|
||||
$em = $this->em;
|
||||
if ( $userEmail ) {
|
||||
$user = $this->userManager->findUserByEmail( $userEmail );
|
||||
} elseif ( $username ) {
|
||||
$user = $this->userManager->findUserByUsername( $username );
|
||||
}
|
||||
|
||||
|
||||
// if null just create new user and set it properties
|
||||
if ( null === $user ) {
|
||||
$username = $response->getRealName();
|
||||
$user = new User();
|
||||
$user->setUsername( $username );
|
||||
|
||||
$user = new User();
|
||||
$data = $response->getData();
|
||||
if ( ! $userEmail ) {
|
||||
$ressourceOwner = $response->getResourceOwner();
|
||||
$userEmail = $username . '@' . $ressourceOwner->getName() . '.com';
|
||||
|
||||
}
|
||||
if ( $ressourceOwner == 'twitter' ) {
|
||||
$userId = $data[ "id" ];
|
||||
$user->setTwitterId( $userId );
|
||||
}
|
||||
$user
|
||||
->setUsername( $username )
|
||||
->setPassword( 'sdfvjsdjfsdsjmldfvlkjsdkjlqlkjef56f4sr46g58s6z8r4g+97sr47hz+4' )
|
||||
->setEmail( $userEmail )
|
||||
->setEmailCanonical( $userEmail );
|
||||
|
||||
// ... save user to database
|
||||
|
||||
$em->persist( $user );
|
||||
$em->flush();
|
||||
|
||||
return $user;
|
||||
}
|
||||
// else update access token of existing user
|
||||
$serviceName = $response->getResourceOwner()->getName();
|
||||
$setter = 'set' . ucfirst( $serviceName ) . 'AccessToken';
|
||||
$user->$setter( $response->getAccessToken() );//update access token
|
||||
$em->persist( $user );
|
||||
$em->flush();
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue