1

I need to detect if I'm currently browsing the site as a different user, having used the devel user switch module.

The LDAP module I have triggers some profile updates on every login, based on some server variables set from AD/SSO. But by using devel switch, it's writing MY user details, to the profile of the person I've switched to.

I need a way of detecting it, so I can prevent the profile write.

I've had a look through the devel API here but can't see that anything is set when running as another user.

Running D7.

2

The Devel switch feature passes the login straight to the user module in devel_switch_user(); it doesn't give other modules the chance to intervene, and doesn't provide any context that would let you know how the login has been instigated.

I guess as a simple low-tech solution you could patch that function and set a session var before it invokes the login, something like

...
if (!$old_uid) {
  // Switch from anonymous to authorized.
  drupal_session_regenerate();
}

$_SESSION['login_from_devel_switch'] = TRUE;

$edit = array();
user_module_invoke('login', $edit, $user);
...

Then you can check for the existence of that session var wherever you need to prevent the profile from writing.

  • Seems like it would work. I'll need to check to see if hitting "logout" clears the session variable, if not I'll need to add that in too. – BeardedGeek Oct 31 '13 at 15:57
  • It does - user_logout() calls session_destroy(). Wouldn't be much of a logout if it left session vars ;) – Clive Oct 31 '13 at 16:02
  • Ha... good point! Nice simple solution then Clive, thanks for the help. – BeardedGeek Oct 31 '13 at 16:19
  • 1
    I'll submit a feature request once drupal.org is back up to get this added to the devel module. I'm sure other's will find it handy! – BeardedGeek Oct 31 '13 at 16:20
  • That's a great idea. Maybe the introduction of a hook would be the best way to go? Then there's scope to do more than just set a session var - for example you could track which users are switching to who and when, and loads of other things – Clive Oct 31 '13 at 16:22

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.