Hi all.

I have successfully installed and configured the simplesaml_auth module to allow users to sign in via an external login to view certain pages on my Drupal site. The part I am struggling with is how do I redirect the user to the set of certain pages after they have logged in via the federated login as currently when the user signs in they are directed to a user profile page on my site as I am assuming that is a default setting? 

Any help is welcome.

Thanks!

Comments

dibyajyoti.mallick’s picture

Hi,

Just put below snippet of code any of your custom module, module page:

Let say your custom module name is my_module then your module name should be my_module.module

function my_module_user_login($account) { /*Here my_module is your module name*/
 $url = "/home"; /* Your redirected path*/
 $response = new Symfony\Component\HttpFoundation\RedirectResponse($url);
 $response->send();
 return;

Then clear the Drupal cache from backend: Admin > configuration >  Development > Performance

Thanks

abhaloda2’s picture

Thanks very much for your reply.

Is there a way to redirect after SSO login without having to create a new module? Say maybe edit some code in the simplesaml_auth module? 

dibyajyoti.mallick’s picture

No need to create any module for that. You can use any existing module(Any contributed module). Make sure that module can use your website 

Just put that code in module file.

Thanks

abhaloda2’s picture

When you say an existing(contributed) module, would that be any module that I use on my website? So could I use the simplesamlphp module? And if so would it matter where in the module file I put the code?

Thanks

dibyajyoti.mallick’s picture

Due you have use any contributed module or create any custom module?

Just check your "modules"  folder and go to that folder you can see all contributed module folder (if you use any)

Every module folder have module file(e.g: moduleName.module), just open it and put that code on below and modify as mention above.

Thanks

abhaloda2’s picture

In the modules folder I have the following folder structure:

  • Contrib
  • Contrib_mod
  • cors
  • custom
  • facebook_pixel
  • login_destination
  • nodeaccess
  • simplesamlphp_auth
  • workbench_scheduler

In the contrib folder there are a load of modules such as block_class, google_analytics, menu_block etc.

I think the custom modules are in the custom folder. 

So what you are saying is edit one of the modules in custom modules with the code you have given?

Does it matter which module I choose and where on the page I paste the code?

Won't it have an affect on that custom module?

Thanks

dibyajyoti.mallick’s picture

Not affect any of your code if you use any module or custom module

Any way go to "simplesamlphp_auth" module open simplesamlphp_auth.module file

At end of the page just pest below snippet of code:

function simplesamlphp_auth_user_login($account) {
  $url = "/home"; /* Your redirected path*/
  $response = new Symfony\Component\HttpFoundation\RedirectResponse($url);
  $response->send();
  return;
 }

Save it and make sure clear your drupal cache from backend.

Thanks

abhaloda2’s picture

Thanks very much.

I'll try this and see if it works and get back to you.

abhaloda2’s picture

Hi.

I tried the above and I got an error message saying the class in simplesamlphp_auth on line 704 does not exist?

Is there a way to do this redirect without adding any code to any of the modules? Is there no way I could amend some code in the simplesamlphp_auth module for example just adding a URL as currently when I login via the SSO method I am redirected to a user profile page.

Thanks 

dibyajyoti.mallick’s picture

Hi,

I don't know how to use this code because I already implemented this module in my last project and its working fine.

Even I face same problem when I was worked on, then I can use this function and it's worked perfectly.

Could you please try to use any of your custom module and make sure change the function name accordingly and also clear the cache 

Thanks

abhaloda2’s picture

Hi 

I tried the bit of code in a custom module but got the same error message. Fatal error: Class 'Symfony\Component\HttpFoundation\RedirectResponse' not found.

Thanks

abhaloda2’s picture

Hi 

I'm not sure if it makes a difference but I'm using Drupal 7. Should that code working Drupal 7 or is it designed for Drupal 8?

I tried the bit of code in a custom module and a normal module but got the same error message. Fatal error: Class 'Symfony\Component\HttpFoundation\RedirectResponse' not found.

Thanks

abhaloda2’s picture

Unfortunately that's for Drupal 8 and I'm currently using Drupal 7

mmjvb’s picture

See https://www.drupal.org/project/simplesamlphp_auth/issues/1912132

Trying to point out ?ReturnTo=#url# !

abhaloda2’s picture

I've tried looking into that. The simplesaml_ auth module has the following code in one of the files 

// Support for deep linking.

  // See if a URL has been explicitly provided in ReturnTo. If so, use it (as
  // long as it points to this site).
  if ((isset($_REQUEST['ReturnTo']) && $_REQUEST['ReturnTo']) &&
    (valid_url($_REQUEST['ReturnTo']) && stristr($_REQUEST['ReturnTo'], $base_url))) {

    $return_to = $_REQUEST['ReturnTo'];

    // If not, see if a REFERER URL is available. If so, use it (as long as it
    // points to this site).
  }
  elseif ((isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER']) &&
    (valid_url($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'], $base_url))
  ) {

I think this is what I need to edit but I'm not entirely sure how. 

Any ideas?

abhaloda2’s picture

That won't work for me as my Simplesamlphp installation and Drupal website are on the same vhost

dibyajyoti.mallick’s picture

Hi,

This code is only for Drupal 8. If you want it to Drupal 7 then you need to change the code accordingly.

Try this code, it can help you:

function mymodulename_user_login(&$edit, $account) {
  if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset') {
    if(in_array('custom role', $account->roles)) {
      $_GET['destination'] = 'custom/page/of/your/choice';
    }
  }
}

Thanks

 

abhaloda2’s picture

I tried adding that code to a custom module. I edited it so the module name matched and also added the URL. The cache were also cleared but this unfortunately did not work. No error message occurred but I was not redirected as soon as I logged in via SSO. Any ideas?

Thanks