| 1 |
<?php
|
| 2 |
|
| 3 |
define('IMPERSONATE_USER', 'impersonate user');
|
| 4 |
define('ADMINISTER_IMPERSONATE_MODULE', 'administer impersonate module');
|
| 5 |
define('IMPERSONATE_VAR_PREFIX','impersonate_original_');
|
| 6 |
|
| 7 |
|
| 8 |
function get_uid_for_username($username=NULL) {
|
| 9 |
$result = FALSE;
|
| 10 |
if (is_string($username)) {
|
| 11 |
$res = db_query("SELECT COUNT(u.uid) AS cnt FROM {users} u WHERE u.name='%s'",$username);
|
| 12 |
$result = (db_result($res) > 0);
|
| 13 |
if ($result) {
|
| 14 |
$res = db_query("SELECT u.uid AS uid FROM {users} u WHERE u.name='%s'",$username);
|
| 15 |
$result = db_result($res);
|
| 16 |
}
|
| 17 |
}
|
| 18 |
return $result;
|
| 19 |
}
|
| 20 |
|
| 21 |
function get_impersonate_variable_name() {
|
| 22 |
return IMPERSONATE_VAR_PREFIX.session_id();
|
| 23 |
}
|
| 24 |
|
| 25 |
function get_impersonate_variable_name_for_user($uid) {
|
| 26 |
return IMPERSONATE_VAR_PREFIX.$uid;
|
| 27 |
}
|
| 28 |
|
| 29 |
/**
|
| 30 |
*Implementation of hook_perm
|
| 31 |
*/
|
| 32 |
function impersonate_perm() {
|
| 33 |
return array(IMPERSONATE_USER, ADMINISTER_IMPERSONATE_MODULE);
|
| 34 |
}
|
| 35 |
|
| 36 |
function user_impersonate_active() {
|
| 37 |
$original_user = variable_get(get_impersonate_variable_name(),FALSE);
|
| 38 |
return ($original_user !== FALSE);
|
| 39 |
}
|
| 40 |
|
| 41 |
/**
|
| 42 |
* Does user have any assigned roles for impersonation
|
| 43 |
*/
|
| 44 |
function user_has_assigned_impersonate_roles($uid) {
|
| 45 |
$settings = variable_get(get_impersonate_variable_name_for_user($uid),FALSE);
|
| 46 |
$rids = '';
|
| 47 |
if (is_array($settings)) {
|
| 48 |
foreach ($settings as $val) {
|
| 49 |
if ($val > 0) {
|
| 50 |
if ($rids != '') {
|
| 51 |
$rids .=',';
|
| 52 |
}
|
| 53 |
$rids .= $val;
|
| 54 |
}
|
| 55 |
}
|
| 56 |
}
|
| 57 |
if ($rids == '') {
|
| 58 |
$rids = '0';
|
| 59 |
}
|
| 60 |
|
| 61 |
$query = "SELECT count(u.name) as cnt FROM {users} u, {users_roles} ur, {role} r
|
| 62 |
WHERE ur.uid=u.uid
|
| 63 |
AND ur.rid=r.rid
|
| 64 |
AND r.rid IN (".$rids.")";
|
| 65 |
|
| 66 |
return (db_result(db_query($query)) > 0);
|
| 67 |
}
|
| 68 |
|
| 69 |
/**
|
| 70 |
* Retrieve a pipe delimited string of autocomplete suggestions for existing users
|
| 71 |
* For now we can assume that $roles look like this: 'Worthologist', 'Registered Member'
|
| 72 |
*
|
| 73 |
*/
|
| 74 |
function impersonate_user_autocomplete($string = '') {
|
| 75 |
global $user;
|
| 76 |
$matches = array();
|
| 77 |
if ($string) {
|
| 78 |
//Load settings for this user (if there are any)
|
| 79 |
$settings = variable_get(get_impersonate_variable_name_for_user($user->uid),FALSE);
|
| 80 |
$rids = '0';
|
| 81 |
if (is_array($settings)) {
|
| 82 |
$rids = '';
|
| 83 |
foreach ($settings as $val) {
|
| 84 |
if ($val > 0) {
|
| 85 |
if ($rids != '') {
|
| 86 |
$rids .=',';
|
| 87 |
}
|
| 88 |
$rids .= $val;
|
| 89 |
}
|
| 90 |
}
|
| 91 |
if ($rids == '') {
|
| 92 |
$rids = '0';
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
$result = db_query_range("SELECT u.name as name FROM {users} u, {users_roles} ur, {role} r
|
| 97 |
WHERE ur.uid=u.uid
|
| 98 |
AND ur.rid=r.rid
|
| 99 |
AND u.name LIKE '%s%%'
|
| 100 |
AND r.rid IN (%s)", $string, $rids, 0, 10);
|
| 101 |
while ($found_user = db_fetch_object($result)) {
|
| 102 |
$matches[$found_user->name] = check_plain($found_user->name);
|
| 103 |
}
|
| 104 |
}
|
| 105 |
print drupal_to_js($matches);
|
| 106 |
exit();
|
| 107 |
}
|
| 108 |
|
| 109 |
function get_impersonate_access($access_right) {
|
| 110 |
global $user;
|
| 111 |
$result = user_access($access_right,$user);
|
| 112 |
$stored_variable = get_impersonate_variable_name();
|
| 113 |
$original_uid = variable_get($stored_variable,FALSE);
|
| 114 |
if ($original_uid !== FALSE) {
|
| 115 |
$account = user_load(array('uid' => $uid));
|
| 116 |
}
|
| 117 |
return user_access($access_right,$user) || user_access($access_right,$account);
|
| 118 |
}
|
| 119 |
|
| 120 |
/**
|
| 121 |
* Implementation of hook_menu
|
| 122 |
*
|
| 123 |
* @param unknown_type $may_cache
|
| 124 |
*/
|
| 125 |
function impersonate_menu($may_cache) {
|
| 126 |
global $user;
|
| 127 |
$items = array();
|
| 128 |
$imp_access = user_impersonate_active()
|
| 129 |
|| ( get_impersonate_access(IMPERSONATE_USER)
|
| 130 |
&& user_has_assigned_impersonate_roles($user->uid));
|
| 131 |
$adm_access = get_impersonate_access(ADMINISTER_IMPERSONATE_MODULE);
|
| 132 |
$any_access = $imp_access || $adm_access;
|
| 133 |
|
| 134 |
if ($may_cache) {
|
| 135 |
$items[] = array('path' => 'user/impersonate_autocomplete', 'title' => t('Impersonate User autocomplete'),
|
| 136 |
'callback' => 'impersonate_user_autocomplete', 'access' => $any_access, 'type' => MENU_CALLBACK);
|
| 137 |
|
| 138 |
} else {
|
| 139 |
if (arg(0) == 'user' && is_numeric(arg(1))) {
|
| 140 |
global $user;
|
| 141 |
$account = user_load(array('uid' => arg(1)));
|
| 142 |
if ( ($user->uid != $account->uid) || $adm_access ) {
|
| 143 |
$items[] = array('path' => 'user/'. arg(1) .'/impersonate_admin',
|
| 144 |
'title' => t('Impersonate Administration'),
|
| 145 |
'callback' => 'impersonate_admin_user_page',
|
| 146 |
'type' => MENU_LOCAL_TASK,
|
| 147 |
'access' => $user->uid,
|
| 148 |
'weight' => 2,
|
| 149 |
);
|
| 150 |
}
|
| 151 |
}
|
| 152 |
if ( ( $user->uid == arg(1) ) && $imp_access ) {
|
| 153 |
|
| 154 |
$items[] = array('path' => 'user/'. arg(1) .'/impersonate',
|
| 155 |
'title' => t('Impersonate'),
|
| 156 |
'callback' => 'impersonate_user_page',
|
| 157 |
'type' => MENU_LOCAL_TASK,
|
| 158 |
'access' => $user->uid,
|
| 159 |
'weight' => 2,
|
| 160 |
);
|
| 161 |
}
|
| 162 |
|
| 163 |
}
|
| 164 |
|
| 165 |
return $items;
|
| 166 |
}
|
| 167 |
|
| 168 |
/**
|
| 169 |
* Callback function for generating form for setting
|
| 170 |
* user impersonation
|
| 171 |
*
|
| 172 |
*/
|
| 173 |
function impersonate_user_page() {
|
| 174 |
//Generate the form
|
| 175 |
global $user;
|
| 176 |
|
| 177 |
if ($account = user_load(array('uid' => arg(1)))) {
|
| 178 |
$output = null;
|
| 179 |
if (user_impersonate_active() || user_access(IMPERSONATE_USER,$account)) {
|
| 180 |
drupal_set_title('Setup impersonating data');
|
| 181 |
$output = drupal_get_form('impersonate_setup_user');
|
| 182 |
}
|
| 183 |
|
| 184 |
return $output;
|
| 185 |
}
|
| 186 |
else {
|
| 187 |
drupal_not_found();
|
| 188 |
}
|
| 189 |
|
| 190 |
}
|
| 191 |
|
| 192 |
/**
|
| 193 |
* Pure form generation
|
| 194 |
*
|
| 195 |
*/
|
| 196 |
function impersonate_setup_user() {
|
| 197 |
global $user;
|
| 198 |
|
| 199 |
if (!user_impersonate_active()) {
|
| 200 |
$disable = !user_has_assigned_impersonate_roles($user->uid);
|
| 201 |
$form['user_to_impersonate'] = array(
|
| 202 |
'#type' => 'textfield',
|
| 203 |
'#title' => t('User to impersonate'),
|
| 204 |
'#maxlength' => 60,
|
| 205 |
'#description' => t("Choose a user you wish to impersonate."),
|
| 206 |
'#autocomplete_path' => 'user/impersonate_autocomplete',
|
| 207 |
'#required' => TRUE,
|
| 208 |
'#disabled' => $disable
|
| 209 |
);
|
| 210 |
|
| 211 |
$form['submit'] = array('#type' => 'submit',
|
| 212 |
'#value' => t('Impersonate'),
|
| 213 |
'#disabled' => $disable
|
| 214 |
);
|
| 215 |
|
| 216 |
$form['#submit']['impersonate_setup_user_submit'] = array();
|
| 217 |
$form['#validate']['impersonate_user_validate'] = array();
|
| 218 |
} else {
|
| 219 |
$form['submit'] = array('#type' => 'submit',
|
| 220 |
'#value' => t('Reset Impersonation'),
|
| 221 |
);
|
| 222 |
|
| 223 |
$form['#submit'] = array('impersonate_user_reset' => array());
|
| 224 |
}
|
| 225 |
|
| 226 |
return $form;
|
| 227 |
}
|
| 228 |
|
| 229 |
/**
|
| 230 |
* Enter description here...
|
| 231 |
*
|
| 232 |
*/
|
| 233 |
function impersonate_admin_user_page() {
|
| 234 |
//Generate the form
|
| 235 |
global $user;
|
| 236 |
|
| 237 |
if ($account = user_load(array('uid' => arg(1)))) {
|
| 238 |
$output = null;
|
| 239 |
if (user_access(ADMINISTER_IMPERSONATE_MODULE,$user)) {
|
| 240 |
drupal_set_title('Setup impersonating data');
|
| 241 |
$output = drupal_get_form('impersonate_admin_setup_page', $account);
|
| 242 |
}
|
| 243 |
|
| 244 |
return $output;
|
| 245 |
}
|
| 246 |
else {
|
| 247 |
drupal_not_found();
|
| 248 |
}
|
| 249 |
}
|
| 250 |
|
| 251 |
function impersonate_admin_setup_page($account) {
|
| 252 |
|
| 253 |
$settings = variable_get(get_impersonate_variable_name_for_user($account->uid),FALSE);
|
| 254 |
if ( isset($settings[0]) ) {
|
| 255 |
unset($settings[0]);
|
| 256 |
}
|
| 257 |
|
| 258 |
$form['selected_roles'] = array(
|
| 259 |
'#type' => 'checkboxes',
|
| 260 |
'#title' => t('User Roles'),
|
| 261 |
'#description' => t('List of all user roles in the system'),
|
| 262 |
'#options' => user_roles(),
|
| 263 |
'#default_value' => array()
|
| 264 |
);
|
| 265 |
|
| 266 |
if (is_array($settings)) {
|
| 267 |
$form['selected_roles']['#default_value'] = $settings;
|
| 268 |
}
|
| 269 |
|
| 270 |
$form['submit'] = array('#type' => 'submit',
|
| 271 |
'#value' => t('Save settings'),
|
| 272 |
);
|
| 273 |
|
| 274 |
$form['#submit'] = array('impersonate_user_admin_submit' => array(''));
|
| 275 |
$form['uid'] = array('#type' => 'value', '#value' => $account->uid);
|
| 276 |
|
| 277 |
return $form;
|
| 278 |
}
|
| 279 |
|
| 280 |
function impersonate_user_validate($form_id, $form_values) {
|
| 281 |
$username = $form_values['user_to_impersonate'];
|
| 282 |
$uid = get_uid_for_username($username);
|
| 283 |
if ($uid === FALSE) {
|
| 284 |
form_set_error('user_to_impersonate',t('Invalid username! Make sure username exists and you have permission to impersonate it.'));
|
| 285 |
}
|
| 286 |
}
|
| 287 |
|
| 288 |
function impersonate_setup_user_submit($form_id, $form_values) {
|
| 289 |
$username = $form_values['user_to_impersonate'];
|
| 290 |
$uid = get_uid_for_username($username);
|
| 291 |
impersonate_activate($uid);
|
| 292 |
return 'user';
|
| 293 |
}
|
| 294 |
|
| 295 |
function impersonate_user_reset($form_id, $form_values) {
|
| 296 |
impersonate_reset();
|
| 297 |
return 'user';
|
| 298 |
}
|
| 299 |
|
| 300 |
function impersonate_user_admin_submit($form_id, $form_values) {
|
| 301 |
$selected_roles = $form_values['selected_roles'];
|
| 302 |
if ( isset($selected_roles[0]) ) {
|
| 303 |
unset($selected_roles[0]);
|
| 304 |
}
|
| 305 |
//Check if we have empty selection
|
| 306 |
$empty_selection = true;
|
| 307 |
foreach ($selected_roles as $role) {
|
| 308 |
if ($role > 0) {
|
| 309 |
$empty_selection = false;
|
| 310 |
break;
|
| 311 |
}
|
| 312 |
}
|
| 313 |
$vname = get_impersonate_variable_name_for_user($form_values['uid']);
|
| 314 |
if ($empty_selection) {
|
| 315 |
variable_del($vname);
|
| 316 |
} else {
|
| 317 |
variable_set($vname,$selected_roles);
|
| 318 |
}
|
| 319 |
}
|
| 320 |
|
| 321 |
function impersonate_activate($uid) {
|
| 322 |
global $user;
|
| 323 |
if ($user->uid == $uid) {
|
| 324 |
return;
|
| 325 |
}
|
| 326 |
|
| 327 |
$account = user_load(array('uid' => $uid));
|
| 328 |
if ($account === FALSE) {
|
| 329 |
drupal_set_message('Invalid user account. Unable to impersonate', 'error');
|
| 330 |
} else {
|
| 331 |
$stored_variable = get_impersonate_variable_name();
|
| 332 |
if (variable_get($stored_variable,FALSE) === FALSE) {
|
| 333 |
variable_set($stored_variable,$user->uid);
|
| 334 |
}
|
| 335 |
$user = $account;
|
| 336 |
}
|
| 337 |
}
|
| 338 |
|
| 339 |
function impersonate_reset() {
|
| 340 |
global $user;
|
| 341 |
$stored_variable = get_impersonate_variable_name();
|
| 342 |
$original_uid = variable_get($stored_variable,FALSE);
|
| 343 |
if ($original_uid !== FALSE) {
|
| 344 |
$account = user_load(array('uid' => $original_uid));
|
| 345 |
$user = $account;
|
| 346 |
variable_del($stored_variable);
|
| 347 |
}
|
| 348 |
|
| 349 |
}
|
| 350 |
|