Symfony 2.8 -> 3.4 Upgrade IsGranted('IS_AUTHENTICATED_ANONYMOUSLY') Throws Errors

前端 未结 3 629
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 07:02

I\'m in the process of upgrading Symfony from 2.8 to 3.4 and I have a Authentication Listener.

The constructor of the listener

  public function __co         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-20 07:50

    Inject AuthorizationChecker to your class

    protected $authChecker;
    
    public function __construct(AuthorizationChecker $authChecker)
        {
            $this->authChecker = $authChecker;
        }
    

    By injecting it in your service.yml

    XXXXXXXXX:
        class: App\XXX\XXXX\XXXXX
        arguments: [ "@security.authorization_checker" ]
    

    And then use it to check role using isGranted

    if ($this->authChecker->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
    
    }
    

提交回复
热议问题