循环参考将安全服务注入选民服务

时间:2016-04-26 17:55:27

标签: symfony

我试图将ServiceA(用于获取基于is_granted的实体)注入ServiceB(选民)并获得循环引用。

我相信因为当加载ServiceA时,其authentication_checker依赖尝试加载所有选民,包括ServiceB,这需要ServiceA ......等。

无论如何要解决这个问题?

YAML服务映射:

services:
app.security_service:
    class: AppBundle\Service\appSecurityService
    arguments: [ "@logger", "@doctrine", "@security.authorization_checker", "@security.token_storage" ]
app.entity_voter:
    class: AppBundle\Security\ChargebackNotificationVoter
    public: false
    tags:
        - { name: security.voter }
    arguments: [ "@logger", "@doctrine", "@app.security_service" ]

我在ServiceA中做的一个例子

   public function getEntitiesForUser(UserInterface $user)
{
    $user = $this->tokenStorage->getToken()->getUser();

    if($this->authorizationChecker->isGranted('ROLE_SYSTEM_ADMIN')){
        //If the user has ROLE_SYSTEM_ADMIN get all the entities
        $entitiess = $this->managerRegistry->getRepository('AppBundle:Entities')->findAll();
    }elseif($this->authorizationChecker->isGranted('ROLE_ORGANIZATION_ADMIN')){
        //ElseIf the user has ROLE_ORGANIZATION_ADMIN get all the entitiess that belong to the organization
        $entitiess = $user->getOrganization()->getEntities();
    } elseif($this->authorizationChecker->isGranted('ROLE_USER')) {
        $entitiess = $this->managerRegistry->getRepository('AppBundle:Entities')->findByUser($user);
    } else {
        //if ROLE_USER is missing return null
        $entitiess = null;
    }

    return $entities;
}

..和我得到的错误

  

检测到服务的循环引用" security.authorization_checker",路径:" twig.controller.exception - >树枝 - > security.authorization_checker - > security.access.decision_manager - > ccp.chargebacknotification_voter - > ccp.security_service"

1 个答案:

答案 0 :(得分:1)

您可以尝试使用setter方法将security.authorization_checker(分别为app.security_service)注入app.security_service(分别为app.entity_voter):

services:
app.security_service:
    class: AppBundle\Service\appSecurityService
    arguments: [ "@logger", "@doctrine", "@security.token_storage" ]
    calls:
            - [setAuthorizationChecker, ['@security.authorization_checker']]
app.entity_voter:
    class: AppBundle\Security\ChargebackNotificationVoter
    public: false
    tags:
        - { name: security.voter }
    arguments: [ "@logger", "@doctrine" ]
    calls:
            - [setSecurityService, ['@app.security_service']]

我使用Symfony3