From 88999e38901f4d04a14f3b6d98f5270e2491b384 Mon Sep 17 00:00:00 2001
From: Alex Verbruggen <alexander.verbruggen@hp.com>
Date: Wed, 26 Dec 2012 12:16:19 +0100
Subject: [PATCH] Added submodule for masquerade integration

---
 admin_menu.module                                  |    2 +-
 admin_menu_masquerade/admin_menu_masquerade.css    |   32 ++++++++++
 admin_menu_masquerade/admin_menu_masquerade.info   |    6 ++
 .../admin_menu_masquerade.install                  |   38 ++++++++++++
 admin_menu_masquerade/admin_menu_masquerade.js     |    7 ++
 admin_menu_masquerade/admin_menu_masquerade.module |   64 ++++++++++++++++++++
 6 files changed, 148 insertions(+), 1 deletions(-)
 create mode 100644 admin_menu_masquerade/admin_menu_masquerade.css
 create mode 100644 admin_menu_masquerade/admin_menu_masquerade.info
 create mode 100644 admin_menu_masquerade/admin_menu_masquerade.install
 create mode 100644 admin_menu_masquerade/admin_menu_masquerade.js
 create mode 100644 admin_menu_masquerade/admin_menu_masquerade.module

diff --git a/admin_menu.module b/admin_menu.module
index 301b4d0..68dd27e 100644
--- a/admin_menu.module
+++ b/admin_menu.module
@@ -153,7 +153,7 @@ function admin_menu_system_info_alter(&$info, $file, $type) {
  * Implements hook_page_build().
  */
 function admin_menu_page_build(&$page) {
-  if (!user_access('access administration menu') || admin_menu_suppress(FALSE)) {
+  if (!isset($_SESSION['masquerading']) && (!user_access('access administration menu') || admin_menu_suppress(FALSE))) {
     return;
   }
   // Performance: Skip this entirely for AJAX requests.
diff --git a/admin_menu_masquerade/admin_menu_masquerade.css b/admin_menu_masquerade/admin_menu_masquerade.css
new file mode 100644
index 0000000..8948e7a
--- /dev/null
+++ b/admin_menu_masquerade/admin_menu_masquerade.css
@@ -0,0 +1,32 @@
+
+/**
+ * @file
+ * Styling for the masquerade form.
+ */
+
+/**
+ * Masquerade form.
+ */
+html.js .admin-menu-masquerade-form .form-submit {
+  /*display: none;*/
+}
+.admin-menu-masquerade-form {
+  float:right;
+  margin-left: 10px;
+}
+.admin-menu-masquerade-form input[name='masquerade_user_field']{
+  background: none no-repeat scroll right center #FFFFFF;
+  border: medium none;
+  border-radius: 5px 5px 5px 5px;
+  font-size: 10px;
+  margin: 1px 0;
+  outline: medium none;
+  padding: 2px 22px 2px 4px;
+  width: 80px;
+}
+
+.admin-menu-masquerade-form #edit-submit {
+  padding:0 5px;
+  margin:0;
+  border-radius: 5px 5px 5px 5px;
+}
\ No newline at end of file
diff --git a/admin_menu_masquerade/admin_menu_masquerade.info b/admin_menu_masquerade/admin_menu_masquerade.info
new file mode 100644
index 0000000..8a3808e
--- /dev/null
+++ b/admin_menu_masquerade/admin_menu_masquerade.info
@@ -0,0 +1,6 @@
+name = Administration menu Masquerade form
+description = Integration with masquerade module
+package = Administration
+core = 7.x
+dependencies[] = admin_menu
+dependencies[] = masquerade
diff --git a/admin_menu_masquerade/admin_menu_masquerade.install b/admin_menu_masquerade/admin_menu_masquerade.install
new file mode 100644
index 0000000..74c139f
--- /dev/null
+++ b/admin_menu_masquerade/admin_menu_masquerade.install
@@ -0,0 +1,38 @@
+<?php
+
+/**
+ * @file
+ * Installation functionality for Administration menu toolbar module.
+ */
+
+/**
+ * Implements hook_install().
+ */
+function admin_menu_masquerade_install() {
+  // Required to load JS/CSS in hook_init() after admin_menu and admin_menu_toolbar.
+  db_update('system')
+    ->fields(array('weight' => 102))
+    ->condition('type', 'module')
+    ->condition('name', 'admin_menu_masquerade')
+    ->execute();
+}
+
+/**
+ * Set module weight to a value higher than admin_menu.
+ *
+ * At this point, admin_menu should have a weight of 100. To account for
+ * customized weights, we increase the weight relatively.
+ *
+ * @see admin_menu_masquerade_install()
+ */
+function admin_menu_masquerade_update_6300() {
+  $weight = db_query("SELECT weight FROM {system} WHERE type = 'module' AND name = 'admin_menu'")->fetchField();
+  $weight++;
+  $weight++;
+  db_update('system')
+    ->fields(array('weight' => $weight))
+    ->condition('type', 'module')
+    ->condition('name', 'admin_menu_masquerade')
+    ->execute();
+}
+
diff --git a/admin_menu_masquerade/admin_menu_masquerade.js b/admin_menu_masquerade/admin_menu_masquerade.js
new file mode 100644
index 0000000..8e42d02
--- /dev/null
+++ b/admin_menu_masquerade/admin_menu_masquerade.js
@@ -0,0 +1,7 @@
+(function ($) {
+Drupal.jsAC.prototype.select = function (node) {
+  this.input.value = $(node).data('autocompleteValue');
+  if(jQuery(this.input).hasClass('auto_submit')){
+    this.input.form.submit();
+  }};
+})(jQuery);
\ No newline at end of file
diff --git a/admin_menu_masquerade/admin_menu_masquerade.module b/admin_menu_masquerade/admin_menu_masquerade.module
new file mode 100644
index 0000000..c9de0d4
--- /dev/null
+++ b/admin_menu_masquerade/admin_menu_masquerade.module
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * @file
+ * Render masquerade form into the administration menu
+ */
+
+/**
+ * Implementation of hook_page_build().
+ */
+function admin_menu_masquerade_page_build(&$page) {
+  if (!isset($page['page_bottom']['admin_menu'])) {
+    return;
+  }
+  $path = drupal_get_path('module', 'admin_menu_masquerade');
+  $attached = &$page['page_bottom']['admin_menu']['#attached'];
+  $options = array('every_page' => TRUE);
+
+  $attached['css'][$path . '/admin_menu_masquerade.css'] = $options;
+}
+
+/**
+ * Implements hook_admin_menu_output_alter().
+ */
+function admin_menu_masquerade_admin_menu_output_alter(&$content) {
+
+  // Unset logout link, we always want to be logged in because we can masquerade
+  // as an anonymous user if we want to. That way we keep our environmental
+  // admin menu
+  unset($content['user']['logout']);
+
+  if (!isset($_SESSION['masquerading'])) {
+    // Get the masquerading block
+    $masquerade_form = drupal_get_form('masquerade_block_1');
+    // Make the form autosumbit
+    $masquerade_form['masquerade_user_field']['#attributes']['class'][] = 'auto_submit';
+    $masquerade_form['#attributes']['class'][] = 'admin-menu-action admin-menu-masquerade-form';
+    // Set a placeholder
+    $masquerade_form['masquerade_user_field']['#attributes']['placeholder'] = 'Masquerade as';
+    // Remove the description, switch back link is in that also
+    unset($masquerade_form['masquerade_desc']);
+    // Add it to our admin menu content
+    $content[] = $masquerade_form;
+  }
+  else {
+    global $user;
+    $content['user']['account']['#title'] = t('Masquerading as <strong>!username</strong>', array('!username' => $user->name));
+    $content['user']['masquerade'] = array(
+      '#title' => t('Switch back'),
+      '#href' => 'masquerade/unswitch',
+      '#weight' => -100,
+      '#options' => array(
+        'query' => array('token' => drupal_get_token('masquerade/unswitch')),
+      ),
+      '#attributes' => array(
+        'class' => array('admin-menu-action', 'admin-menu-masquerade'),
+      ),
+    );
+    // Also dont need search cause we dont have buttons
+    if ((count($content)) < 9) {
+      unset($content['search']);
+    }
+  }
+}

