简体   繁体   English

您如何将Symfony存储库注入到自定义用户提供程序中?

[英]How do you inject a Symfony repository into a custom user provider?

I have successfully injected repositories into services before, but can't get injection to work in a custom user provider. 我之前已经成功地将存储库注入了服务,但是无法在自定义用户提供程序中工作。 It seems like the __construct() method is never getting called, so the repository is never made available. 似乎从未调用过__construct()方法,因此该存储库从未可用。 The way you are supposed to make a user provider is to implement the interface UserProviderInterface, but the UserProvider class you create doesn't extend another class that would have a constructor. 应该使用户提供程序的方式是实现接口UserProviderInterface,但是您创建的UserProvider类不会扩展具有构造函数的另一个类。 When I try to access the repository using $this->personRepository->findStatusByUsername($username), I get the error: 当我尝试使用$ this-> personRepository-> findStatusByUsername($ username)访问存储库时,出现错误:

Using $this when not in object context 不在对象上下文中时使用$ this

One reason I think the constructor is never getting called is that it has two assignments to $this, but I'm not getting an error message about them. 我认为构造函数从未被调用的原因之一是它有两个给$ this的赋值,但是我没有得到关于它们的错误消息。

I need the Person repository to be injected so that I can check whether or not the person trying to authenticate is already approved in the app (status in the Person table = approved.) 我需要注入人员资料库,以便可以检查应用程序中是否已批准尝试进行身份验证的人员(人员表中的状态=批准)。

My services.yml file has these settings: 我的services.yml文件具有以下设置:

parameters:
  ginsberg_transportation.user.class: Ginsberg\TransportationBundle\Services\User
  user_provider.class: Ginsberg\TransportationBundle\Security\User\UserProvider

services:
  ginsberg_user:
    class: "%ginsberg_transportation.user.class%"
  user_provider:
    class: "%user_provider.class%"
    arguments:
      ["@ginsberg_person.person_repository", "@logger"]
  ginsberg_person.person_repository:
    class: Doctrine\ORM\EntityRepository
    factory_service: doctrine.orm.default_entity_manager
    factory_method: getRepository
    arguments:
      - Ginsberg\TransportationBundle\Entity\Person

My UserProvider.php class starts like this: 我的UserProvider.php类开始如下:

namespace Ginsberg\TransportationBundle\Security\User;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Ginsberg\TransportationBundle\Entity\Person;
use Doctrine\ORM\EntityRepository;

class UserProvider implements UserProviderInterface
{
  private $personRepository;
  private $logger;

  public function __construct(\Ginsberg\TransportationBundle\Entity\PersonRepository $personRepository, \Monolog\Logger $logger) {
    $this->personRepository = $personRepository;
    $this->logger = $logger;
  }

    public function loadUserByUsername($uniqname)
    {
      $password = "admin";
      $salt = "";
      $roles = array();

      if (self::is_authenticated() && self::is_approved()) {
        if (self::is_superuser()) {
          $roles[] = 'ROLE_SUPER_ADMIN';
        } elseif (self::is_admin()) {
          $roles[] = 'ROLE_ADMIN';
        } elseif (self::is_eligible() && self::is_approved()) {
          $roles[] = 'ROLE_USER';
        }

        return new User($uniqname, $password, $salt, $roles);
      }

      throw new UsernameNotFoundException(
      sprintf('Username "%s" does not exist.', $uniqname));
    }
...
    public static function is_approved()
    {
      $uniqname = self::get_uniqname();
      $status = $this->personRepository->findStatusByUsername($username);

      return($status == 'approved') ? TRUE : FALSE;      
    }

I also tried using property injection too, but that didn't work either. 我也尝试过使用属性注入,但这也不起作用。

Should I just make my UserProvider class extend another appropriate class that would make use of a constructor? 我是否应该让UserProvider类扩展使用构造函数的另一个合适的类? If so, any thoughts on what class would be fitting? 如果是这样,对什么课程合适的想法?

Thanks for any suggestions. 感谢您的任何建议。

The problem is that the method is_approved() is static . 问题是方法is_approved()static You cannot use $this in a static context. 您不能在静态上下文中使用$this You need to declare your method non-static. 您需要声明您的方法是非静态的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM