检测控制器symfony2中的access_control

时间:2013-11-26 14:47:49

标签: php symfony redirect cookies fosuserbundle

我想知道用户是否来自security.yml中设置的access_control网址,如果是,则重定向用户,如果没有,则将其重定向到控制器中的自定义网址。目前我有:

security.yml

access_control:
        - { path: ^/subscriptions/.*/.*/subscribe, role: [IS_AUTHENTICATED_FULLY] }

AuthenticationSuccessHandler.php


    use Symfony\Component\Security\Http\HttpUtils;
    use Symfony\Component\HttpFoundation\RedirectResponse;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
    use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationSuccessHandler;


    class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {

        protected $router;

        public function __construct( HttpUtils $httpUtils, array $options, $router) {
        $this->router = $router;
            parent::__construct( $httpUtils, $options );
        }

        public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {


            $referer = $request->cookies->get('last-visited');
    //If i come from an access_control redirection, not redirect to cookie redirection
            if($referer == NULL){
                //Redirect 1.
                 $url = $this->router->generate('myurl');
            }else{
                //Redirect 2.
                $url = $referer;
            }

        return new RedirectResponse($url);
        }
    }

它工作得很好,但是当用户进入这种模式时:^ / subscriptions /。 /。 / subscribe如果它不是autenticated,它会重定向到登录页面,但如果用户有一个cookie将他重定向到cookie中存储的URL。我想首先重定向到access_control,如果没有存储,则转到cookie网址。

0 个答案:

没有答案