2016-04-26 61 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