Projet

Général

Profil

Paste
Télécharger (30,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / masquerade / masquerade.module @ 64ad485a

1
<?php
2

    
3
/**
4
 * @file
5
 * The masquerade module allows administrators to masquerade as other user.
6
 */
7

    
8
/**
9
 * Implements hook_help().
10
 */
11
function masquerade_help($path, $arg) {
12
  switch ($path) {
13
    case 'admin/help#masquerade':
14
      return t('<p>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.</p><p>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.</p>');
15
    case 'admin/settings/masquerade':
16
      return t('Only the users with <strong>masquerade as admin</strong> 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.');
17
  }
18
}
19

    
20
/**
21
 * Implements hook_permission().
22
 *
23
 * @return array
24
 */
25
function masquerade_permission() {
26
  return array(
27
    'masquerade as user' => array(
28
      'title' => t('Masquerade as user'),
29
      'description' => t('Masquerade as another user.'),
30
    ),
31
    'masquerade as admin' => array(
32
      'title' => t('Masquerade as admin'),
33
      'description' => t('Masquerade as the site admin (UID 1).'),
34
    ),
35
    'administer masquerade' => array(
36
      'title' => t('Administer Masquerade'),
37
      'description' => t('Perform administration tasks and configure the Masquerade module.'),
38
    ),
39
  );
40
}
41

    
42
/**
43
 * Implements hook_init().
44
 */
45
function masquerade_init() {
46
  global $user;
47

    
48
  // Try to load masqing uid from masquerade table.
49
  $uid = db_query("SELECT uid_from FROM {masquerade} WHERE sid = :sid AND uid_as = :uid_as", array(
50
    ':sid' => session_id(),
51
    ':uid_as' => $user->uid,
52
  ))->fetchField();
53

    
54
  // We are using identical operator (===) instead of equal (==) because if
55
  // $uid === 0 we want to store the session variable. If there's no record in
56
  // masquerade table we clear the session variable.
57
  if ($uid === FALSE) {
58
    if (isset($_SESSION)) {
59
      unset($_SESSION['masquerading']);
60
    }
61
  }
62
  else {
63
    $_SESSION['masquerading'] = $uid;
64
  }
65
}
66

    
67
/**
68
 * Implements hook_cron().
69
 *
70
 * Cleanup masquerade records where people didn't use the switch back link
71
 * that would have cleanly removed the user switch record.
72
 */
73
function masquerade_cron() {
74
  // see http://drupal.org/node/268487 before modifying this query
75
  $subquery = db_select('sessions', 's');
76
  $subquery->addField('s', 'sid');
77

    
78
  $query = db_delete('masquerade');
79
  $query->condition('sid', $subquery, 'NOT IN');
80
  $query->execute();
81
}
82

    
83
/**
84
 * Implements hook_menu().
85
 */
86
function masquerade_menu() {
87
  $items = array();
88

    
89
  $default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', ''));
90
  if ($default_test_user && ($default_test_user->uid || $default_test_user->name == variable_get('anonymous', t('Anonymous')))) {
91
    $items['masquerade/switch/' . $default_test_user->uid] = array(
92
      'title' => 'Masquerade as @testuser',
93
      'title arguments' => array('@testuser' => $default_test_user->name),
94
      'page callback' => 'masquerade_switch_user_page',
95
      'page arguments' => array(2),
96
      'access callback' => 'masquerade_menu_access',
97
      'access arguments' => array('switch'),
98
      'type' => MENU_NORMAL_ITEM,
99
    );
100
  }
101

    
102
  $items['masquerade/switch/%'] = array(
103
    'title' => 'Masquerading',
104
    'page callback' => 'masquerade_switch_user_page',
105
    'page arguments' => array(2),
106
    'access callback' => 'masquerade_menu_access',
107
    'access arguments' => array('switch', 2),
108
    'type' => MENU_NORMAL_ITEM,
109
  );
110
  $items['masquerade/unswitch'] = array(
111
    'title' => 'Switch back',
112
    'page callback' => 'masquerade_switch_back_page',
113
    'access callback' => 'masquerade_menu_access',
114
    'access arguments' => array('unswitch'),
115
    'type' => MENU_NORMAL_ITEM,
116
  );
117
  $items['masquerade/autocomplete'] = array(
118
    'title' => '',
119
    'page callback' => 'masquerade_autocomplete',
120
    'access callback' => 'masquerade_menu_access',
121
    'access arguments' => array('autocomplete'),
122
    'type' => MENU_CALLBACK,
123
  );
124
  $items['masquerade/autocomplete-users'] = array(
125
    'title' => '',
126
    'page callback' => 'masquerade_autocomplete_multiple',
127
    'access callback' => 'masquerade_menu_access',
128
    'access arguments' => array('autocomplete'),
129
    'type' => MENU_CALLBACK,
130
  );
131
  $items['masquerade/autocomplete-user'] = array(
132
    'title' => 'Masquerade autocomplete',
133
    'page callback' => 'masquerade_autocomplete_multiple',
134
    'page arguments' => array(2, FALSE),
135
    'access arguments' => array('access user profiles'),
136
    'type' => MENU_CALLBACK,
137
  );
138
  $items['admin/config/people/masquerade'] = array(
139
    'title' => 'Masquerade',
140
    'description' => 'Masquerade module allows administrators to masquerade as other users.',
141
    'page callback' => 'drupal_get_form',
142
    'page arguments' => array('masquerade_admin_settings'),
143
    'access callback' => 'user_access',
144
    'access arguments' => array('administer masquerade'),
145
    'type' => MENU_NORMAL_ITEM,
146
  );
147

    
148
  return $items;
149
}
150

    
151
/**
152
 * Implements hook_menu_alter().
153
 *
154
 * We need to add a token to the Masquerade paths to protect against CSRF
155
 * attacks. Since menu items in Drupal do not support dynamic elements these
156
 * tokens need to be added during rendering via masquerade_translated_menu_link_alter().
157
 * Set the 'alter'-option to TRUE to make sure
158
 * the links get passed through masquerade_translated_menu_link_alter.
159
 */
160
function masquerade_menu_alter(&$items) {
161
  $default_test_user = _masquerade_user_load(variable_get('masquerade_test_user', ''));
162
  if (isset($default_test_user->uid)) {
163
    $items['masquerade/switch/' . $default_test_user->uid]['options']['alter'] = TRUE;
164
  }
165
  $items['masquerade/switch/%']['options']['alter'] = TRUE;
166
  $items['masquerade/unswitch']['options']['alter'] = TRUE;
167
}
168

    
169
/**
170
 * Implements hook_translated_menu_link_alter().
171
 *
172
 * Dynamically add the CSRF protection token to the Masquerade menu items.
173
 */
174
function masquerade_translated_menu_link_alter(&$item, $map) {
175
  if (isset($item['page_callback'])) {
176
    if ($item['page_callback'] == 'masquerade_switch_user_page' && isset($map[2])) {
177
      $item['localized_options']['query']['token'] = drupal_get_token('masquerade/switch/' . $map[2]);
178
    }
179
    elseif ($item['page_callback'] == 'masquerade_switch_back_page') {
180
      $item['localized_options']['query']['token'] = drupal_get_token('masquerade/unswitch');
181
    }
182
  }
183
}
184

    
185
/**
186
 * Implements hook_user_operations().
187
 */
188
function masquerade_user_operations() {
189
  return array(
190
    'masquerade' => array(
191
      'label' => t('Masquerade as user'),
192
      'callback' => 'masquerade_user_operations_masquerade',
193
    ),
194
  );
195
}
196

    
197
/**
198
 * Callback for user operation.
199
 */
200
function masquerade_user_operations_masquerade(array $accounts) {
201
  // Only process the first account since switching to multiple makes no sense.
202
  if (($uid = current($accounts)) && masquerade_menu_access('switch', $uid)) {
203
    masquerade_switch_user($uid);
204
  }
205
}
206

    
207
/**
208
 * Determine if the current user has permission to switch users.
209
 *
210
 * @param string $type
211
 *   Either 'switch', 'unswitch', 'user', or 'autocomplete'.
212
 *
213
 * @param object $uid
214
 *   An optional parameter indicating a specific uid to switch to.
215
 *   Otherwise, return if the user can switch to any user account.
216
 *
217
 * @return
218
 *   TRUE, if the user can perform the requested action, FALSE otherwise.
219
 */
220

    
221
function masquerade_menu_access($type, $uid = NULL) {
222
  switch ($type) {
223
    case 'unswitch':
224
      return isset($_SESSION['masquerading']) || arg(2) == 'menu-customize' || arg(2) == 'menu';
225
    case 'autocomplete':
226
      return isset($_SESSION['masquerading']) || (user_access('masquerade as user') || user_access('masquerade as admin'));
227
      break;
228
    case 'user':
229
      global $user;
230
      return db_query("SELECT 1 FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $user->uid))->fetchField();
231
      break;
232
    case 'switch':
233
      $switch_to_account = FALSE;
234
      global $user;
235
      if ($uid) {
236
        if (!is_numeric($uid)) {
237
          return FALSE;
238
        }
239
        if ($account = user_load($uid)) {
240
          $switch_to_account = db_query("SELECT 1 FROM {masquerade_users} WHERE uid_from = :uid_from AND uid_to = :uid_to", array(
241
            ':uid_from' => $user->uid,
242
            ':uid_to' => $account->uid
243
          ))->fetchField();
244
        }
245
      }
246
      return !isset($_SESSION['masquerading']) && (user_access('masquerade as user') || user_access('masquerade as admin') || $switch_to_account);
247
      break;
248
  }
249
}
250

    
251
/**
252
 * Admin settings form.
253
 */
254
function masquerade_admin_settings() {
255
  // create a list of roles; all selected roles are considered administrative.
256
  $roles = user_roles();
257
  $form['masquerade_admin_roles'] = array(
258
    '#type' => 'checkboxes',
259
    '#title' => t('Roles that are considered "administrators" for masquerading'),
260
    '#options' => $roles,
261
    '#default_value' => variable_get('masquerade_admin_roles', array()),
262
  );
263

    
264
  $test_name = _masquerade_user_load(variable_get('masquerade_test_user', ''));
265

    
266
  $form['masquerade_test_user'] = array(
267
    '#type' => 'textfield',
268
    '#title' => t('Menu <em>Quick Switch</em> user'),
269
    '#autocomplete_path' => 'masquerade/autocomplete',
270
    '#default_value' => isset($test_name->name) ? check_plain($test_name->name) : '',
271
    '#description' => t('Enter the username of an account you wish to switch easily between via a menu item.'),
272
    '#maxlength' => NULL,
273
  );
274

    
275
  $quick_switch = user_load_multiple(variable_get('masquerade_quick_switches', array()));
276
  $quick_switch_users = array();
277
  foreach ($quick_switch as $uid => $account) {
278
    if ($uid == 0) {
279
      $account->name = variable_get('anonymous', t('Anonymous'));
280
    }
281
    $quick_switch_users[] = $account->name;
282
  }
283
  $form['masquerade_quick_switches'] = array(
284
    '#type' => 'textfield',
285
    '#title' => t('Masquerade Block <em>Quick Switch</em> users'),
286
    '#autocomplete_path' => 'masquerade/autocomplete-users',
287
    '#default_value' => drupal_implode_tags($quick_switch_users),
288
    '#description' => t('Enter the usernames, separated by commas, of accounts to show as quick switch links in the Masquerade block.'),
289
    '#maxlength' => NULL,
290
  );
291

    
292
  $form = system_settings_form($form);
293
  $form['#validate'][] = 'masquerade_admin_settings_validate';
294
  $form['#submit'][] = 'masquerade_admin_settings_submit';
295

    
296
  return $form;
297
}
298

    
299
function masquerade_admin_settings_validate($form, &$form_state) {
300
  if (!empty($form_state['values']['masquerade_test_user'])) {
301
    $test_user = _masquerade_user_load($form_state['values']['masquerade_test_user']);
302
    if (!$test_user) {
303
      form_set_error('masquerade_test_user', t('%user does not exist. Please enter a valid username.', array('%user' => $form_state['values']['masquerade_test_user'])));
304
    }
305
  }
306
  // Needs to rebuild menu in masquerade_admin_settings_submit().
307
  $form_state['masquerade_rebuild_menu'] = (variable_get('masquerade_test_user', '') != $form_state['values']['masquerade_test_user']);
308

    
309
  // A comma-separated list of users.
310
  $masquerade_switches = drupal_explode_tags($form_state['values']['masquerade_quick_switches']);
311
  // Change user names to user ID's for system_settings_form_submit() to save.
312
  $masquerade_uids = array();
313
  foreach ($masquerade_switches as $switch_user) {
314
    $test_user = _masquerade_user_load($switch_user);
315
    if (!$test_user) {
316
      form_set_error('masquerade_quick_switches', t('%user does not exist. Please enter a valid username.', array('%user' => $switch_user)));
317
    }
318
    else {
319
      $masquerade_uids[] = $test_user->uid;
320
    }
321
  }
322
  $form_state['values']['masquerade_quick_switches'] = $masquerade_uids;
323
}
324

    
325
function masquerade_admin_settings_submit($form, &$form_state) {
326
  // Rebuild the menu system so the menu "Quick Switch" user is updated.
327
  if ($form_state['masquerade_rebuild_menu']) {
328
    menu_rebuild();
329
  }
330
}
331

    
332
/**
333
 * Wrapper around user_load() to allow the loading of anonymous users.
334
 *
335
 * @param $username
336
 *   The username of the user you wish to load (i.e. $user->name). To load the
337
 *   anonymous user, pass the value of the 'anonymous' variable.
338
 *
339
 * @return
340
 *   A fully-loaded $user object upon successful user load or FALSE if user
341
 *   cannot be loaded.
342
 */
343
function _masquerade_user_load($username) {
344
  $account = FALSE;
345
  if (!empty($username)) {
346
    $anon = variable_get('anonymous', t('Anonymous'));
347
    $account = user_load_by_name(($username == $anon ? '' : $username));
348
    if (isset($account->uid) && empty($account->uid)) {
349
      // Anonymous user should have a name.
350
      $account->name = $anon;
351
    }
352
  }
353
  return $account;
354
}
355

    
356
/**
357
 * Implements hook_user_logout().
358
 */
359
function masquerade_user_logout($account) {
360
  if (!empty($account->masquerading)) {
361
    global $user;
362
    cache_clear_all($user->uid, 'cache_menu', TRUE);
363
    $real_user = user_load($user->masquerading);
364
    watchdog('masquerade', "User %user no longer masquerading as %masq_as.", array('%user' => $real_user->name, '%masq_as' => $user->name), WATCHDOG_INFO);
365

    
366
    $query = db_delete('masquerade');
367
    $query->condition('sid', session_id());
368
    $query->condition('uid_as', $account->uid);
369
    $query->execute();
370
  }
371
}
372

    
373
/**
374
 * Implements hook_user_view().
375
 */
376
function masquerade_user_view($account, $view_mode, $langcode) {
377
  // check if user qualifies as admin
378
  $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
379
  $perm = $account->uid == 1 || array_intersect(array_keys((array)$account->roles), $roles) ?
380
    'masquerade as admin' :
381
    'masquerade as user';
382

    
383
  global $user;
384

    
385
  if (user_access($perm) && empty($account->masquerading) && $user->uid != $account->uid) {
386
    $account->content['masquerade'] = array(
387
      '#markup' => l(t('Masquerade as !user', array('!user' => $account->name)),
388
        'masquerade/switch/' . $account->uid,
389
        array('query' => array(
390
          'token' => drupal_get_token('masquerade/switch/' . $account->uid)),
391
          'destination' => $_GET['q'],
392
          'attributes' => array('class' => 'masquerade-switch'),
393
        )),
394
      '#weight' => 10,
395
    );
396
  }
397
}
398

    
399
/**
400
 * Implements hook_form_FORM_ID_alter().
401
 */
402
function masquerade_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
403
  if ($form['#user_category'] != 'account') {
404
    // Do not show this form for different categories.
405
    return;
406
  }
407
  $form['masquerade'] = array(
408
    '#type' => 'fieldset',
409
    '#title' => t('Masquerade settings'),
410
    '#access' => user_access('administer masquerade'),
411
  );
412
  $edit_user = $form['#user'];
413
  $uids = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $edit_user->uid))
414
    ->fetchCol();
415
  $users = user_load_multiple($uids);
416
  $masquerade_users = array();
417
  foreach ($users as $uid => $account) {
418
    if ($uid == 0) {
419
      $masquerade_users[] = variable_get('anonymous', t('Anonymous'));
420
    }
421
    else {
422
      $masquerade_users[] = $account->name;
423
    }
424
  }
425
  $form['masquerade']['masquerade_users'] = array(
426
    '#type' => 'textfield',
427
    '#title' => t('Enter the users this user is able to masquerade as'),
428
    '#description' => t('Enter a comma separated list of user names that this user can masquerade as.'),
429
    '#autocomplete_path' => 'masquerade/autocomplete-user',
430
    '#default_value' => drupal_implode_tags($masquerade_users),
431
    '#maxlength' => NULL,
432
  );
433
  $form['#validate'][] = 'masquerade_user_validate';
434
  $form['#submit'][] = 'masquerade_user_submit';
435
}
436

    
437
/**
438
 * Validates user account form.
439
 */
440
function masquerade_user_validate(&$form, $form_state) {
441
  if (isset($form_state['values']['masquerade_users'])) {
442
    $users = drupal_explode_tags($form_state['values']['masquerade_users']);
443
    foreach ($users as $username) {
444
      if (!_masquerade_user_load($username)) {
445
        form_set_error('masquerade_users', t('%user is not a valid user name.', array('%user' => $username)));
446
      }
447
    }
448
  }
449
}
450

    
451
/**
452
 * Submit handler for masquerade users form element.
453
 */
454
function masquerade_user_submit(&$form, $form_state) {
455
  global $_masquerade_old_session_id;
456
  $_masquerade_old_session_id = session_id();
457
}
458

    
459
/**
460
 * Implements hook_user_update().
461
 */
462
function masquerade_user_update(&$edit, $account, $category) {
463
  global $_masquerade_old_session_id;
464
  if ($category == 'account' && isset($edit['masquerade_users'])) {
465
    $query = db_delete('masquerade_users');
466
    $query->condition('uid_from', $account->uid);
467
    $query->execute();
468
    // Save users from settings form.
469
    $users = drupal_explode_tags($edit['masquerade_users']);
470
    $query = db_insert('masquerade_users')->fields(array('uid_from', 'uid_to'));
471
    foreach ($users as $username) {
472
      if ($to_user = _masquerade_user_load($username)) {
473
        $query->values(array(
474
          'uid_from' => $account->uid,
475
          'uid_to' => $to_user->uid,
476
        ));
477
      }
478
    }
479
    $query->execute();
480
    $edit['masquerade_users'] = NULL;
481

    
482
    // Update user session...
483
    // @TODO check other way of session API.
484
    if (!empty($_masquerade_old_session_id)) {
485
      $query = db_update('masquerade');
486
      $query->fields(array(
487
        'sid' => session_id(),
488
      ));
489
      $query->condition('sid', $_masquerade_old_session_id);
490
      $query->execute();
491
    }
492
  }
493
}
494

    
495
/**
496
 * Implements hook_user_delete().
497
 */
498
function masquerade_user_delete($account) {
499
  // Cleanup tables.
500
  $query = db_delete('masquerade_users');
501
  $conditions = db_or();
502
  $conditions->condition('uid_from', $account->uid);
503
  $conditions->condition('uid_to', $account->uid);
504
  $query->condition($conditions);
505
  $query->execute();
506
  // Cleanup variables.
507
  $switches = variable_get('masquerade_quick_switches', array());
508
  $switches_new = array_diff($switches, array($account->uid));
509
  if ($switches != $switches_new) {
510
    variable_set('masquerade_quick_switches', $switches_new);
511
    // @TODO Implement block cache cleaning.
512
    menu_rebuild();
513
  }
514
}
515

    
516
/**
517
 * Implements hook_block_info().
518
 */
519
function masquerade_block_info() {
520
  $blocks = array();
521
  $blocks['masquerade'] = array(
522
    'info' => t('Masquerade'),
523
    'cache' => DRUPAL_NO_CACHE,
524
  );
525
  return $blocks;
526
}
527

    
528
/**
529
 * Implements hook_block_view().
530
 */
531
function masquerade_block_view($delta = '') {
532
  $block = array();
533
  switch ($delta) {
534
    case 'masquerade':
535
      if (isset($_SESSION['masquerading']) || (user_access('masquerade as user') || user_access('masquerade as admin'))) {
536
        $block['subject'] = t('Masquerade');
537
        $block['content'] = drupal_get_form('masquerade_block_1');
538
      }
539
      break;
540
  }
541
  return $block;
542
}
543

    
544
/**
545
 * Masquerade block form.
546
 */
547
function masquerade_block_1() {
548
  global $user;
549
  $quick_switch_links = array();
550
  $markup_value = '';
551
  if (isset($_SESSION['masquerading'])) {
552
    $quick_switch_links[] = l(t('Switch back'), 'masquerade/unswitch', array('query' => array('token' => drupal_get_token('masquerade/unswitch'))));
553
    if ($user->uid > 0) {
554
      $markup_value = t('You are masquerading as <a href="@user-url">%masq_as</a>.', array('@user-url' => url('user/' . $user->uid), '%masq_as' => $user->name));
555
    }
556
    else {
557
      $markup_value = t('You are masquerading as %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous'))));
558
    }
559
  }
560
  else {
561
    $quick_switches = variable_get('masquerade_quick_switches', array());
562

    
563
    // Add in user-specific switches, and prevent duplicates.
564
    $user_switches = db_query("SELECT uid_to FROM {masquerade_users} WHERE uid_from = :uid_from", array(':uid_from' => $user->uid))->fetchCol();
565
    $masquerade_switches = array_unique(array_merge($quick_switches, $user_switches));
566

    
567
    foreach ($masquerade_switches as $switch_user) {
568
      if (!isset($_SESSION['user']->uid) || $switch_user != $_SESSION['user']->uid) {
569
        $account = user_load($switch_user);
570
        if (isset($account->uid)) {
571
          $switch_link = 'masquerade/switch/' . $account->uid;
572
          if ($account->uid) {
573
            $quick_switch_links[] = l($account->name, $switch_link, array('query' => array('token' => drupal_get_token($switch_link))));
574
          }
575
          if ($switch_user == 0) {
576
            $account->name = variable_get('anonymous', t('Anonymous'));
577
            $quick_switch_links[] = l($account->name, $switch_link, array('query' => array('token' => drupal_get_token($switch_link))));
578
          }
579
        }
580
      }
581
    }
582

    
583
    if (masquerade_menu_access('autocomplete')) {
584
      $markup_value .= t('Enter the username to masquerade as.');
585
      $form['masquerade_user_field'] = array(
586
        '#prefix' => '<div class="container-inline">',
587
        '#type' => 'textfield',
588
        '#size' => '18',
589
        '#default_value' => '',
590
        '#autocomplete_path' => 'masquerade/autocomplete',
591
        '#required' => TRUE,
592
      );
593
      $form['submit'] = array(
594
        '#type' => 'submit',
595
        '#value' => t('Go'),
596
        '#suffix' => '</div>',
597
      );
598
    }
599

    
600
  }
601
  if ($quick_switch_links) {
602
    $markup_value .= '<div id="quick_switch_links">' . t('Quick switches:') . theme('item_list', array('items' => $quick_switch_links)) . '</div>';
603
  }
604
  $form['masquerade_desc'] = array(
605
    '#prefix' => '<div class="form-item"><div class="description">',
606
    '#markup' => $markup_value,
607
    '#suffix' => '</div></div>',
608
  );
609
  return $form;
610
}
611

    
612
/**
613
 * Masquerade block form validation.
614
 */
615
function masquerade_block_1_validate($form, &$form_state) {
616
  global $user;
617
  //unset($form);
618
  $name = $form_state['values']['masquerade_user_field'];
619
  if (isset($_SESSION['masquerading'])) {
620
    form_set_error('masquerade_user_field', t('You are already masquerading. Please <a href="@unswitch">switch back</a> to your account to masquerade as another user.', array('@unswitch' => url('masquerade/unswitch', array('query' => array('token' => drupal_get_token('masquerade/unswitch')))))));
621
  }
622
  if ($name != variable_get('anonymous', t('Anonymous')) && module_exists('alt_login')) {
623
    $alt_login = db_query("SELECT u.name FROM {users} u INNER JOIN {alt_login} al ON u.uid = al.uid WHERE al.alt_login = :alt_login", array(
624
      ':alt_login' => $name
625
    ))->fetchObject();
626
    if ($alt_login->name) {
627
      $name = $alt_login->name;
628
    }
629
  }
630
  $masq_user = _masquerade_user_load($name);
631
  if (!$masq_user) {
632
    form_set_error('masquerade_user_field', t('User %masq_as does not exist. Please enter a valid username.', array('%masq_as' => $form_state['values']['masquerade_user_field'])));
633
  }
634
  elseif ($masq_user->uid == $user->uid) {
635
    form_set_error('masquerade_user_field', t('You cannot masquerade as yourself. Please choose a different user to masquerade as.'));
636
  }
637
  elseif (variable_get('maintenance_mode', 0) && !user_access('access site in maintenance mode', $masq_user)) {
638
    form_set_error('masquerade_user_field', t('It is not possible to masquerade in off-line mode as !user does not have the %config-perm permission. Please <a href="@site-maintenance">set the site status</a> to "online" to masquerade as !user.', array('!user' => theme('username', array('account' => $masq_user)), '%config-perm' => 'use the site in maintenance mode', '@site-maintenance' => url('admin/settings/site-maintenance'))));
639
  }
640
  else {
641
    $form_state['values']['masquerade_user_field'] = $name;
642
  }
643
}
644

    
645
/**
646
 * Masquerade block form submission.
647
 */
648
function masquerade_block_1_submit($form, &$form_state) {
649
  //unset($form);
650
  $masq_user = _masquerade_user_load($form_state['values']['masquerade_user_field']);
651
  if (!masquerade_switch_user($masq_user->uid)) {
652
    drupal_access_denied();
653
  }
654
  else {
655
    drupal_goto($_SERVER['HTTP_REFERER']);
656
  }
657
}
658

    
659
/**
660
 * Returns JS array for Masquerade autocomplete fields.
661
 */
662
function masquerade_autocomplete($string) {
663
  $matches = array();
664
  // Anonymous user goes first to be visible for user.
665
  $anonymous = variable_get('anonymous', t('Anonymous'));
666
  if (stripos($anonymous, $string) === 0) {
667
    $matches[$anonymous] = $anonymous;
668
  }
669
  // Other suggestions.
670
  $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER(:string)", 0, 10, array(
671
    ':string' => $string . '%',
672
  ));
673
  foreach ($result as $user) {
674
    $matches[$user->name] = check_plain($user->name);
675
  }
676
  if (module_exists('devel')) {
677
    $GLOBALS['devel_shutdown'] = FALSE;
678
  }
679
  drupal_json_output($matches);
680
}
681

    
682
/**
683
 * Returns JS array for Masquerade autocomplete fields.
684
 *
685
 * Supports multiple entries separated by a comma.
686
 *
687
 * @param $string
688
 *   The string of autocmplete value submitted by the user.
689
 * @param $add_anonymous
690
 *   Flag to include Anonymous user into result.
691
 */
692
function masquerade_autocomplete_multiple($string, $add_anonymous = TRUE) {
693
  $matches = array();
694
  // The user enters a comma-separated list of users. We only autocomplete the last user.
695
  $users_typed = drupal_explode_tags($string);
696
  // Fetch last string.
697
  $last_string = drupal_strtolower(array_pop($users_typed));
698
  if ($last_string) {
699
    $prefix = count($users_typed) ? implode(', ', $users_typed) . ', ' : '';
700
    if ($add_anonymous) {
701
      // Anonymous user goes first to be visible for user.
702
      $anonymous = variable_get('anonymous', t('Anonymous'));
703
      if (stripos($anonymous, $last_string) === 0) {
704
        $matches[$prefix . $anonymous] = $anonymous;
705
      }
706
    }
707
    // Other suggestions.
708
    $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE :string", 0, 10, array(
709
      ':string' => $last_string . '%',
710
    ));
711
    foreach ($result as $user) {
712
      $matches[$prefix . $user->name] = check_plain($user->name);
713
    }
714

    
715
    // Remove existing tags.
716
    $matches = array_diff($matches, $users_typed);
717

    
718
    // @todo Check compatibility for D7.
719
    if (module_exists('alt_login')) {
720
      $result = db_query_range("SELECT u.alt_login AS alt_login FROM {alt_login} u WHERE LOWER(u.alt_login) LIKE LOWER(:string)", 0, 10, array(
721
        ':string' => $last_string . '%',
722
      ));
723
      foreach ($result as $user) {
724
        $matches[$user->alt_login] = check_plain($user->alt_login);
725
      }
726
    }
727
  }
728
  if (module_exists('devel')) {
729
    $GLOBALS['devel_shutdown'] = FALSE;
730
  }
731
  drupal_json_output($matches);
732
}
733

    
734
/**
735
 * Page callback to switch users.
736
 */
737
function masquerade_switch_user_page($uid) {
738
  if (isset($_GET['token']) && drupal_valid_token($_GET['token'], 'masquerade/switch/' . $uid) && masquerade_switch_user($uid)) {
739
    drupal_goto($_SERVER['HTTP_REFERER']);
740
  }
741
  else {
742
    drupal_access_denied();
743
  }
744
}
745

    
746
/**
747
 * Allows a user with the right permissions to become the selected user.
748
 *
749
 * @param $uid
750
 *   The user ID to switch to.
751
 *
752
 * @return
753
 *   TRUE if the user was sucessfully switched, or FALSE if there was an error.
754
 */
755
function masquerade_switch_user($uid) {
756
  global $user;
757
  if (!is_numeric($uid)) {
758
    drupal_set_message(t('A user id was not correctly passed to the switching function.'));
759
    watchdog('masquerade', 'The user id provided to switch users was not numeric.', NULL, WATCHDOG_ERROR);
760
    return drupal_goto($_SERVER['HTTP_REFERER']);
761
  }
762

    
763
  $new_user = user_load($uid);
764

    
765
  $roles = array_keys(array_filter(variable_get('masquerade_admin_roles', array())));
766
  $perm = $uid == 1 || array_intersect(array_keys($new_user->roles), $roles) ?
767
    'masquerade as admin' :
768
    'masquerade as user';
769

    
770
  // Check to see if we need admin permission.
771
  $results = db_query_range('SELECT 1 FROM {masquerade_users} WHERE uid_from = :uid_from AND uid_to = :uid_to', 0, 1, array(
772
    ':uid_from' => $user->uid,
773
    ':uid_to' => $new_user->uid,
774
  ));
775
  if (!user_access($perm) && !isset($_SESSION['masquerading']) && !$results->fetchField()) {
776
    watchdog('masquerade', 'This user requires administrative permissions to switch to the user %user.', array('%user' => $new_user->name), WATCHDOG_ERROR);
777
    return FALSE;
778
  }
779

    
780
  if ($user->uid == $uid || isset($user->masquerading)) {
781
    watchdog('masquerade', 'This user is already %user.', array('%user' => $new_user->name), WATCHDOG_ERROR);
782
    return FALSE;
783
  }
784

    
785
  if (variable_get('maintenance_mode', 0) && !user_access('access site in maintenance mode', $new_user)) {
786
    drupal_set_message(t('It is not possible to masquerade in off-line mode as %user does not have the %config-perm permission. Please <a href="@site-maintenance">set the site status</a> to "online" to masquerade as %user.', array('%user' => $new_user->name, '%config-perm' => 'use the site in maintenance mode', '@site-maintenance' => url('admin/settings/site-maintenance'))));
787
    return FALSE;
788
  }
789

    
790
  // Call logout hooks when switching from original user.
791
  module_invoke_all('user_logout', $user);
792
  drupal_session_regenerate();
793

    
794
  $query = db_insert('masquerade');
795
  $query->fields(array(
796
    'uid_from' => $user->uid,
797
    'uid_as' => $new_user->uid,
798
    'sid' => session_id(),
799
  ));
800
  $query->execute();
801
  // switch user
802

    
803
  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);
804
  drupal_set_message(t('You are now masquerading as !masq_as.', array('!masq_as' => theme('username', array('account' => $new_user)))));
805
  $user->masquerading = $new_user->uid;
806
  $user = $new_user;
807

    
808
  // Call all login hooks when switching to masquerading user.
809
  $edit = array(); // Passed by reference.
810
  user_module_invoke('login', $edit, $user);
811

    
812
  return TRUE;
813
}
814

    
815
/**
816
 * Allows a user who is currently masquerading to become a new user.
817
 */
818
function masquerade_switch_back_page() {
819
  if (isset($_GET['token']) && drupal_valid_token($_GET['token'], 'masquerade/unswitch')) {
820
    global $user;
821
    $olduser = $user;
822
    masquerade_switch_back();
823
    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)))));
824
    drupal_goto($_SERVER['HTTP_REFERER']);
825
  }
826
  else {
827
    drupal_access_denied();
828
  }
829
}
830

    
831
/**
832
 * Function for a masquerading user to switch back to the previous user.
833
 */
834
function masquerade_switch_back() {
835
  // switch user
836
  global $user;
837
  cache_clear_all($user->uid, 'cache_menu', TRUE);
838
  $uid = db_query("SELECT m.uid_from FROM {masquerade} m WHERE m.sid = :sid AND m.uid_as = :uid_as ", array(
839
    ':sid' => session_id(),
840
    ':uid_as' => $user->uid,
841
  ))->fetchField();
842
  // erase record
843
  $conditions = db_or();
844
  $conditions->condition('sid', session_id());
845
  $conditions->condition('uid_as', $user->uid);
846
  $query = db_delete('masquerade');
847
  $query->condition($conditions);
848
  $query->execute();
849
  $oldname = ($user->uid == 0 ? variable_get('anonymous', t('Anonymous')) : $user->name);
850

    
851
  // Call logout hooks when switching from masquerading user.
852
  module_invoke_all('user_logout', $user);
853
  drupal_session_regenerate();
854

    
855
  $user = user_load($uid);
856

    
857
  // Call all login hooks when switching back to original user.
858
  $edit = array(); // Passed by reference.
859
  user_module_invoke('login', $edit, $user);
860

    
861
  watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO);
862
}