function simplenews_impersonate_user in Simplenews 7.2
Same name and namespace in other branches
- 7 simplenews.module \simplenews_impersonate_user()
Impersonates another user.
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 user_revert_user().
@todo: This function is a backport of http://drupal.org/node/287292. Switch to that once available.
Parameters
$new_user: User to impersonate, either a UID or a user object.
Return value
Current user object.
See also
3 calls to simplenews_impersonate_user()
- SimplenewsSourceEntity::setContext in includes/
simplenews.source.inc - Set up the necessary language and user context.
- simplenews_mail_spool in includes/
simplenews.mail.inc - Send simplenews newsletters from the spool.
- simplenews_revert_user in ./
simplenews.module - Reverts to the previous user after impersonating.
File
- ./
simplenews.module, line 2922 - Simplenews node handling, sent email, newsletter block and general hooks
Code
function simplenews_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;
}