I have a site which contains many links. Now I want to achieve the

following:-

  • Authenticate user in my site opens a in new tab of browser.
  • Authenticate User log out from one of the tab.
  • If user select link on the page that is still opened in tab 2 then it should redirect to login page.

Please provide your valuable input to achieve this functionality.

Comments

Jaypan’s picture

You can implement hook_page_alter (which is called on every page load), and if the user is not logged in, use drupal_goto() to redirect to the front page.

tm0032938’s picture

I have implemented your suggestion and it worked like charm.
However I have two situations in this scenario where I need such advises:-

  • In my website, many links are opened in lightframe. Suppose user log out from my website in one tab and now opens the lightframe link in another tab. After implementing drupal_goto(), it is showing the login page inside the lightframe which I don't want. I want to close the lightframe and redirect to login page. I see that lightframe can be closed using JQuery like this:- " window.parent.Lightbox.end();". How to invoke js from hook_page_alter. Please share with me some piece of code.
  • After user logout from the website and if user login again then they should be able to go directly to the last page they have visited before log out. Any suggestion as how to implement in Drupal 7?
Jaypan’s picture

I don't have an answer for your first scenario, I'd have to sit down and play with it for a while, and I don't have the time to do that at the moment.

For your second scenario, you can append the 'destination' query to the URL that will redirect the user back to the page they were on. You can do that like this, still in hook_page_alter():

function HOOK_page_alter(&$page)
{
  if(user_is_anonymous())
  {
    $query['destination'] = current_path();
    drupal_goto('user_login', array('query' => $query));
  }
}
tm0032938’s picture

Again thanks for your suggestion regarding 2nd problem. It is working fine now.
Regarding first one, I tried to do following but it is not working:-

function HOOK_page_alter(&$page)
{
  if(user_is_anonymous())
  {
    $query['destination'] = current_path();
    drupal_add_js('jQuery(document).ready(function () {window.parent.Lightbox.end();});', 'inline');
    drupal_goto(' ', array('query' => $query));
  }
}

This is not invoking the JS code when page is loaded. Is my approach correct?

Jaypan’s picture

That will not work because the JS is added, but then you are redirecting to another page, which will not have that JS. You would need to add that on the page the user is redirected to. I don't know if that will work though. Probably better to start a new thread.

Also, google 'Managing JavaScript in Drupal 7', as you are not using the D7 JavaScript APIs.