2016-04-26 41 views
0

我想注入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 - > twig - > security.authorization_checker - > security.access.decision_manager - > ccp.chargebacknotification_voter - > ccp.security_service“。

+0

我不知道是否可以通过使用getRoles解决来自ServiceA中的用户对象,in_array或 - >包含以检查角色,然后我不需要security.auth_checker ...最佳实践文档说使用security.auth_checker来检查角色:/ –

+0

哪个Symfony版本可以你用? – xabbuh

+0

symfony3,我也在IRC频道中询问过,听起来好像只要服务对security.authentication_checker有依赖性,就不能注入到投票人中。我目前的解决方法是在用户对象上添加一个hasRole方法,并使用它来检查我的服务中的角色,该角色允许我删除security.auth_checker依赖项并将其注入到我的投票器中,而不会有任何问题 –

回答

1

您可以尝试security.authorization_checker(RESP app.security_service)到app.security_service使用setter方法注入(RESP 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