-2

Instead of using one of multiple contributed modules, which deal with redirections from error pages, I'd like to simply redirect users from the pages with other users uids in the path they tried to access to exactly the same pages, but with their own uids.

For example, if a user with uid '3' tried to access a page with the path ssh-keys/user/4/add then she should be redirected to ssh-keys/user/3/add. To achieve the goal I composed the following simple function:

function return_users_page_alter(&$page) {
  $current_path=(current_path());
  // make sure we have 'user' in path.
  if (strpos($current_path, 'user') !== false) { 
    global $user;
    // exclude site team members.
    if (!in_array('administrator', $user->roles) && !in_array('support', $user->roles) && !in_array('billing', $user->roles)) {
        $path_items=explode('/',$current_path);
        foreach ($path_items as $key => $value) {
          if ($value == 'user') {
            $found_uid = $path_items[$key+1];
            break;
          }
        }
        // If user is really different one.
        if ($user->uid != $path_items[$key+1]) {
          $redirect = str_replace($path_items[$key+1], $user->uid, $current_path);
          drupal_goto($redirect);
        }
      }
    }
  }

but, unfortunately, it doesn't work. What is wrong with my code?

0

After reading Clive's comments on 7 - drupal_goto() ignores $path if $_GET["destination"] is set?, I've added added unset($_GET['destination']); just before drupal_goto command and it works now fine:

      unset($_GET['destination']);
      drupal_goto($external, array('external' => TRUE));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.