如何从 UserProvider 类访问 EntityManager

发布于 2024-12-12 07:47:35 字数 1630 浏览 0 评论 0原文

我想从 UserProviderClass 访问 EntityManager,以便能够在用户通过 API 登录后立即将用户保留在我的数据库中。

但是这个类实现了UserProviderInterface并且不扩展Controller,所以这个代码无法工作:

$em = $this->getDoctrine()->getEntityManager();

这是完整的类

<?php
namespace am\ContributionBundle\Objects;

use am\ContributionBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class UserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username){

            // Processing to the authentification using curl

            $url = 'exemple.com/api.php?pseudo='.$username;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            $output = json_decode(curl_exec($ch));
            curl_close($ch);

            try {

                $user = // ... looking for user in DB
                // How to ??

                if (null === $user) {
                    // ... user creation
                }

                return $user;

            } catch (\Exception $e) {
                echo $e->getMessage();
                throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
            }
    }

    function supportsClass($class){}

    function refreshUser(UserInterface $user){
       return $this->loadUserByUsername($user->getUsername());
    }
}

如何在这里获取EntityManager的实例?

I would like to acces to the EntityManager from a UserProviderClass for be able to instantly persist users in my database after they log-in via an API.

But this class implements UserProviderInterface and do not extend Controller so this code can't work:

$em = $this->getDoctrine()->getEntityManager();

Here is the full class

<?php
namespace am\ContributionBundle\Objects;

use am\ContributionBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;

class UserProvider implements UserProviderInterface
{
    public function loadUserByUsername($username){

            // Processing to the authentification using curl

            $url = 'exemple.com/api.php?pseudo='.$username;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            $output = json_decode(curl_exec($ch));
            curl_close($ch);

            try {

                $user = // ... looking for user in DB
                // How to ??

                if (null === $user) {
                    // ... user creation
                }

                return $user;

            } catch (\Exception $e) {
                echo $e->getMessage();
                throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username));
            }
    }

    function supportsClass($class){}

    function refreshUser(UserInterface $user){
       return $this->loadUserByUsername($user->getUsername());
    }
}

How can I get an instance of the EntityManager here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必了 2024-12-19 07:47:35

尝试扩展 Symfony\Component\DependencyInjection\ContainerAware。然后你就可以使用容器来查找任何对象。

另请记住将其配置为服务: http://symfony.com/doc/current/书/service_container.html

Try extending Symfony\Component\DependencyInjection\ContainerAware. Then you can use the container to find any object.

Also remember to configure it as a service: http://symfony.com/doc/current/book/service_container.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文