The masquerade module adds a link on a user\'s profile page that allows permitted users to masquerade as that user. Upon masquerading, a link to "switch back" to the original user will appear in the menu. While masquerading, the option to masquerade as another user will not appear. All masquerading transactions are logged, and $user->masquerading will be set; this could be displayed via theme.
In the masquerade settings a list of roles are presented; any checked role is considered an "administrator" and requires the second level "masquerade as admin" permission to masquerade as. User #1 is automatically considered an administrator, regardless of roles.
'); case 'admin/settings/masquerade': return t('Only the users with masquerade as admin permission, will be able to masquerade as the users who belong to the roles selected below. User #1 is automatically considered an administrator, regardless of roles.'); } } /** * Implements hook_permission(). * * @return array */ function masquerade_permission() { return array( 'masquerade as user' => array( 'title' => t('Masquerade as user'), 'description' => t('Masquerade as another user.'), ), 'masquerade as admin' => array( 'title' => t('Masquerade as admin'), 'description' => t('Masquerade as the site admin (UID 1).'), ), 'administer masquerade' => array( 'title' => t('Administer Masquerade'), 'description' => t('Perform administration tasks and configure the Masquerade module.'), ), ); } /** * Implements hook_init(). */ function masquerade_init() { global $user; // Try to load masqing uid from masquerade table. $uid = db_query("SELECT uid_from FROM {masquerade} WHERE sid = :sid AND uid_as = :uid_as", array( ':sid' => session_id(), ':uid_as' => $user->uid, ))->fetchField(); // We are using identical operator (===) instead of equal (==) because if // $uid === 0 we want to store the session variable. If there's no record in // masquerade table we clear the session variable. if ($uid === FALSE) { if (isset($_SESSION)) { unset($_SESSION['masquerading']); } } else { $_SESSION['masquerading'] = $uid; } } /** * Implements hook_cron(). * * Cleanup masquerade records where people didn't use the switch back link * that would have cleanly removed the user switch record. */ function masquerade_cron() { // see http://drupal.org/node/268487 before modifying this query $subquery = db_select('sessions', 's'); $subquery->addField('s', 'sid'); $query = db_delete('masquerade'); $query->condition('sid', $subquery, 'NOT IN'); $query->execute(); } /** * Implements hook_menu(). */ function masquerade_menu() { $items = array(); $default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', '')); if ($default_test_user && ($default_test_user->uid || $default_test_user->name == variable_get('anonymous', t('Anonymous')))) { $items['masquerade/switch/' . $default_test_user->uid] = array( 'title' => 'Masquerade as @testuser', 'title arguments' => array('@testuser' => $default_test_user->name), 'page callback' => 'masquerade_switch_user_page', 'page arguments' => array(2), 'access callback' => 'masquerade_menu_access', 'access arguments' => array('switch'), 'type' => MENU_NORMAL_ITEM, ); } $items['masquerade/switch/%'] = array( 'title' => 'Masquerading', 'page callback' => 'masquerade_switch_user_page', 'page arguments' => array(2), 'access callback' => 'masquerade_menu_access', 'access arguments' => array('switch', 2), 'type' => MENU_NORMAL_ITEM, ); $items['masquerade/unswitch'] = array( 'title' => 'Switch back', 'page callback' => 'masquerade_switch_back_page', 'access callback' => 'masquerade_menu_access', 'access arguments' => array('unswitch'), 'type' => MENU_NORMAL_ITEM, ); $items['masquerade/autocomplete'] = array( 'title' => '', 'page callback' => 'masquerade_autocomplete', 'access callback' => 'masquerade_menu_access', 'access arguments' => array('autocomplete'), 'type' => MENU_CALLBACK, ); $items['masquerade/autocomplete-users'] = array( 'title' => '', 'page callback' => 'masquerade_autocomplete_multiple', 'access callback' => 'masquerade_menu_access', 'access arguments' => array('autocomplete'), 'type' => MENU_CALLBACK, ); $items['masquerade/autocomplete-user'] = array( 'title' => 'Masquerade autocomplete', 'page callback' => 'masquerade_autocomplete_multiple', 'page arguments' => array(2, FALSE), 'access arguments' => array('access user profiles'), 'type' => MENU_CALLBACK, ); $items['admin/config/people/masquerade'] = array( 'title' => 'Masquerade', 'description' => 'Masquerade module allows administrators to masquerade as other users.', 'page callback' => 'drupal_get_form', 'page arguments' => array('masquerade_admin_settings'), 'access callback' => 'user_access', 'access arguments' => array('administer masquerade'), 'type' => MENU_NORMAL_ITEM, ); return $items; } /** * Implements hook_menu_alter(). * * We need to add a token to the Masquerade paths to protect against CSRF * attacks. Since menu items in Drupal do not support dynamic elements these * tokens need to be added during rendering via masquerade_translated_menu_link_alter(). * Set the 'alter'-option to TRUE to make sure * the links get passed through masquerade_translated_menu_link_alter. */ function masquerade_menu_alter(&$items) { $default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', '')); if (isset($default_test_user->uid)) { $items['masquerade/switch/' . $default_test_user->uid]['options']['alter'] = TRUE; } $items['masquerade/switch/%']['options']['alter'] = TRUE; $items['masquerade/unswitch']['options']['alter'] = TRUE; } /** * Implements hook_translated_menu_link_alter(). * * Dynamically add the CSRF protection token to the Masquerade menu items. */ function masquerade_translated_menu_link_alter(&$item, $map) { if (isset($item['page_callback'])) { if ($item['page_callback'] == 'masquerade_switch_user_page' && isset($map[2])) { $item['localized_options']['query']['token'] = drupal_get_token('masquerade/switch/' . $map[2]); } elseif ($item['page_callback'] == 'masquerade_switch_back_page') { $item['localized_options']['query']['token'] = drupal_get_token('masquerade/unswitch'); } } } /** * Implements hook_user_operations(). */ function masquerade_user_operations() { return array( 'masquerade' => array( 'label' => t('Masquerade as user'), 'callback' => 'masquerade_user_operations_masquerade', ), ); } /** * Callback for user operation. */ function masquerade_user_operations_masquerade(array $accounts) { // Only process the first account since switching to multiple makes no sense. if (($uid = current($accounts)) && masquerade_menu_access('switch', $uid)) { masquerade_switch_user($uid); } } /** * Determine if the current user has permission to switch users. * * @param string $type * Either 'switch', 'unswitch', 'user', or 'autocomplete'. * * @param object $uid * An optional parameter indicating a specific uid to switch to. * Otherwise, return if the user can switch to any user account. * * @return * TRUE, if the user can perform the requested action, FALSE otherwise. */ function masquerade_menu_access($type, $uid = NULL) { switch ($type) { case 'unswitch': return isset($_SESSION['masquerading']) || arg(2) == 'menu-customize' || arg(2) == 'menu'; case 'autocomplete': return isset($_SESSION['masquerading']) || (user_access('masquerade as user') || user_access('masquerade as admin')); break; case 'user': global $user; return db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $user->uid))->fetchField(); break; case 'switch': $switch_to_account = FALSE; global $user; if ($uid) { if (!is_numeric($uid)) { return FALSE; } $account = user_load($uid); $switch_to_account = db_query("SELECT TRUE FROM {masquerade_users} WHERE uid_from = :uid_from AND uid_to = :uid_to", array( ':uid_from' => $user->uid, ':uid_to' => $account->uid ))->fetchField(); } return !isset($_SESSION['masquerading']) && (user_access('masquerade as user') || user_access('masquerade as admin') || $switch_to_account); break; } } /** * Admin settings form. */ function masquerade_admin_settings() { // create a list of roles; all selected roles are considered administrative. $roles = user_roles(); $form['masquerade_admin_roles'] = array( '#type' => 'checkboxes', '#title' => t('Roles that are considered "administrators" for masquerading'), '#options' => $roles, '#default_value' => variable_get('masquerade_admin_roles', array()), ); $test_name = _masquerade_user_load(variable_get('masquerade_test_user', '')); $form['masquerade_test_user'] = array( '#type' => 'textfield', '#title' => t('Menu Quick Switch user'), '#autocomplete_path' => 'masquerade/autocomplete', '#default_value' => isset($test_name->name) ? check_plain($test_name->name) : '', '#description' => t('Enter the username of an account you wish to switch easily between via a menu item.'), '#maxlength' => NULL, ); $quick_switch = user_load_multiple(variable_get('masquerade_quick_switches', array())); $quick_switch_users = array(); foreach ($quick_switch as $uid => $account) { if ($uid == 0) { $account->name = variable_get('anonymous', t('Anonymous')); } $quick_switch_users[] = $account->name; } $form['masquerade_quick_switches'] = array( '#type' => 'textfield', '#title' => t('Masquerade Block Quick Switch users'), '#autocomplete_path' => 'masquerade/autocomplete-users', '#default_value' => drupal_implode_tags($quick_switch_users), '#description' => t('Enter the usernames, separated by commas, of accounts to show as quick switch links in the Masquerade block.'), '#maxlength' => NULL, ); $form = system_settings_form($form); $form['#validate'][] = 'masquerade_admin_settings_validate'; $form['#submit'][] = 'masquerade_admin_settings_submit'; return $form; } function masquerade_admin_settings_validate($form, &$form_state) { if (!empty($form_state['values']['masquerade_test_user'])) { $test_user = _masquerade_user_load($form_state['values']['masquerade_test_user']); if (!$test_user) { form_set_error('masquerade_test_user', t('%user does not exist. Please enter a valid username.', array('%user' => $form_state['values']['masquerade_test_user']))); } } // Needs to rebuild menu in masquerade_admin_settings_submit(). $form_state['masquerade_rebuild_menu'] = (variable_get('masquerade_test_user', '') != $form_state['values']['masquerade_test_user']); // A comma-separated list of users. $masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']); // Change user names to user ID's for system_settings_form_submit() to save. $masquerade_uids = array(); foreach ($masquerade_switches as $switch_user) { $test_user = _masquerade_user_load($switch_user); if (!$test_user) { form_set_error('masquerade_quick_switches', t('%user does not exist. Please enter a valid username.', array('%user' => $switch_user))); } else { $masquerade_uids[] = $test_user->uid; } } $form_state['values']['masquerade_quick_switches'] = $masquerade_uids; } function masquerade_admin_settings_submit($form, &$form_state) { // Rebuild the menu system so the menu "Quick Switch" user is updated. if ($form_state['masquerade_rebuild_menu']) { menu_rebuild(); } } /** * Wrapper around user_load() to allow the loading of anonymous users. * * @param $username * The username of the user you wish to load (i.e. $user->name). To load the * anonymous user, pass the value of the 'anonymous' variable. * * @return * A fully-loaded $user object upon successful user load or FALSE if user * cannot be loaded. */ function _masquerade_user_load($username) { $account = FALSE; if (!empty($username)) { $anon = variable_get('anonymous', t('Anonymous')); $account = user_load_by_name(($username == $anon ? '' : $username)); if (isset($account->uid) && empty($account->uid)) { // Anonymous user should have a name. $account->name = $anon; } } return $account; } /** * Implements hook_user_logout(). */ function masquerade_user_logout($account) { if (!empty($account->masquerading)) { global $user; cache_clear_all($user->uid, 'cache_menu', TRUE); $real_user = user_load($user->masquerading); watchdog('masquerade', "User %user no longer masquerading as %masq_as.", array('%user' => $real_user->name, '%masq_as' => $user->name), WATCHDOG_INFO); $query = db_delete('masquerade'); $query->condition('sid', session_id()); $query->condition('uid_as', $account->uid); $query->execute(); } } /** * Implements hook_user_view(). */ function masquerade_user_view($account, $view_mode, $langcode) { // check if user qualifies as admin $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array()))); $perm = $account->uid == 1 || array_intersect(array_keys((array)$account->roles), $roles) ? 'masquerade as admin' : 'masquerade as user'; global $user; if (user_access($perm) && empty($account->masquerading) && $user->uid != $account->uid) { $account->content['masquerade'] = array( '#markup' => l(t('Masquerade as !user', array('!user' => $account->name)), 'masquerade/switch/' . $account->uid, array('query' => array( 'token' => drupal_get_token('masquerade/switch/' . $account->uid)), 'destination' => $_GET['q'], 'attributes' => array('class' => 'masquerade-switch'), )), '#weight' => 10, ); } } /** * Implements hook_form_FORM_ID_alter(). */ function masquerade_form_user_profile_form_alter(&$form, &$form_state, $form_id) { if ($form['#user_category'] != 'account') { // Do not show this form for different categories. return; } $form['masquerade'] = array( '#type' => 'fieldset', '#title' => t('Masquerade settings'), '#access' => user_access('administer masquerade'), ); $edit_user = $form['#user']; $uids = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $edit_user->uid)) ->fetchCol(); $users = user_load_multiple($uids); $masquerade_users = array(); foreach ($users as $uid => $account) { if ($uid == 0) { $masquerade_users[] = variable_get('anonymous', t('Anonymous')); } else { $masquerade_users[] = $account->name; } } $form['masquerade']['masquerade_users'] = array( '#type' => 'textfield', '#title' => t('Enter the users this user is able to masquerade as'), '#description' => t('Enter a comma separated list of user names that this user can masquerade as.'), '#autocomplete_path' => 'masquerade/autocomplete-user', '#default_value' => drupal_implode_tags($masquerade_users), '#maxlength' => NULL, ); $form['#validate'][] = 'masquerade_user_validate'; $form['#submit'][] = 'masquerade_user_submit'; } /** * Validates user account form. */ function masquerade_user_validate(&$form, $form_state) { if (isset($form_state['values']['masquerade_users'])) { $users = drupal_explode_tags($form_state['values']['masquerade_users']); foreach ($users as $username) { if (!_masquerade_user_load($username)) { form_set_error('masquerade_users', t('%user is not a valid user name.', array('%user' => $username))); } } } } /** * Submit handler for masquerade users form element. */ function masquerade_user_submit(&$form, $form_state) { global $_masquerade_old_session_id; $_masquerade_old_session_id = session_id(); } /** * Implements hook_user_update(). */ function masquerade_user_update(&$edit, $account, $category) { global $_masquerade_old_session_id; if ($category == 'account' && isset($edit['masquerade_users'])) { $query = db_delete('masquerade_users'); $query->condition('uid_from', $account->uid); $query->execute(); // Save users from settings form. $users = drupal_explode_tags($edit['masquerade_users']); $query = db_insert('masquerade_users')->fields(array('uid_from', 'uid_to')); foreach ($users as $username) { if ($to_user = _masquerade_user_load($username)) { $query->values(array( 'uid_from' => $account->uid, 'uid_to' => $to_user->uid, )); } } $query->execute(); $edit['masquerade_users'] = NULL; // Update user session... // @TODO check other way of session API. if (!empty($_masquerade_old_session_id)) { $query = db_update('masquerade'); $query->fields(array( 'sid' => session_id(), )); $query->condition('sid', $_masquerade_old_session_id); $query->execute(); } } } /** * Implements hook_user_delete(). */ function masquerade_user_delete($account) { // Cleanup tables. $query = db_delete('masquerade_users'); $conditions = db_or(); $conditions->condition('uid_from', $account->uid); $conditions->condition('uid_to', $account->uid); $query->condition($conditions); $query->execute(); // Cleanup variables. $switches = variable_get('masquerade_quick_switches', array()); $switches_new = array_diff($switches, array($account->uid)); if ($switches != $switches_new) { variable_set('masquerade_quick_switches', $switches_new); // @TODO Implement block cache cleaning. menu_rebuild(); } } /** * Implements hook_block_info(). */ function masquerade_block_info() { $blocks = array(); $blocks['masquerade'] = array( 'info' => t('Masquerade'), 'cache' => DRUPAL_NO_CACHE, ); return $blocks; } /** * Implements hook_block_view(). */ function masquerade_block_view($delta = '') { $block = array(); switch ($delta) { case 'masquerade': if (isset($_SESSION['masquerading']) || (user_access('masquerade as user') || user_access('masquerade as admin'))) { $block['subject'] = t('Masquerade'); $block['content'] = drupal_get_form('masquerade_block_1'); } break; } return $block; } /** * Masquerade block form. */ function masquerade_block_1() { global $user; $quick_switch_links = array(); $markup_value = ''; if (isset($_SESSION['masquerading'])) { $quick_switch_links[] = l(t('Switch back'), 'masquerade/unswitch', array('query' => array('token' => drupal_get_token('masquerade/unswitch')))); if ($user->uid > 0) { $markup_value = t('You are masquerading as %masq_as.', array('@user-url' => url('user/' . $user->uid), '%masq_as' => $user->name)); } else { $markup_value = t('You are masquerading as %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))); } } else { $quick_switches = variable_get('masquerade_quick_switches', array()); // Add in user-specific switches, and prevent duplicates. $user_switches = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $user->uid))->fetchCol(); $masquerade_switches = array_unique(array_merge($quick_switches, $user_switches)); foreach ($masquerade_switches as $switch_user) { if (!isset($_SESSION['user']->uid) || $switch_user != $_SESSION['user']->uid) { $account = user_load($switch_user); if (isset($account->uid)) { $switch_link = 'masquerade/switch/' . $account->uid; if ($account->uid) { $quick_switch_links[] = l($account->name, $switch_link, array('query' => array('token' => drupal_get_token($switch_link)))); } if ($switch_user == 0) { $account->name = variable_get('anonymous', t('Anonymous')); $quick_switch_links[] = l($account->name, $switch_link, array('query' => array('token' => drupal_get_token($switch_link)))); } } } } if (masquerade_menu_access('autocomplete')) { $markup_value .= t('Enter the username to masquerade as.'); $form['masquerade_user_field'] = array( '#prefix' => '