Symfony/Doctrine/SfGuardPlugin:重定向到请求的页面(路由),而不是引用者

发布于 2024-09-03 07:21:25 字数 6790 浏览 4 评论 0原文

我希望能够在登录后将用户带到请求的页面,但 sfGuard 不会发生这种情况。 ** 我的注册操作需要登录;) **

在列表页面 [http://cim/frontend_dev.php/ ] ->用户单击“注册”链接 [@register = 注册/索引] ->用户被带到 sfGuard 提供的“登录”页面 ->登录后,用户将返回列表页面(而不是注册) 这实在是太烦人了!但合乎逻辑,因为引用者是列表页面。

如何更改逻辑以使 @register 成为引荐来源网址?请帮忙。谢谢

public function executeSignin($request)
  {
    $user = $this->getUser();
    $this->logMessage('Signin>>> form - isAuth() '.$user->isAuthenticated(), 'info');

    if ($user->isAuthenticated())
    {
        $this->getUser()->setAttribute('tenant', $this->getUser()->getGuardUser()->sfuser->Tenant->getID());
        return $this->redirect($user->getReferer($request->getReferer()));
    }

    $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin'); 
    $this->form = new $class();
    $referer = $user->getReferer($request->getReferer());
    $this->logMessage('Signin>>> referer: '.$referer, 'info');
    $this->logMessage('Signin>>> referer: '.$request->getReferer(), 'info');

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('signin'));
      if ($this->form->isValid())
      {
        $values = $this->form->getValues(); 
        $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);
        $this->getUser()->setAttribute('tenant', $this->getUser()->getGuardUser()->sfuser->Tenant->getID());

        $this->logMessage('Signin>>> sfUrl | @homepage: '.sfConfig::get('app_sf_guard_plugin_success_signin_url','@homepage'), 'info');
        return $this->redirect("" != $referer ? $referer : sfConfig::get('app_sf_guard_plugin_success_signin_url','@homepage'));
      }
    }
    else
    {
      if ($request->isXmlHttpRequest())
      {
        $this->getResponse()->setHeaderOnly(true);
        $this->getResponse()->setStatusCode(401);

        return sfView::NONE;
      }

      // if we have been forwarded, then the referer is the current URL
      // if not, this is the referer of the current request
      $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

      $this->logMessage('Signin>>> oldy: '.$request->getUri(), 'info');
      $this->logMessage('Signin>>> oldy: '.$request->getReferer(), 'info');

      $module = sfConfig::get('sf_login_module');
      if ($this->getModuleName() != $module)
      {
        return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
      }

      $this->getResponse()->setStatusCode(401);
    }
  }

跟踪:

May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signin" (/login)
May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signout" (/logout)
May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_password" (/request_password)
May 27 10:10:14 symfony [info] {sfPatternRouting} Match route "register" (/register) for /register with parameters array (  'module' => 'register',  'action' => 'index',)
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfGuardRememberMeFilter"
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfRenderingFilter"
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfExecutionFilter"
May 27 10:10:14 symfony [info] {registerActions} Call "registerActions->executeIndex()"
May 27 10:10:14 symfony [info] {sfFrontWebController} Redirect to "http://cim/frontend_dev.php/login"
May 27 10:10:14 symfony [info] {sfWebResponse} Send status "HTTP/1.1 302 Found"
May 27 10:10:14 symfony [info] {sfWebResponse} Send header "Location: http://cim/frontend_dev.php/login"
May 27 10:10:14 symfony [info] {sfWebResponse} Send header "Content-Type: text/html; charset=utf-8"
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Configuration 13.39 ms (9)
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Factories 50.02 ms (1)
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Action "register/index" 1.94 ms (1)
May 27 10:10:14 symfony [info] {sfWebResponse} Send content (104 o)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signin" (/login)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signout" (/logout)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_password" (/request_password)
May 27 10:10:16 symfony [info] {sfPatternRouting} Match route "sf_guard_signin" (/login) for /login with parameters array (  'module' => 'sfGuardAuth',  'action' => 'signin',)
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfGuardRememberMeFilter"
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfRenderingFilter"
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfExecutionFilter"
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Call "sfGuardAuthActions->executeSignin()"
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> form - isAuth() 
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> referer: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> referer: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> oldy: http://cim/frontend_dev.php/login
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> oldy: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfPHPView} Render "D:/projects/cim/plugins/sfDoctrineGuardPlugin/modules/sfGuardAuth/templates/signinSuccess.php"
May 27 10:10:16 symfony [info] {sfPHPView} Decorate content with "D:\projects\cim\apps\frontend\templates/layout.php"
May 27 10:10:16 symfony [info] {sfPHPView} Render "D:\projects\cim\apps\frontend\templates/layout.php"
May 27 10:10:16 symfony [info] {main} Get slot "title"
May 27 10:10:16 symfony [info] {sfWebResponse} Send status "HTTP/1.1 401 Unauthorized"
May 27 10:10:16 symfony [info] {sfWebResponse} Send header "Content-Type: text/html; charset=utf-8"
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Configuration 16.06 ms (10)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Factories 50.00 ms (1)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Action "sfGuardAuth/signin" 14.53 ms (1)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} View "Success" for "sfGuardAuth/signin" 34.44 ms (1)
May 27 10:10:16 symfony [info] {sfWebResponse} Send content (38057 o)

I want to be able to take the user to the requested page after login, but this does not happen with sfGuard.
** My Register action requires SignIn ;) **

On the listing page [http://cim/frontend_dev.php/]
-> user clicks the 'Register' link [@register = register/index]
-> user is taken to 'Signin' page provided by sfGuard
-> after sign-in, user is taken back to the Listing page (instead of Register)
This is quite annoying! But logical, because the referrer is the listing page.

How can I change logic to make @register the referrer? Pl help. thanks

public function executeSignin($request)
  {
    $user = $this->getUser();
    $this->logMessage('Signin>>> form - isAuth() '.$user->isAuthenticated(), 'info');

    if ($user->isAuthenticated())
    {
        $this->getUser()->setAttribute('tenant', $this->getUser()->getGuardUser()->sfuser->Tenant->getID());
        return $this->redirect($user->getReferer($request->getReferer()));
    }

    $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin'); 
    $this->form = new $class();
    $referer = $user->getReferer($request->getReferer());
    $this->logMessage('Signin>>> referer: '.$referer, 'info');
    $this->logMessage('Signin>>> referer: '.$request->getReferer(), 'info');

    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('signin'));
      if ($this->form->isValid())
      {
        $values = $this->form->getValues(); 
        $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);
        $this->getUser()->setAttribute('tenant', $this->getUser()->getGuardUser()->sfuser->Tenant->getID());

        $this->logMessage('Signin>>> sfUrl | @homepage: '.sfConfig::get('app_sf_guard_plugin_success_signin_url','@homepage'), 'info');
        return $this->redirect("" != $referer ? $referer : sfConfig::get('app_sf_guard_plugin_success_signin_url','@homepage'));
      }
    }
    else
    {
      if ($request->isXmlHttpRequest())
      {
        $this->getResponse()->setHeaderOnly(true);
        $this->getResponse()->setStatusCode(401);

        return sfView::NONE;
      }

      // if we have been forwarded, then the referer is the current URL
      // if not, this is the referer of the current request
      $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

      $this->logMessage('Signin>>> oldy: '.$request->getUri(), 'info');
      $this->logMessage('Signin>>> oldy: '.$request->getReferer(), 'info');

      $module = sfConfig::get('sf_login_module');
      if ($this->getModuleName() != $module)
      {
        return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
      }

      $this->getResponse()->setStatusCode(401);
    }
  }

Trace:

May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signin" (/login)
May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signout" (/logout)
May 27 10:10:14 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_password" (/request_password)
May 27 10:10:14 symfony [info] {sfPatternRouting} Match route "register" (/register) for /register with parameters array (  'module' => 'register',  'action' => 'index',)
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfGuardRememberMeFilter"
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfRenderingFilter"
May 27 10:10:14 symfony [info] {sfFilterChain} Executing filter "sfExecutionFilter"
May 27 10:10:14 symfony [info] {registerActions} Call "registerActions->executeIndex()"
May 27 10:10:14 symfony [info] {sfFrontWebController} Redirect to "http://cim/frontend_dev.php/login"
May 27 10:10:14 symfony [info] {sfWebResponse} Send status "HTTP/1.1 302 Found"
May 27 10:10:14 symfony [info] {sfWebResponse} Send header "Location: http://cim/frontend_dev.php/login"
May 27 10:10:14 symfony [info] {sfWebResponse} Send header "Content-Type: text/html; charset=utf-8"
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Configuration 13.39 ms (9)
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Factories 50.02 ms (1)
May 27 10:10:14 symfony [info] {sfWebDebugLogger} Action "register/index" 1.94 ms (1)
May 27 10:10:14 symfony [info] {sfWebResponse} Send content (104 o)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signin" (/login)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_signout" (/logout)
May 27 10:10:16 symfony [info] {sfPatternRouting} Connect sfRoute "sf_guard_password" (/request_password)
May 27 10:10:16 symfony [info] {sfPatternRouting} Match route "sf_guard_signin" (/login) for /login with parameters array (  'module' => 'sfGuardAuth',  'action' => 'signin',)
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfGuardRememberMeFilter"
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfRenderingFilter"
May 27 10:10:16 symfony [info] {sfFilterChain} Executing filter "sfExecutionFilter"
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Call "sfGuardAuthActions->executeSignin()"
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> form - isAuth() 
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> referer: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> referer: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> oldy: http://cim/frontend_dev.php/login
May 27 10:10:16 symfony [info] {sfGuardAuthActions} Signin>>> oldy: http://cim/frontend_dev.php/
May 27 10:10:16 symfony [info] {sfPHPView} Render "D:/projects/cim/plugins/sfDoctrineGuardPlugin/modules/sfGuardAuth/templates/signinSuccess.php"
May 27 10:10:16 symfony [info] {sfPHPView} Decorate content with "D:\projects\cim\apps\frontend\templates/layout.php"
May 27 10:10:16 symfony [info] {sfPHPView} Render "D:\projects\cim\apps\frontend\templates/layout.php"
May 27 10:10:16 symfony [info] {main} Get slot "title"
May 27 10:10:16 symfony [info] {sfWebResponse} Send status "HTTP/1.1 401 Unauthorized"
May 27 10:10:16 symfony [info] {sfWebResponse} Send header "Content-Type: text/html; charset=utf-8"
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Configuration 16.06 ms (10)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Factories 50.00 ms (1)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} Action "sfGuardAuth/signin" 14.53 ms (1)
May 27 10:10:16 symfony [info] {sfWebDebugLogger} View "Success" for "sfGuardAuth/signin" 34.44 ms (1)
May 27 10:10:16 symfony [info] {sfWebResponse} Send content (38057 o)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天的雪花 2024-09-10 07:21:25

我认为获取引荐来源网址的最佳方法是 $request->getReferer()
到目前为止,它对我来说效果很好!

I think the best way to get the referrer is $request->getReferer()
So far, it has worked well for me!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文