dsfphczao23473056 2018-09-21 14:06 采纳率: 100%
浏览 101
已采纳

Symfony 3.4服务 - Doctrine \ ORM \ EntityManager的实例,给定布尔值

I currently have an error with a service since I updated to Symfony 3.4 from 3.0.

I have a SwitchUserListener which receives a couple of objects in its construct but it fails to receive the EntityManager even though i have it Type-Hinted. I also added a public key to my services

I get a FatalThrowableError because the class gets instantiated with a boolean instead of an EntityManager Object.

Error Message

Type error: Argument 10 passed to Dbm\UserBundle\Security\SwitchUserListener::__construct() must be an instance of Doctrine\ORM\EntityManager, boolean given, called in C:\wamp64\www\Portail\var\cache\dev\ContainerWyiblvw\getSecurity_Authentication_SwitchuserListener_MainService.php on line 8

FatalThrowableError Line

SwitchUserListener->__construct(object(TokenStorage), object(EmailUserProvider), object(UserChecker), 'main', object(TraceableAccessDecisionManager), object(Logger), '_switch_user', 'ROLE_ALLOWED_TO_SWITCH', object(TraceableEventDispatcher), false, object(AuthorizationChecker)) in var\cache\dev\ContainerWyiblvw\getSecurity_Authentication_SwitchuserListener_MainService.php

Here is what I have right now.

services.yml

services:
    _defaults:
        public: true
        autowire: true
        autoconfigure: true

    security.authentication.switchuser_listener:
        class: Dbm\UserBundle\Security\SwitchUserListener
        arguments:
            - '@security.token_storage'
            - ~
            - '@security.user_checker'
            - ~
            - '@security.access.decision_manager'
            - '@logger'
            - ''
            - ''
            - '@event_dispatcher'
            - '@doctrine.orm.entity_manager'
            - '@security.authorization_checker'
        tags:
            - { name: monolog.logger, channel: security }

SwitchUserListener.php

namespace Dbm\UserBundle\Security;
...
use Doctrine\ORM\EntityManager;

class SwitchUserListener implements ListenerInterface
{
    private $tokenStorage;
    private $provider;
    private $userChecker;
    private $providerKey;
    private $accessDecisionManager;
    private $usernameParameter;
    private $role;
    private $logger;
    private $dispatcher;
    private $em;
    private $authCheck;

    public function __construct(TokenStorageInterface $tokenStorage,
                                UserProviderInterface $provider,
                                UserCheckerInterface $userChecker,
                                $providerKey,
                                AccessDecisionManagerInterface $accessDecisionManager,
                                LoggerInterface $logger = null,
                                $usernameParameter = '_switch_user',
                                $role = 'ROLE_ALLOWED_TO_SWITCH',
                                EventDispatcherInterface $dispatcher = null,
                                EntityManager $em,
                                AuthorizationChecker $authCheck)
    {
        ...
    }
  • 写回答

1条回答 默认 最新

  • duandaoji3992 2018-09-21 16:34
    关注

    EDIT

    Finally found my error. I was overriding the switchuser_listener from "Symfony\Bundle\SecurityBundle\Resources\config\securityListeners.xml" with my own service and I guess the last args from their list (the 10th one hehe..heh...) which wasn't there before my updates, forced me to have it in my services.yml file too

    security_listeners.xml

        <service id="security.authentication.switchuser_listener" class="Symfony\Component\Security\Http\Firewall\SwitchUserListener" abstract="true">
            <tag name="monolog.logger" channel="security" />
            <argument type="service" id="security.token_storage" />
            <argument /> <!-- User Provider -->
            <argument /> <!-- User Checker -->
            <argument /> <!--  Provider Key -->
            <argument type="service" id="security.access.decision_manager" />
            <argument type="service" id="logger" on-invalid="null" />
            <argument>_switch_user</argument>
            <argument>ROLE_ALLOWED_TO_SWITCH</argument>
            <argument type="service" id="event_dispatcher" on-invalid="null"/>
            <argument>false</argument> <!-- Stateless -->
        </service>
    

    The text below is still valid, it is the right answer to the issue. But the reason why is in this edit.

    END OF EDIT

    I was somehow able to fix this weird issue by setting autowire to false AND I also added an extra parameter where the "false" was (10th args).

    Only by doing so I was able to "Skip" the issue. It's a little dirty to do it like this. But it works.

    The arguments sent by the dependencyInjection were just not correct. (At least, that's what I think)

    This is a workaround, I will not mark it as answered so if anyone finds the actual answer to this I will mark it as the right answer

    Here are the changes

    services.yml I simply added an extra empty args at the 10th position

    services:
        _defaults:
            public: true
            autowire: true
            autoconfigure: true
    
        security.authentication.switchuser_listener:
            class: Dbm\UserBundle\Security\SwitchUserListener
            autowire: false
            arguments: 
                - '@security.token_storage'
                - ~
                - '@security.user_checker'
                - ''
                - '@security.access.decision_manager'
                - '@logger'
                - ''
                - ''
                - '@event_dispatcher'
                - ''  
                - '@doctrine.orm.entity_manager'
                - '@security.authorization_checker'
            tags:
                - { name: monolog.logger, channel: security }
    

    SwitchUserListener.php I simply added an extra param at the 10th position

    public function __construct(TokenStorageInterface $tokenStorage,
                                UserProviderInterface $provider,
                                UserCheckerInterface $userChecker,
                                $providerKey,
                                AccessDecisionManagerInterface $accessDecisionManager,
                                LoggerInterface $logger = null,
                                $usernameParameter = '_switch_user',
                                $role = 'ROLE_ALLOWED_TO_SWITCH',
                                EventDispatcherInterface $dispatcher = null,
                                $buggyParam, //Not doing anything with this
                                EntityManagerInterface $em,
                                AuthorizationChecker $authCheck)
    {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形