2017-08-01 39 views
1

我有一個在控制器中完美工作的選民,但是當我嘗試在服務中使用它時,它總是返回true,儘管它有一個「return false」語句。選民不在Symfony中工作3.3.4

我看到的唯一區別就是我稱之爲的方式。

在控制器我使用這種方式:

$this->denyAccessUnlessGranted('ver', $menu); 

而且在服務,我把它叫做這樣:

$this->authorizationChecker->isGranted('ver', $menu); 

在服務我注入AuthorizationChecker和它的作品,但它似乎運行其他選民(我只有一個)。

在 「security.yml」 我有這樣的:

access_decision_manager: 
     strategy: unanimous 

投票代碼:

protected function voteOnAttribute($attribute, $subject, TokenInterface $token){ 

    $usuario = $token->getUser(); 

    if (!$usuario instanceof Usuarios) { 
     return false; 
    } 

    /** @var Menu $menu */ 
    $menu = $subject; 


    switch ($attribute) { 
     case self::VER: 
      return false; 
     case self::EDITAR: 
      return false; 
     case self::IMPRIMIR: 
      return false; 
    } 

    throw new \LogicException('This code should not be reached!'); 
} 

誰能幫助我,好嗎?

+0

此版本存在問題,您是否嘗試升級到3.3.5? – COil

+0

我只是升級(不知道有一個新版本),但問題仍然存在:( – Angainor

+0

我猜你的選民實際上並沒有被調用,雖然我看不出爲什麼。也許添加死亡聲明只是爲了驗證。 m假設你的選民被正確標記。 – Cerad

回答

1

好的,我找到了答案。

這個......之間的區別:

$this->denyAccessUnlessGranted('ver', $menu); 

...這...:

$this->authorizationChecker->isGranted('ver', $menu); 

...是他們通知你結果的方式。

第一條語句拋出DenyAccessException異常,但第二條語句返回布爾值(不拋出異常)。

我沒有意識到:)

謝謝大家的幫助。

0
$this->denyAccessUnlessGranted('ver', $menu); 

$this->authorizationChecker->isGranted('ver', $menu); 

會做同樣的工作。

請檢查服務和控制器中的內容$menu

$this->denyAccessUnlessGranted($attributes, $object = null, $message = 'Access Denied.')$this->container->get('security.authorization_checker')->isGranted($attributes, $object)

快捷方式您的自定義選民需要實現VoterInterface或延長選民,這使得建立一個選民更容易。你是否 ?

檢查官方文檔,檢查您的選民的良好實施。 https://symfony.com/doc/current/security/voters.html

+0

它應該是相同的,但它不會返回相同的結果。我的選民擴展選民,它完美地在一個控制器中工作,但它在一個服務中不起作用。 – Angainor