通过多个提供商的用户名查找用户[symfony2] [英] find user by username from multiple providers [symfony2]

查看:122
本文介绍了通过多个提供商的用户名查找用户[symfony2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据用户名或emailadres在symfony2中找到一个用户对象.这不是用于登录,而是用于用户的其他操作.

I need to find a user object in symfony2 based on the username or emailadres. This is not for loggin in, but for other actions on a user.

我可以简单地请求(Doctrine2)存储库,然后从我的存储库类上的UserProviderInterface调用方法loadByUsername.

I can simply request the (Doctrine2) repository and call the method loadByUsername from the UserProviderInterface that is on my repository-class.

但是需要执行此操作的代码将在多个项目中使用,我需要使其更加通用.用户类/表可能不同,或者用户可能来自完全不同的提供程序类型.

But the code that needs to do this will be used in multiple projects and I need it to be a bit more generic. The user class/table might be different or the users might come from a completely different type of provider.

有没有一种方法可以像Symfony2本身在登录时所使用的那样,通过用户名查找用户? 这样,无论在security.yml中如何配置用户提供程序,它都将起作用.

Is there a way to find a user by username just like Symfony2 itself uses when logging in? This way it will work no matter how the user providers are configured in security.yml.

我可以在Symfony2中使用某些服务吗? 是否有一个用户提供程序服务",我可以在其中调用诸如"loadUserByUsername"之类的方法来尝试配置的每个提供程序?

Is there some service in Symfony2 I can use for this? Is there a "user provider service" where I can call a method something like "loadUserByUsername" that will try each configured provider?

推荐答案

深入研究Symfony本身的SecurityBundle后,我发现了以下内容:

After some poking into the SecurityBundle of Symfony itself, I figured out the following:

鉴于此位于您的security.yml中:

Given this is in your security.yml:

providers:
    AdministrationUser:
        entity:
            class: AdministrationBundle\Entity\User

Symfony将使用以下名称创建服务:

Symfony will create a service with the following name:

security.user.provider.concrete.administrationuser

此服务使用UserProviderInterface,当您获取此服务时,您只需调用loadUserByName方法并找到您的用户即可.

This service uses the UserProviderInterface and when you fetch this service you can simply call the method loadUserByName and find your user.

因此,您只需知道自己配置的提供程序的名称,就可以确定并获取服务名称.

So all you need to know is the name of the provider you configured yourself and you can determine the service-name and fetch it.

我的情况更一般,因此我在捆绑包的扩展类中为该服务添加了一个别名:

I'm in a more generic situation, so I added an alias to that service in the Extension-class of my bundle:

    // alias the user_provider mentioned
    $container->setAlias('my_security_bundle.user.provider', new Alias('security.user.provider.concrete.' . strtolower($config['user']['provider'])));

$ config ['user'] ['provider']来自config.yml(需要在您的Configuration类中进行配置,但这是另一回事.

Where $config['user']['provider'] comes from config.yml (and needs to be configured in your Configuration class, but that is a different story.

现在,我可以简单地使用该新别名,这样我将获得正确的服务以在Controller中找到我的用户:

Now I can simply use that new alias and I will get the correct service to find my user in a Controller like so:

    /** @var UserProviderInterface $userProvider */
    $userProvider = $this->get('my_security_bundle.user.provider');
    $user = $userProvider->loadUserByUsername('someone@somewhere.tld');

这篇关于通过多个提供商的用户名查找用户[symfony2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆