From 558568e3c05b6bd65485cfa0863ec28ce58dbd90 Mon Sep 17 00:00:00 2001 From: sun Date: Sun, 24 Feb 2013 20:19:07 +0100 Subject: [PATCH] - #1836516 by sun: Added masquerade_user_is_masquerading() helper function. --- masquerade.module | 54 +++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/masquerade.module b/masquerade.module index c0c43f6..28cb064 100644 --- a/masquerade.module +++ b/masquerade.module @@ -91,8 +91,7 @@ function masquerade_menu() { $items['unmasquerade'] = array( 'title' => 'Unmasquerade', 'page callback' => 'masquerade_switch_back_page', - 'access callback' => 'masquerade_menu_access', - 'access arguments' => array('unmasquerade'), + 'access callback' => 'masquerade_user_is_masquerading', 'menu_name' => 'account', 'weight' => 9, // Invoke masquerade_translated_menu_link_alter() to append token. @@ -172,19 +171,10 @@ function masquerade_masquerade_access($user, $target_user) { } /** - * Determine if the current user has permission to switch users. - * - * @param string $type - * Either 'unmasquerade'. - * - * @return - * TRUE, if the user can perform the requested action, FALSE otherwise. + * Returns whether the current user is masquerading. */ -function masquerade_menu_access($type) { - switch ($type) { - case 'unmasquerade': - return isset($_SESSION['masquerading']); - } +function masquerade_user_is_masquerading() { + return isset($_SESSION['masquerading']); } /** @@ -315,7 +305,7 @@ function masquerade_switch_user_page(User $target_account) { function masquerade_switch_user_validate(User $target_account) { global $user; - if (isset($_SESSION['masquerading'])) { + if (masquerade_user_is_masquerading()) { return t('You are masquerading already. Please switch back to your account to masquerade as another user.', array( '@unmasquerade-url' => url('unmasquerade', array( 'query' => array('token' => drupal_get_token('unmasquerade')), @@ -356,6 +346,8 @@ function masquerade_switch_user(User $target_account) { module_invoke_all('user_logout', $user); drupal_session_regenerate(); + $_SESSION['masquerading'] = $user->uid; + $query = db_insert('masquerade'); $query->fields(array( 'uid_from' => $user->uid, @@ -364,10 +356,16 @@ function masquerade_switch_user(User $target_account) { )); $query->execute(); - watchdog('masquerade', 'User %user now masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $target_account->name), WATCHDOG_INFO); - drupal_set_message(t('You are now masquerading as !masq_as.', array('!masq_as' => theme('username', array('account' => $target_account))))); - $user->masquerading = $target_account->uid; + watchdog('masquerade', 'User %username masqueraded as %target_username.', array( + '%username' => $user->name, + '%target_username' => $target_account->name, + ), WATCHDOG_INFO); + drupal_set_message(t('You are now masquerading as !user.', array( + '!user' => theme('username', array('account' => $target_account)), + ))); + $user = $target_account; + $user->masquerading = $target_account->uid; // Call all login hooks when switching to masquerading user. module_invoke_all('user_login', $user); @@ -382,9 +380,12 @@ function masquerade_switch_back_page() { $token = drupal_container()->get('request')->query->get('token'); if (isset($token) && drupal_valid_token($token, 'unmasquerade')) { global $user; - $olduser = $user; + $old_user = $user; masquerade_switch_back(); - drupal_set_message(t('You are no longer masquerading as !masq_as and are now logged in as !user.', array('!user' => theme('username', array('account' => $user)), '!masq_as' => theme('username', array('account' => $olduser))))); + drupal_set_message(t('You are no longer masquerading as !old_user and are now logged in as !user.', array( + '!user' => theme('username', array('account' => $user)), + '!old_user' => theme('username', array('account' => $old_user)), + ))); drupal_goto(drupal_container()->get('request')->server->get('HTTP_REFERER')); } else { @@ -407,21 +408,24 @@ function masquerade_switch_back() { ->condition('sid', session_id()) ->execute(); - $oldname = $user->name; + $old_user = $user; // Call logout hooks when switching from masquerading user. module_invoke_all('user_logout', $user); drupal_session_regenerate(); + // Remove the masquerading flag from the user's session. + unset($_SESSION['masquerading']); + $user = user_load($uid); // Call all login hooks when switching back to original user. module_invoke_all('user_login', $user); - // Remove the masquerading flag from the user's session. - unset($_SESSION['masquerading']); - - watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO); + watchdog('masquerade', 'User %username stopped masquerading as %old_username.', array( + '%username' => $user->name, + '%old_username' => $old_user->name, + ), WATCHDOG_INFO); } /** -- 1.7.11.msysgit.1