diff --git a/masquerade.api.php b/masquerade.api.php new file mode 100644 index 0000000..b70452c --- /dev/null +++ b/masquerade.api.php @@ -0,0 +1,65 @@ + array( + 'group' => $group, + ), + 'masquerade_to_end' => array( + 'group' => $group, + ), + 'masquerade_began' => array( + 'group' => $group, + ), + 'masquerade_ended' => array( + 'group' => $group, + ), + ); + return $hooks; +} + +/** * Implements hook_permission(). * * @return array @@ -360,12 +386,17 @@ function masquerade_user_logout($account) { global $user; cache_clear_all($user->uid, 'cache_menu', TRUE); $real_user = user_load($user->masquerading); + // Let other modules identify when the masquerade is about to end. + module_invoke_all('masquerade_to_end', $user, $real_user); 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(); + + // Let other modules identify when the masquerade finished. + module_invoke_all('masquerade_ended', $user, $real_user); } } @@ -786,6 +817,9 @@ function masquerade_switch_user($uid) { return FALSE; } + // Allow other modules to identify when the masquerade is about to start. + module_invoke_all('masquerade_to_begin', $user, $new_user); + $query = db_insert('masquerade'); $query->fields(array( 'uid_from' => $user->uid, @@ -798,7 +832,13 @@ function masquerade_switch_user($uid) { watchdog('masquerade', 'User %user now masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $new_user->name ? $new_user->name : variable_get('anonymous', t('Anonymous'))), WATCHDOG_INFO); drupal_set_message(t('You are now masquerading as !masq_as.', array('!masq_as' => theme('username', array('account' => $new_user))))); $user->masquerading = $new_user->uid; + + // Allow other modules to identify when the masquerade started. + module_invoke_all('masquerade_began', $user, $new_user); + + // Finalize the switch. $user = $new_user; + return TRUE; } @@ -837,6 +877,13 @@ function masquerade_switch_back() { $query->condition($conditions); $query->execute(); $oldname = ($user->uid == 0 ? variable_get('anonymous', t('Anonymous')) : $user->name); - $user = user_load($uid); + $current_user = $user; + $resume_user = user_load($uid); + // Allow other modules to identify when the masquerade is about to end. + module_invoke_all('masquerade_to_end', $current_user, $resume_user); + // Replace the global $user object. + $user = $resume_user; + // Allow other modules to identify when the masquerade ended. + module_invoke_all('masquerade_ended', $current_user, $user); watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO); }