Search code examples
doctrine-ormfosuserbundlehwioauthbundlesymfony3

extend UserManager to search User by relation (symfony3)


I installed FOSUserbundle and HWI Oauth bundle. My problem is: I want to access data from my user entity that is stored in a relation. I'd like to access the data from the fields social_network_slug and social_identifier from UserInSocialNetworks within the FOSUserProvider. The idea was, that one user can have more that one social network logins. (1:n)- When I log in with my google/facebook etc login, I want to check the table user_in_social_networks if the Id with the social network already exists.

/*
 * This is the User class, depending on fos_userBundle
 */

namespace AppBundle\Entity\Registration;

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

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

    /**
     * One User can have many social networks
     * @ORM\OneToMany(targetEntity="UserInSocialNetworks", mappedBy="user", cascade={"remove"})
     */
    private $socialnetworks; ....

the Entity Class to store all User's social media logins:

<?php

namespace AppBundle\Entity\Registration;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;



/**
 * UserInSocialNetworks
 *
 * @ORM\Table(name="user_in_social_networks")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\Registration\UserInSocialNetworksRepository")
 */
class UserInSocialNetworks
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * Many Socialnetwork Logins have one User
     * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Registration\User", inversedBy="socialnetworks")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     *
     */
    private $user;

    /**
     * @var int
     *
     * @ORM\Column(name="social_network_slug", type="string", length=255, nullable=true)
     */
    private $socialNetworkSlug;

    /**
     * @var string
     *
     * @ORM\Column(name="social_identifier", type="string", length=255, nullable=true)
     */
    private $socialIdentifier;

The extended FOSUBUserProvider class:

    <?php
    namespace AppBundle\Entity\Registration;

    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 loadUserByOAuthUserResponse(UserResponseInterface $response)
        {
            // get user_id and socialnetworkname from response
            $userIdInSocialNetwork = $response->getUsername();
            $socialnetwork = $response->getResourceOwner()->getName();
// Here I'd like to search for an existing $userIdInSocialNetwork

What I checked since now: I can't access the entitymanager in FOSUBUserProvider class, and I can't search that way:

$user = $this->userManager->findUserBy(array(
                'socialIdentifier' => $userIdInSocialNetwork,
                'social_network_slug' => $socialnetwork)

because it's a relation. Thanks for any idea!


Solution

  • As you mentioned that you have extended FOSUBUserProvider i assume you have defined a new service for this, If so then you can pass doctrine's entity manager to your class @doctrine.orm.entity_manager. Following HWIOAuthBundle documentation for FOSUserBundle you can pass entity manager as

    services:
        my.custom.user_provider:
            class:        MyBundle\Security\Core\User\MyFOSUBUserProvider
            arguments: ['@fos_user.user_manager', { facebook: facebook_id }, @doctrine.orm.entity_manager]
    

    And then in your class you can use this service as

    use Doctrine\ORM\EntityManager;
    use FOS\UserBundle\Model\UserManagerInterface;
    //.... other use statements
    
    class FOSUBUserProvider extends BaseClass
    {
        private $em;
    
        public function __construct(UserManagerInterface $userManager, array $properties, EntityManager $em)
        {
            $this->em = $em;
            parent::__construct($userManager, $properties); /* pass dependencies to parent */
        }
    
        public function loadUserByOAuthUserResponse(UserResponseInterface $response)
        {
            $this->em->getRepository('AppBundle\Entity\Registration\UserInSocialNetworks')->findBy(....);
    
            /* Do your stuff here */
        }
    }