diff --git a/masquerade.info b/masquerade.info --- a/masquerade.info Thu Jan 15 03:14:12 1970 +++ b/masquerade.info Thu Jan 15 03:14:12 1970 @@ -2,11 +2,5 @@ description = "This module allows permitted users to masquerade as other users." core = 7.x files[] = masquerade.test +files[] = views/masquerade_handler_field_user_link.inc configure = admin/config/people/masquerade - -; Information added by drupal.org packaging script on 2011-05-04 -version = "7.x-1.x-dev" -core = "7.x" -project = "masquerade" -datestamp = "1304510964" - diff --git a/masquerade.module b/masquerade.module --- a/masquerade.module Thu Jan 15 03:14:12 1970 +++ b/masquerade.module Thu Jan 15 03:14:12 1970 @@ -837,3 +837,13 @@ $user = user_load($uid); watchdog('masquerade', 'User %user no longer masquerading as %masq_as.', array('%user' => $user->name, '%masq_as' => $oldname), WATCHDOG_INFO); } + +/** + * Implements hook_views_api() + */ +function masquerade_views_api() { + return array( + 'api' => 2, + 'path' => drupal_get_path('module', 'masquerade') . '/views', + ); +} diff --git a/masquerade.views.inc b/masquerade.views.inc --- a/masquerade.views.inc Thu Jan 15 03:14:12 1970 +++ b/masquerade.views.inc Thu Jan 15 03:14:12 1970 @@ -0,0 +1,106 @@ + array( + 'left_table' => 'users', + 'left_field' => 'uid', + 'field' => 'uid_as', + ), + 'user_is' => array( + 'left_table' => 'users', + 'left_field' => 'uid', + 'field' => 'uid_from', + ), + ); + + // A faux field that should allow us to access any user ID + $data['users']['masquerade_as'] = array( + // tell views that 'masquerade_as' isn't a real field + // and to use 'uid' instead + 'real field' => 'uid', + 'title' => t('Masquerade Link'), + 'help' => t('The user you want to masquerade as.'), + 'field' => array( + 'handler' => 'masquerade_handler_field_user', + 'click sortable' => TRUE, + ), + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'base field' => 'uid', + 'field' => 'uid', + 'label' => t('user'), + ), + ); + + $data['masquerade']['sid'] = array( + 'title' => t('Session SID'), + 'help' => t('The session ID of the person masquerading.'), + 'field' => array( + 'handler' => 'views_handler_field', + 'click sortable' => TRUE, + ), + ); + + // Who is BEING masqueraded. + $data['masquerade']['uid_as'] = array( + 'title' => t('Masquerade As'), + 'help' => t('The user this person is posing as.'), + 'field' => array( + 'handler' => 'views_handler_field_numeric', + 'click sortable' => TRUE, + ), + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'base field' => 'uid', + 'field' => 'uid_as', + 'label' => t('Masquerading as'), + ), + ); + + // Who actually IS masquerading. + $data['masquerade']['uid_from'] = array( + 'title' => t('Masquerade From'), + 'help' => t('The user this person actually is.'), + 'field' => array( + 'handler' => 'views_handler_field_numeric', + 'click sortable' => TRUE, + ), + 'relationship' => array( + 'handler' => 'views_handler_relationship', + 'base' => 'users', + 'base field' => 'uid', + 'field' => 'uid_from', + 'label' => t('Who is masquerading'), + ), + ); + + return $data; +} + +/** + * Use hook_views_handlers() to avoid forcing our handler to be loaded + * on every page call with files[] in the .info file. + */ +function masquerade_views_handlers() { + return array( + 'info' => array( + 'path' => drupal_get_path('module', 'masquerade') . '/views', + ), + 'handlers' => array( + 'masquerade_handler_field_user' => array( + 'parent' => 'views_handler_field_user' + ), + ), + ); +} diff --git a/masquerade_handler_field_user_link.inc b/masquerade_handler_field_user_link.inc --- a/masquerade_handler_field_user_link.inc Thu Jan 15 03:14:12 1970 +++ b/masquerade_handler_field_user_link.inc Thu Jan 15 03:14:12 1970 @@ -0,0 +1,85 @@ + FALSE); + $options['custom_destination'] = array('default' => ''); + return $options; + } + + /** + * Adds additional options to the Field options form + */ + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + + $form['provide_custom_destination'] = array( + '#title' => t('Provide a custom destination.'), + '#description' => t('This is helpful for providing a shortcut to a certain function of the site from one area or another.'), + '#type' => 'checkbox', + '#default_value' => !empty($this->options['provide_custom_destination']) ? $this->options['provide_custom_destination'] : FALSE, + ); + + // provide the an optional redirect immediately after switching to another user + $form['custom_destination'] = array( + '#title' => t('Alternate destination'), + '#description' => t('Redirect the browser to this page immediately after switching to another user. There is a caveat, however. When you switch back, masquerade will not remember where you last were.'), + '#type' => 'textfield', + '#dependency' => array( 'edit-options-provide-custom-destination' => array(TRUE) ), + '#default_value' => !empty($this->options['custom_destination']) && valid_url($this->options['custom_destination']) ? $this->sanitize_value($this->options['custom_destination']) : '', + ); + + // override the normal link-to title and description + $form['link_to_user']['#title'] = t('Generate a link to Masquerade as this user.'); + $form['link_to_user']['#description'] = t('Be careful not to rewrite this field with tokens that generate links. This field will only display a link to users who have permission to masquerade.'); + } + + /** + * Prevent users that don't have permission to masquerade from accessing this function. + */ + function access() { + return user_access('masquerade as user'); + } + + /** + * Generates a link + */ + function render_link($data, $values) { + if (isset($values->{$this->aliases['uid']}) && + $data !== NULL && + $data !== '') { + + // make sure any data being used to generate the link is clean + $switchto = $this->sanitize_value($values->{$this->aliases['uid']}); + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = "masquerade/switch/" . $switchto; + + // let drupal know who we are before we switch users. + $this->options['alter']['query'] = array('token' => drupal_get_token($this->options['alter']['path'])); + + // if the user provided a custom destination (that isn't blank), tack it onto the link + if ($this->options['provide_custom_destination']== TRUE && + $this->options['custom_destination'] != '') { + $this->options['alter']['query'] += array('destination' => $this->options['custom_destination']); + } + } + return $data; + } + +}