function linkchecker_impersonate_user in Link checker 7
Same name and namespace in other branches
- 6.2 linkchecker.module \linkchecker_impersonate_user()
Impersonates another user, see https://drupal.org/node/287292#comment-3162350.
Each time this function is called, the active user is saved and $new_user becomes the active user. Multiple calls to this function can be nested, and session saving will be disabled until all impersonation attempts have been reverted using linkchecker_revert_user().
Parameters
int|object $new_user: User to impersonate, either a UID or a user object.
Return value
object Current user object.
See also
3 calls to linkchecker_impersonate_user()
- LinkCheckerImpersonatingUserTestCase::testLinkCheckerImpersonateUser in ./
linkchecker.test - linkchecker_revert_user in ./
linkchecker.module - Reverts to the previous user after impersonating.
- _linkchecker_status_handling in ./
linkchecker.module - Status code handling.
6 string references to 'linkchecker_impersonate_user'
- linkchecker_admin_settings_form in ./
linkchecker.admin.inc - Implements hook_admin_settings_form().
- linkchecker_admin_settings_form_validate in ./
linkchecker.admin.inc - Implements hook_admin_settings_form_validate().
- linkchecker_install in ./
linkchecker.install - Implements hook_install().
- linkchecker_uninstall in ./
linkchecker.install - Implements hook_uninstall().
- linkchecker_update_7009 in ./
linkchecker.install - Set user 1 as default user to impersonate content updates.
File
- ./
linkchecker.module, line 2595 - This module periodically check links in given node types, blocks etc.
Code
function linkchecker_impersonate_user($new_user = NULL) {
global $user;
$user_original =& drupal_static(__FUNCTION__);
if (!isset($new_user)) {
if (isset($user_original) && !empty($user_original)) {
// Restore the previous user from the stack.
$user = array_pop($user_original);
// Re-enable session saving if we are no longer impersonating a user.
if (empty($user_original)) {
drupal_save_session(TRUE);
}
}
}
else {
// Push the original user onto the stack and prevent session saving.
$user_original[] = $user;
drupal_save_session(FALSE);
if (is_numeric($new_user)) {
$user = user_load($new_user);
}
else {
$user = is_object($new_user) ? $new_user : (object) $new_user;
}
}
return $user;
}