I have coded a module which uses hook_user at login to set some user variables. When using masquerade to switch user, hook_user is not called and my variables are not set.

I think a "hook_user('load', $new_account)" call whould be the solution.

Comments

gease’s picture

Title: call hook_user when switching » simulate user login when switching

Yes, this would be great. I have some rules that set user roles at login depending on various conditions, and to check how it all works via masquerade simulating user login is needed. Obviously, hook_user('login') should be called somewhere.

peterx’s picture

@enboig and @gease I am interested in your use of login and rules for possible use in http://drupal.org/node/1416136. The second login has to get all the roles assigned to the fist login. I am looking at custom code. hook_user and/or Rules might be a cleaner way to do things.

enboig’s picture

At hook_boot() I check global $user->uid versus $_SESSION['my_old_uid']; if its different, I reload anything I need and update $_SESSION['my_old_uid'].

function my_module_boot() {
  global $user;
  if ($user->uid <> $_SESSION['mymodule_old_uid']) {
    //my code to reload data
    $_SESSION['mymodule_old_uid']=$user->uid;
  }
}

peterx’s picture

Sounds like your code would have to run after everything else that modifies the user. For my use, it would be something like:
$old = load user $_SESSION['mymodule_old_uid']
$user->roles = array_merge($user->roles, $old->roles);

apaderno’s picture

Version: 6.x-1.7 » 6.x-1.x-dev
Issue summary: View changes
Status: Active » Closed (outdated)

I am closing this feature request, as Drupal 6 is no longer supported. Please re-open it if you would like the feature implemented in the Drupal 7 or 8 version of the module.