symfony security guard with locale not working

521 views Asked by At

So i have this in Controller:

/**
 * @Route("/", name="home", defaults={"_locale": "%locale%"})
 * @Route("/{_locale}", name="home_locale", defaults={"_locale": "%locale%"}, requirements={"_locale": "\w{2,3}"})
 *
 * @param Request $request
 *
 * @return Response
 */
public function indexAction(Request $request)
{
    return $this->render('@Base/Default/index.html.twig');
}

/**
 * @Route("/{_locale}/welcome", name="welcome", defaults={"_locale": "%locale%"}, requirements={"_locale": "\w{2,3}"})
 *
 * @param Request $request
 *
 * @return RedirectResponse|Response
 */
public function loginAction(Request $request)
{
    $loggedUser = $this->getUser();

    $locale = $request->getLocale();

    if ($loggedUser instanceof User) {
        return $this->redirect($this->generateUrl('home', ['_locale' => $locale]));
    }

    $error = $this->get('security.authentication_utils')->getLastAuthenticationError();

    $loginForm = $this->createForm(LoginType::class, null, ['action' => $this->generateUrl('welcome', ['_locale' => $locale]), 'method' => 'POST']);

    $user = new User();
    $registrationForm = $this->createForm(UserRegistrationType::class, $user, ['action' => $this->generateUrl('register'), 'method' => 'POST']);

    return $this->render(
        '@Base/Default/login.html.twig',
        [
            'error' => $error,
            'loginForm' => $loginForm->createView(),
            'registerForm' => $registrationForm->createView()
        ]
    );
}

and this in security:

encoders:
    BaseBundle\Entity\User: bcrypt

# http://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
    database_users:
        entity: { class: BaseBundle:User, property: username }

firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        pattern: ^/
        anonymous: ~
        guard:
            authenticators:
                - base.security.login_form_authenticator
        logout:
            path: /logout

access_control:
    - { path: ^/%locales%/welcome, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/%locales%/register$, roles: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/*, roles: ROLE_USER }

in parameters the locales are like this:

locale: ro
locales: ro|en

in guard the functions to redirect are:

 /**
 * @return mixed
 */
protected function getLoginUrl()
{
    /** @var RequestContext $locale */
    $requestContext = $this->router->getContext();

    $locale = $requestContext->getParameter('_locale');

    return $this->router->generate('welcome', ['_locale' => $locale]);
}

/**
 * @return mixed
 */
protected function getDefaultSuccessRedirectUrl()
{
    /** @var RequestContext $locale */
    $requestContext = $this->router->getContext();

    $locale = $requestContext->getParameter('_locale');

    return $this->router->generate('home', ['_locale' => $locale]);
}

The scenario is next, on the left will be path and the right what is happening:

/     ->    i am redirected to /ro/welcome  witch is correct

/en   ->    i am redirected to /en/welcome witch is also correct

/ro/welcome -> is working

/en/welcome -> is working

/ro   ->   i am NOT redirected to /ro/welcome witch is NOT good

After I login instead of keeping locale ro I am redirected to en, also if I try to access /en/welcome logged I am redirected to /?_locale=en (which is showing ro translation, so not working) instead of /en

Does anyone have any idea why this is happening?

1

There are 1 answers

0
Ion Ghițun On

I found the problem, in security instead of path: ^/%locales%/welcome the corect path is path: ^/(%locales%)/welcome