[Drupal][Solved] Redirection issue while using FB Connect and Legal Module in Drupal 7

| | 2 min read

For our Drupal site, a requirement were to use Facebook connect module and legal module. Once enabling both the modules and adding legal condition, Facebook connect module let us to login through Facebook on our Drupal site.

After that I tried to login through Drupal login form, it redirects to legal page and works fine. But if I supposed to login through the Facebook connect an infinite loop redirection occurs and it throws an error.
Finally, I have fixed the issue using the code added below.


    diff --git a/sites/all/modules/legal/legal.module b/sites/all/modules/legal/legal.module
    index 657f01e..4314f50 100644
    --- a/sites/all/modules/legal/legal.module
    +++ b/sites/all/modules/legal/legal.module
    @@ -374,12 +374,11 @@ function legal_form_user_profile_form_alter(&$form, $form_state) {
     }
     
     /**
    - * Implements hook_user_login().
    + * Implements hook_init().
      */
    -function legal_user_login(&$edit, $account) {
    +function legal_init() {
       global $user;
       global $language;
    -
       if ($user->uid == 1) {
         return;
       }
    @@ -412,7 +411,7 @@ function legal_user_login(&$edit, $account) {
       }
     
       $uid = $user->uid;
    -
    +  /*
       // Destroy the current session.
       module_invoke_all('user_logout', $user);
       session_destroy();
    @@ -431,7 +430,7 @@ function legal_user_login(&$edit, $account) {
       }
     
       unset($_GET['destination']);
    -
    +  */
       $result    = db_select('users', 'u')
         ->fields('u')
         ->condition('uid', $uid)
    @@ -439,7 +438,30 @@ function legal_user_login(&$edit, $account) {
         ->execute()
         ->fetchAllAssoc('uid');
       $signatory = array_pop($result);
    -
    +  $allow_paths = array(
    +    array('legal_accept', $signatory->uid),
    +    array('user', 'logout'),
    +  );
    +  // To block the infinite loop redirection.
    +  foreach ($allow_paths as $allow_path) {
    +    // This while loop ends in "return" if the path matches.
    +    // If the path does not match, break 2 will loop to the next allowed path
    +    while (1) {
    +      $i = 0;
    +      // Array path components mean logical or (any of)
    +      foreach ($allow_path as $part) {
    +        if (is_array($part) && !in_array(arg($i), $part)) {
    +          break 2;
    +        }
    +        if (!is_array($part) && arg($i) != $part) {
    +          break 2;
    +        }
    +        $i++;
    +      }
    +      // The arg() path matches the one in the $allow_path
    +      return;
    +    }
    +  }
       drupal_goto('legal_accept/' . $signatory->uid . '/' . md5($signatory->name . $signatory->pass . $signatory->login), array('query' => $query));
     }
     
    @@ -629,10 +651,10 @@ function legal_login_submit($form, &$form_state) {
         ->execute();
     
       // User has new permissions, so we clear their menu cache.
    -  cache_clear_all($user->uid, 'cache_menu', TRUE);
    +  //cache_clear_all($user->uid, 'cache_menu', TRUE);
       // Fixes login problems in Pressflow.
    -  drupal_session_regenerate();
    -  user_module_invoke('login', $edit, $user);
    +  //drupal_session_regenerate();
      //user_module_invoke('login', $edit, $user);
     }
     
     function theme_legal_login($variables) {
  

By using the above code I have fixed the redirection issue and now its working fine. Try this ...