Skip to content

Instantly share code, notes, and snippets.

@danvbe
Last active July 23, 2025 05:55

Revisions

  1. danvbe revised this gist Dec 1, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion User.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    <?php
    namespace danvbe\UserBundle\Entity;

    use FOS\UserBundle\Entity\User as BaseUser;
    use FOS\UserBundle\Model\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;

  2. danvbe revised this gist Nov 30, 2013. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions security.yml
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,20 @@
    #app/config/security.yml
    security:
    encoders:
    FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
    ROLE_ADMIN: ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN
    ROLE_SUPER_ADMIN: ROLE_USER

    providers:
    fos_userbundle:
    id: fos_user.user_manager
    id: fos_user.user_provider.username_email

    firewalls:
    dev:
    pattern: ^/(_(profiler|wdt)|css|images|js)/
    security: false

    main:
    pattern: ^/
    form_login:
    @@ -27,13 +30,12 @@ security:
    failure_path: /login

    oauth_user_provider:
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
    service: my_user_provider
    logout:
    path: /logout
    target: /
    logout: true
    anonymous: true

    login:
    pattern: ^/login$
    security: false
    @@ -48,4 +50,4 @@ security:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, roles: ROLE_SUPER_ADMIN }
    - { path: ^/admin/, role: ROLE_ADMIN }
  3. danvbe revised this gist Nov 30, 2013. 2 changed files with 3 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Here are the steps:
    2. Configuration. I have set the `config.yml` mostly as it is presented in the HWIOAuthBundle.
    3. Security. I have set the `security.yml` mostly as it is presented in the HWIOAuthBundle (though my routes are using `/login` pattern, not `/connect`). Also, the `oauth_user_provider` is set for my custom service.
    4. User. My own User entity, extended from FosUser.
    5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.) and in responsible for connecting already logged in users with accounts from PROVIDERS. It does this by overvriting 2 functions (`connect($user, UserResponseInterface $response)` and `loadUserByOAuthUserResponse(UserResponseInterface $response)`). See code below.
    5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.) and in responsible for connecting already logged in users with accounts from PROVIDERS. It does this by overvriting 2 functions (`connect(UserInterface $user, UserResponseInterface $response)` and `loadUserByOAuthUserResponse(UserResponseInterface $response)`). See code below.
    6. Custom service. My user provider is registered as service.

    Using this code, when:
    3 changes: 2 additions & 1 deletion FOSUBUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -3,14 +3,15 @@

    use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
    use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
    use Symfony\Component\Security\Core\User\UserInterface;

    class FOSUBUserProvider extends BaseClass
    {

    /**
    * {@inheritDoc}
    */
    public function connect($user, UserResponseInterface $response)
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
    $property = $this->getProperty($response);
    $username = $response->getUsername();
  4. danvbe revised this gist Jan 20, 2013. 3 changed files with 25 additions and 7 deletions.
    2 changes: 1 addition & 1 deletion 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ Here are the steps:
    2. Configuration. I have set the `config.yml` mostly as it is presented in the HWIOAuthBundle.
    3. Security. I have set the `security.yml` mostly as it is presented in the HWIOAuthBundle (though my routes are using `/login` pattern, not `/connect`). Also, the `oauth_user_provider` is set for my custom service.
    4. User. My own User entity, extended from FosUser.
    5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.)
    5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.) and in responsible for connecting already logged in users with accounts from PROVIDERS. It does this by overvriting 2 functions (`connect($user, UserResponseInterface $response)` and `loadUserByOAuthUserResponse(UserResponseInterface $response)`). See code below.
    6. Custom service. My user provider is registered as service.

    Using this code, when:
    24 changes: 19 additions & 5 deletions FOSUBUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -12,13 +12,27 @@ class FOSUBUserProvider extends BaseClass
    */
    public function connect($user, UserResponseInterface $response)
    {
    //on connect - get the access token
    $serviceAccessTokenName = $response->getResourceOwner()->getName() . 'AccessToken';
    $serviceAccessTokenSetter = 'set' . ucfirst($serviceAccessTokenName);
    $property = $this->getProperty($response);
    $username = $response->getUsername();

    //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);
    }

    $user->$serviceAccessTokenSetter($response->getAccessToken());
    //we connect current user
    $user->$setter_id($username);
    $user->$setter_token($response->getAccessToken());

    //you may want to get extra data... put code here.
    $this->userManager->updateUser($user);
    }

    6 changes: 5 additions & 1 deletion config.yml
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,11 @@
    #app/config/config.yml

    hwi_oauth:
    connect: ~
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
    #and also, the connecting part (get the token and the user_id)
    connect:
    account_connector: my_user_provider
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main
    fosub:
  5. danvbe revised this gist Jan 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    I have managed to install this… and make it work. I have worked with Facebook and Google, but you can exted it.
    I have managed to install this… and make it work. I implemented it for Facebook and Google, but you can extend it.
    My solution it is mostly as described in [#116](https://github.com/hwi/HWIOAuthBundle/issues/116), with a bit of more code presented. The key aspects that lack in the [#116](https://github.com/hwi/HWIOAuthBundle/issues/116) presentation (IMO) are:
    - the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
    - set the service for `oauth_user_provider` in the `security.yml` with your custom created service
  6. danvbe revised this gist Jan 7, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    I have managed to install this… and make it work. I will present my solution. Mostly it is as described in [#116](https://github.com/hwi/HWIOAuthBundle/issues/116), with a bit of more code presented. The key aspects that lack in the [#116](https://github.com/hwi/HWIOAuthBundle/issues/116) presentation (IMO) are:
    I have managed to install this… and make it work. I have worked with Facebook and Google, but you can exted it.
    My solution it is mostly as described in [#116](https://github.com/hwi/HWIOAuthBundle/issues/116), with a bit of more code presented. The key aspects that lack in the [#116](https://github.com/hwi/HWIOAuthBundle/issues/116) presentation (IMO) are:
    - the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
    - set the service for `oauth_user_provider` in the `security.yml` with your custom created service

  7. danvbe revised this gist Jan 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    I have managed to install this… and make it work. I will present my solution. All the code is available in [this Gist] (https://gist.github.com/4476697). Mostly it is as described in #116, with a bit of more code presented. The key aspects that lack in the #116 presentation (IMO) are:
    I have managed to install this… and make it work. I will present my solution. Mostly it is as described in [#116](https://github.com/hwi/HWIOAuthBundle/issues/116), with a bit of more code presented. The key aspects that lack in the [#116](https://github.com/hwi/HWIOAuthBundle/issues/116) presentation (IMO) are:
    - the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
    - set the service for `oauth_user_provider` in the `security.yml` with your custom created service

  8. danvbe revised this gist Jan 7, 2013. 1 changed file with 19 additions and 0 deletions.
    19 changes: 19 additions & 0 deletions 1-Explanations.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    I have managed to install this… and make it work. I will present my solution. All the code is available in [this Gist] (https://gist.github.com/4476697). Mostly it is as described in #116, with a bit of more code presented. The key aspects that lack in the #116 presentation (IMO) are:
    - the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
    - set the service for `oauth_user_provider` in the `security.yml` with your custom created service

    Here are the steps:

    1. Routing. In `routing.yml` I have added all the routes for both bundles.
    2. Configuration. I have set the `config.yml` mostly as it is presented in the HWIOAuthBundle.
    3. Security. I have set the `security.yml` mostly as it is presented in the HWIOAuthBundle (though my routes are using `/login` pattern, not `/connect`). Also, the `oauth_user_provider` is set for my custom service.
    4. User. My own User entity, extended from FosUser.
    5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.)
    6. Custom service. My user provider is registered as service.

    Using this code, when:

    1. No user is authenticated on my site: by accessing `http://my_app_web_root/login/facebook` or `http://my_app_web_root/login/google`, a user is created in my database (with data as it is saved in the custom FOSUBUserProvider) and it is automatically login-ed to my site.
    2. A user is authenticated on my site: by accessing `http://my_app_web_root/login/facebook` or `http://my_app_web_root/login/google`, the current user is updated with data from the provider (account linking).

    I think this is the behavior everybody was expecting :).
  9. danvbe revised this gist Jan 7, 2013. 4 changed files with 15 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion config.yml
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,13 @@
    #app/config/config.yml

    hwi_oauth:
    connect: ~
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main
    fosub:
    username_iterations: 30
    properties:
    # these properties will be used/redefined later in the custom FOSUBUserProvider.
    # these properties will be used/redefined later in the custom FOSUBUserProvider service.
    facebook: facebook_id
    google: google_id
    resource_owners:
    2 changes: 2 additions & 0 deletions routing.yml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    #app/config/routing.yml

    #FosUserBundle Routes
    fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"
    1 change: 1 addition & 0 deletions security.yml
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #app/config/security.yml
    security:
    encoders:
    FOS\UserBundle\Model\UserInterface: sha512
    9 changes: 9 additions & 0 deletions services.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    #danvbe/UserBundle/Resources/services.yml
    parameters:
    my_user_provider.class: danvbe\UserBundle\Security\Core\User\FOSUBUserProvider

    services:
    my_user_provider:
    class: "%my_user_provider.class%"
    #this is the place where the properties are passed to the UserProvider - see config.yml
    arguments: [@fos_user.user_manager,{facebook: facebook_id, google: google_id}]
  10. danvbe revised this gist Jan 7, 2013. 2 changed files with 99 additions and 0 deletions.
    64 changes: 64 additions & 0 deletions FOSUBUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,64 @@
    <?php
    namespace danvbe\UserBundle\Security\Core\User;

    use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
    use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;

    class FOSUBUserProvider extends BaseClass
    {

    /**
    * {@inheritDoc}
    */
    public function connect($user, UserResponseInterface $response)
    {
    //on connect - get the access token
    $serviceAccessTokenName = $response->getResourceOwner()->getName() . 'AccessToken';
    $serviceAccessTokenSetter = 'set' . ucfirst($serviceAccessTokenName);

    $user->$serviceAccessTokenSetter($response->getAccessToken());

    //you may want to get extra data... put code here.
    $this->userManager->updateUser($user);
    }

    /**
    * {@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;
    }

    }
    35 changes: 35 additions & 0 deletions User.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    <?php
    namespace danvbe\UserBundle\Entity;

    use FOS\UserBundle\Entity\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;

    /**
    * @ORM\Entity(repositoryClass="danvbe\UserBundle\Repository\UserRepository")
    * @ORM\Table(name="lcl_user")
    */
    class User extends BaseUser
    {
    /**
    * @ORM\Id
    * @ORM\Column(type="integer")
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    protected $id;

    /** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
    protected $facebook_id;

    /** @ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
    protected $facebook_access_token;

    /** @ORM\Column(name="google_id", type="string", length=255, nullable=true) */
    protected $google_id;

    /** @ORM\Column(name="google_access_token", type="string", length=255, nullable=true) */
    protected $google_access_token;


    //YOU CAN ADD MORE CODE HERE !
    }
  11. danvbe revised this gist Jan 7, 2013. 2 changed files with 72 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions config.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    hwi_oauth:
    connect: ~
    # name of the firewall in which this bundle is active, this setting MUST be set
    firewall_name: main
    fosub:
    username_iterations: 30
    properties:
    # these properties will be used/redefined later in the custom FOSUBUserProvider.
    facebook: facebook_id
    google: google_id
    resource_owners:
    facebook:
    type: facebook
    client_id: "%facebook_app_id%"
    client_secret: "%facebook_app_secret%"
    scope: ""
    google:
    type: google
    client_id: "%google_app_id%"
    client_secret: "%google_app_secret%"
    scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"
    # here you will add one (or more) configurations for resource owners
    50 changes: 50 additions & 0 deletions security.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    security:
    encoders:
    FOS\UserBundle\Model\UserInterface: sha512

    role_hierarchy:
    ROLE_ADMIN: ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
    fos_userbundle:
    id: fos_user.user_manager

    firewalls:
    main:
    pattern: ^/
    form_login:
    provider: fos_userbundle
    csrf_provider: form.csrf_provider
    login_path: /login
    check_path: /login_check
    oauth:
    resource_owners:
    facebook: "/login/check-facebook"
    google: "/login/check-google"
    login_path: /login
    failure_path: /login

    oauth_user_provider:
    #this is my custom user provider, created from FOSUBUserProvider - will manage the
    #automatic user registration on your site, with data from the provider (facebook. google, etc.)
    service: my_user_provider
    logout:
    path: /logout
    target: /
    anonymous: true
    login:
    pattern: ^/login$
    security: false

    remember_me:
    key: "%secret%"
    lifetime: 31536000 # 365 days in seconds
    path: /
    domain: ~ # Defaults to the current domain from $_SERVER

    access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, roles: ROLE_SUPER_ADMIN }
  12. danvbe revised this gist Jan 7, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion routing.yml
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    FosUserBundle Routes
    #FosUserBundle Routes
    fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

  13. danvbe created this gist Jan 7, 2013.
    38 changes: 38 additions & 0 deletions routing.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    FosUserBundle Routes
    fos_user_security:
    resource: "@FOSUserBundle/Resources/config/routing/security.xml"

    fos_user_profile:
    resource: "@FOSUserBundle/Resources/config/routing/profile.xml"
    prefix: /profile

    fos_user_register:
    resource: "@FOSUserBundle/Resources/config/routing/registration.xml"
    prefix: /register

    fos_user_resetting:
    resource: "@FOSUserBundle/Resources/config/routing/resetting.xml"
    prefix: /resetting

    fos_user_change_password:
    resource: "@FOSUserBundle/Resources/config/routing/change_password.xml"
    prefix: /profile

    #HWIOAuthBundle routes
    hwi_oauth_security:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix: /login

    hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix: /login

    hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix: /login

    facebook_login:
    pattern: /login/check-facebook

    google_login:
    pattern: /login/check-google