Hasitha Shan
Hasitha Shan

Reputation: 2980

Get current url within security.yml

In my requirement, a user receives an email with a url, once he clicks the user will be navigated to the url via an authentication process.

So to redirect the user to the clicked url I am using the method mentioned here ( Pass parameters when redirect ) where I intend to pass the redirect url as parameter like

login_path: %accounts_host%/signin?redirect=%need_current_url_here%

within the security.yml and capture as such $url=$_GET['redirect']; and provide the redirection accordingly.

My query is how can I access the current url from within the security.yml so that I can attach it to the login_path.

I am quite new to this and any example/ document is very much appreciated. :)

PS

The authentication is done within another symonfy2 application at which point, I cant use referer command as it will be null. That is why I am trying to pass thee redirect url as a parameter. :)

Upvotes: 0

Views: 490

Answers (1)

CSchulz
CSchulz

Reputation: 11030

I would suggest to use an entry point and a success handler.

security.yml:

firewalls:            # Required
    # Examples:
    somename:
        entry_point: some.service.id
        ...
        form_login:
            ...
            success_handler: some.service.id

SuccessHandler (source):

<?php
namespace StatSidekick\UserBundle\Handler;

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

class AuthenticationSuccessHandler extends DefaultAuthenticationSuccessHandler {

    public function __construct( HttpUtils $httpUtils, array $options ) {
        parent::__construct( $httpUtils, $options );
    }

    public function onAuthenticationSuccess( Request $request, TokenInterface $token ) {
        // Create if necessary a redirect response otherwise use the parent one with
        // $response = parent::onAuthenticationSuccess( $request, $token );

        return $response;
    }
}

Entry point (source):

When the user is not authenticated at all (i.e. when the token storage has no token yet), the firewall's entry point will be called to "start" the authentication process. An entry point should implement AuthenticationEntryPointInterface, which has only one method: start() ...

Upvotes: 2

Related Questions