Index: masquerade.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.info,v retrieving revision 1.1 diff -u -r1.1 masquerade.info --- masquerade.info 20 Nov 2006 09:13:33 -0000 1.1 +++ masquerade.info 16 Feb 2008 01:04:03 -0000 @@ -1,3 +1,4 @@ -; $Id: masquerade.info,v 1.1 2006/11/20 09:13:33 Gurpartap Exp $ +; $Id: masquerade.info,v 1.1.2.2 2007/06/18 23:06:50 dww Exp $ name = Masquerade -description = This module allows administrators to masquerade as other users. \ No newline at end of file +description = "This module allows permitted users to masquerade as other users." +core = 6.x \ No newline at end of file Index: masquerade.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.install,v retrieving revision 1.3 diff -u -r1.3 masquerade.install --- masquerade.install 20 Nov 2006 09:13:33 -0000 1.3 +++ masquerade.install 16 Feb 2008 01:04:03 -0000 @@ -1,41 +1,64 @@ array( + 'fields' => array( + 'sid' => array( + 'type' => 'varchar', + 'length' => '32', + 'not null' => true, + 'default' => ''), + 'uid_from' => array( + 'type' => 'int', + 'not null' => true, + 'default' => 0, + 'disp-width' => '10'), + 'uid_as' => array( + 'type' => 'int', + 'not null' => true, + 'default' => 0, + 'disp-width' => '10') + ), + 'indexes' => array( + 'sid' => array('sid', 'uid_from'), + 'sid_2' => array('sid', 'uid_as') + ) + ) + ); +} + +/** + * Implementation of hook_install(). */ function masquerade_install() { - switch ($GLOBALS['db_type']) { - case 'mysql': - case 'mysqli': - db_query("create table {masquerade} ( - sid varchar(32) NOT NULL default '', - uid_from int(10) NOT NULL default 0, - uid_as int(10) NOT NULL default 0, - key (sid, uid_from), - key (sid, uid_as) - ) /*!40100 DEFAULT CHARACTER SET utf8 */;"); - drupal_set_message(t('The required tables were created.')); - break; - case 'pgsql': - db_query("CREATE TABLE {masquerade} ( - sid varchar(32) NOT NULL default '', - uid_from numeric(10) NOT NULL default 0, - uid_as numeric(10) NOT NULL default 0 - );"); - db_query("CREATE INDEX idx_masquerade_sid_uid_from ON {masquerade} (sid, uid_from);"); - db_query("CREATE INDEX idx_masquerade_sid_uid_as ON {masquerade} (sid, uid_as);"); - drupal_set_message(t('The required tables were created.')); - break; - } + drupal_install_schema('masquerade'); } +/** + * Implementation of hook_uninstall(). + */ +function masquerade_uninstall() { + drupal_uninstall_schema('masquerade'); + variable_del('masquerade_test_user'); + variable_del('masquerade_admin_roles'); +} + +/** + * Implementation of hook_update_N(). + */ function masquerade_update_1() { return _system_update_utf8(array('masquerade')); } Index: masquerade.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/masquerade/masquerade.module,v retrieving revision 1.15 diff -u -r1.15 masquerade.module --- masquerade.module 20 Nov 2006 09:13:33 -0000 1.15 +++ masquerade.module 16 Feb 2008 01:04:03 -0000 @@ -1,5 +1,5 @@ The masquerade module adds a link on a user's profile page that allows administrators to masquerade as that user. Upon masquerading, the open to 'switch back' to the original user will appear in the menus. 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.
"); + return t('The masquerade module adds a link on a user\'s profile page that allows permitted users to masquerade as that user. Upon masquerading, the 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.'); } } /** - * Implementation of hook_menu() + * Implementation of hook_perm(). + * + * @return array */ -function masquerade_menu($may_cache) { - $items = array(); - $default_test_user = user_load(array('name' => variable_get('masquerade_test_user', ''))); - if ($may_cache) { - $items[] = array('path' => 'masquerade/switch', - 'title' => t('Switch user'), - 'callback' => 'masquerade_switch_user', - 'access' => !$GLOBALS['masquerading'] && (user_access('masquerade as user') || user_access('masquerade as admin')), - 'type' => MENU_CALLBACK); - - $items[] = array('path' => 'masquerade/quickswitch', - 'title' => t('Switch to test user'), - 'callback' => 'masquerade_switch_user', - 'callback arguments' => array($default_test_user->uid), - 'access' => !$GLOBALS['masquerading'] && (user_access('masquerade as user') || user_access('masquerade as admin')), - 'type' => MENU_NORMAL_ITEM); - - $items[] = array('path' => 'masquerade/unswitch', - 'title' => t('Switch back'), - 'callback' => 'masquerade_switch_back', - 'access' => $GLOBALS['masquerading'], - 'type' => MENU_NORMAL_ITEM); - - $items[] = array('path' => 'masquerade/autocomplete', - 'title' => t('Masquerade autocomplete'), - 'callback' => 'masquerade_autocomplete', - 'access' => $GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin')), - 'type' => MENU_CALLBACK); - - $items[] = array('path' => 'admin/settings/masquerade', - 'title' => t('Masquerade'), - 'description' => t('Masquerade module allows administrators to masquerade as other users.'), - 'callback' => 'drupal_get_form', - 'callback arguments' => 'masquerade_settings', - 'type' => MENU_NORMAL_ITEM,); - } - - return $items; +function masquerade_perm() { + return array('masquerade as user', 'masquerade as admin'); } /** - * Implementation of hook_init() + * Implementation of hook_init(). */ function masquerade_init() { global $user; @@ -72,23 +40,125 @@ if ($uid) { $GLOBALS['masquerading'] = $uid; } - else{ + else { $GLOBALS['masquerading'] = null; - } + } } /** - * Implementation of hook_user() + * Implementation of hook_menu(). + */ +function masquerade_menu() { + $items = array(); + + $items['masquerade/switch/%'] = array( + 'title' => 'Masquerading', + 'page callback' => 'masquerade_switch_user', + 'page arguments' => array(2), + 'access callback' => 'masquerade_access', + 'access arguments' => array('switch'), + 'type' => MENU_NORMAL_ITEM, + ); + $items['masquerade/unswitch'] = array( + 'title' => 'Switch back', + 'page callback' => 'masquerade_switch_back', + 'access callback' => 'masquerade_access', + 'access arguments' => array('unswitch'), + 'type' => MENU_NORMAL_ITEM, + ); + $items['masquerade/autocomplete'] = array( + 'title' => 'Masquerade autocomplete', + 'page callback' => 'masquerade_autocomplete', + 'access callback' => 'masquerade_access', + 'access arguments' => array('autocomplete'), + 'type' => MENU_CALLBACK, + ); + $items['admin/settings/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 permissions'), + 'type' => MENU_NORMAL_ITEM, + ); + + return $items; +} + +function masquerade_access($type) { + switch ($type) { + case 'unswitch': + return $GLOBALS['masquerading']; + case 'autocomplete': + return $GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin')); + break; + case 'switch': + return empty($GLOBALS['masquerading']) && (user_access('masquerade as user') || user_access('masquerade as admin')); + break; + } +} + +/** + * Implementation of hook_settings(). + */ +function masquerade_admin_settings() { + // create a list of roles; all selected roles are considered administrative. + $rids = array(); + $result = db_query("SELECT r.rid, r.name FROM {role} r ORDER BY r.name"); + while ($obj = db_fetch_object($result)) { + $rids[$obj->rid] = $obj->name; + } + + $form['masquerade_admin_roles'] = array( + '#type' => 'checkboxes', + '#title' => t('Roles that are considered "administrator" for masquerading'), + '#options' => $rids, + '#default_value' => variable_get('masquerade_admin_roles', array()), + ); + + $test_name = _masquerade_test_user(); + $form['masquerade_test_user'] = array( + '#type' => 'textfield', + '#title' => t('Default test user'), + '#autocomplete_path' => 'masquerade/autocomplete', + '#default_value' => check_plain($test_name->name), + '#description' => t('Enter the username of an account you wish to switch easily between. The name must be an exisiting user.'), + ); + $form['#validate'][] = 'masquerade_admin_settings_validate'; + + return system_settings_form($form); +} + +function masquerade_admin_settings_validate($form, &$form_state) { + unset($form); + $test_user = user_load(array('name' => $form_state['values']['masquerade_test_user'])); + if (!$test_user) { + form_set_error('masquerade_test_user', t('No such user exists. Please enter a valid username.')); + } +} + +function _masquerade_test_user() { + $test_user->uid = 0; + $test_user->name = ''; + + $test_user = user_load(array('name' => variable_get('masquerade_test_user', $test_user->name))); + + return $test_user; +} + +/** + * Implementation of hook_user(). */ function masquerade_user($op, &$edit, &$edit_user, $category = NULL) { switch ($op) { - + case 'logout': if ($edit_user->masquerading) { global $user; - cache_clear_all(); + cache_clear_all($user->uid, 'cache_menu', true); $real_user = user_load(array('uid' => $user->masquerading)); - watchdog('masquerade', "User '$real_user->name' no longer masquerading as '$user->name'"); + watchdog('masquerade', "User %user no longer masquerading as %masq_as.", array('%user' => $real_user->name, '%masq_as' => $user->name), WATCHDOG_INFO); db_query("DELETE FROM {masquerade} WHERE sid = '%s' AND uid_as = %d", session_id(), $edit_user->uid); } break; @@ -96,32 +166,23 @@ case 'view': // check if user qualifies as admin $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array()))); - $perm = $edit_user->uid == 1 || array_intersect(array_keys($edit_user->roles), $roles) ? + $perm = $edit_user->uid == 1 || array_intersect(array_keys($edit_user->roles), $roles) ? 'masquerade as admin' : 'masquerade as user'; global $user; - if (user_access($perm) && !$edit_user->masquerading && $user->uid != $edit_user->uid) { - $items[] = array( - 'title' => t("Masquerade"), - 'value' => l(t('Masquerade as !s', array('!s' => $edit_user->name)), "masquerade/switch/$edit_user->uid", array('destination' => $_GET['q'])), - 'class' => 'masquerade', + if (user_access($perm) && empty($edit_user->masquerading) && $user->uid != $edit_user->uid) { + $edit_user->content['Masquerade'] = array( + '#value' => l(t('Masquerade as !user', array('!user' => $edit_user->name)), 'masquerade/switch/'. $edit_user->uid, array('destination' => $_GET['q'])), + '#weight' => 10 ); - return array(t('History') => $items); } break; } } /** - * Implementation of hook_perm() - */ -function masquerade_perm() { - return array('masquerade as user', 'masquerade as admin'); -} - -/** - * Implementation of hook_block() + * Implementation of hook_block(). */ function masquerade_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { @@ -129,88 +190,70 @@ $blocks[0]['info'] = t('Masquerade'); return $blocks; case 'view': - if ($GLOBALS['masquerading'] || (user_access('masquerade as user') || user_access('masquerade as admin'))) { - switch($delta) { - case 0: - $block['subject'] = t('Masquerade'); - $block['content'] = _masquerade_block_1(); - break; + if (masquerade_access('autocomplete')) { + switch ($delta) { + case 0: + $block['subject'] = t('Masquerade'); + $block['content'] = drupal_get_form('_masquerade_block_1'); + break; + } + return $block; } - return $block; - } + break; } } /** - * masquerade block form + * Masquerade block form. */ -function _masquerade_block_1() { - - $output = drupal_get_form('_masquerade_block_1_form', $form); - - if ($masq_as != '%anonymous') { - $links[] = $GLOBALS['masquerading'] ? l('switch back', 'masquerade/unswitch', array()) : l('quick switch', 'masquerade/quickswitch', array()); - } - else if ($masq_as == '%anonymous' && $GLOBALS['masquerading']) { - $links[] = l('login back', 'user/login', array()); - } - if ($links) { - $output .= theme('item_list', $links); - } - return $output; -} - -function _masquerade_block_1_form($record) { - $attributes = $GLOBALS['masquerading'] ? array('disabled' => 'disabled') : array(); - +function _masquerade_block_1($record) { if ($GLOBALS['masquerading']) { global $user; - $masq_as = $user->name ? '%masq_user' : '%anonymous'; - $markup_value = t('You are masquerading as: