0

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. :)

Community
  • 1
  • 1
Hasitha Shan
  • 2,900
  • 6
  • 42
  • 83
  • @CSchulz Its the same authentication process where I am trying to pass in the url to be redirected after signing as a paramerter within the security.yml. – Hasitha Shan Jan 12 '15 at 08:56
  • It isn't the same like you are calling before redirected to the sigin url? Example: call yourapp/url => yourapp/signin => yourapp/url – CSchulz Jan 12 '15 at 08:57
  • @CSchulz I have tried this using the `referer` method but once it navigates to the signin the referer is always null.so thats why I am trying to pass in the url to be navigated as a parameter. – Hasitha Shan Jan 12 '15 at 09:00
  • Is the url in the email the same as your target after the login? If yes you don't need anything. The security component redirects automatically back to the first called url after success. – CSchulz Jan 12 '15 at 09:01
  • @CSchulz the thing is I have a seperate centralized authentication process. so once the application reached that end the referer is no more. that is the reason I need the redirect url as parameter. and i should have mentioned this within the question. Ill update it – Hasitha Shan Jan 12 '15 at 09:08
  • Have you seen my answer? – CSchulz Jan 26 '15 at 13:06
  • @CSchulz ohh thank u for that i will check on this and post back (y) – Hasitha Shan Jan 26 '15 at 15:05
  • If it helps you please vote, if it is the solution mark it as solution, thanks. – CSchulz Jan 27 '15 at 10:23
  • @CSchulz i did upvote but still am checking on it. I am kinda busy these days. I will mark it as an answer if it helps (y) thanks again – Hasitha Shan Jan 28 '15 at 03:02

1 Answers1

2

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() ...

Community
  • 1
  • 1
CSchulz
  • 10,882
  • 11
  • 60
  • 114