扩展UserManager以按关系搜索用户(symfony3)

时间:2017-12-07 23:32:49

标签: doctrine-orm fosuserbundle hwioauthbundle symfony3.x

我安装了FOSUserbundle和HWI Oauth捆绑包。 我的问题是:我想访问存储在关系中的用户实体的数据。我想从FOSUserProvider中的UserInSocialNetworks访问来自social_network_slug和social_identifier字段的数据。 这个想法是,一个用户可以拥有更多的社交网络登录。 (1:n) - 当我使用google / facebook etc登录时,如果具有社交网络的Id已存在,我想检查表user_in_social_networks。     

/*
 * 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; ....

用于存储所有用户的社交媒体登录的实体类:

<?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;

扩展的FOSUBUserProvider类:

    <?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

我从现在开始检查的内容:我无法访问FOSUBUserProvider类中的实体管理器,我无法以这种方式进行搜索:

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

因为它是一种关系。 谢谢你的任何想法!

1 个答案:

答案 0 :(得分:1)

如你所说,你已经扩展FOSUBUserProvider我假设你为此定义了一项新服务,如果是,那么你可以将教义的实体经理传递给你的班级@doctrine.orm.entity_manager。关于HWIOAuthBundleFOSUserBundle文档,您可以将实体管理器作为

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

然后在您的班级中,您可以将此服务用作

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 */
    }
}