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

不打扰是莪最后的温柔 提交于 2019-12-01 21:36:58

I believe it's the securitycontext which has been deprecated / removed. isGranted needs to be called on the authorization checker

return $this->get('security.authorization_checker');

You need the 'security.authorization_checker' service.

You then call isGranted on the authorization_checker service.

// get the service from the container or pass it in via injection
$authChecker = $this->get('security.authorization_checker');
if ($authChecker->isGranted('IS...')) { ... }

I used rector for easier migration. I would highly recommend https://github.com/rectorphp/rector for smooth migration. I can guarantee you will save lots of time by using this tool.

https://www.tomasvotruba.cz/blog/2019/02/28/how-to-upgrade-symfony-2-8-to-3-4/

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')) {

}

Issue was with the priority order.

thanks @cerad for giving a clue about it

bin/console debug:event-dispatcher kernel.request

helped to solve the issue. i was using

tags:
        - { name: kernel.event_listener, event: kernel.response, method: onKernelResponse, priority: 10 }

in Services.yml and it had a conflict with the

getSubscribedEvents()

therefore i have removed tags and only kept

public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::REQUEST  => array('onKernelRequest', 10),
        );
    }

then i moved authentication listener to the down by giving high priority to other two listeners as same as it was there in symfony 2.8

Thanks all for helping me out on this specially @Pie @Cerad and @BoShurik

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!