From 5c18bc7d91abc8cf8799b50be40b2c8faf05dfc0 Mon Sep 17 00:00:00 2001
From: Agnar Odegard <agnar@norweb.no>
Date: Mon, 24 Oct 2016 12:22:48 +0200
Subject: [PATCH] Issue #2124117 by odegard: Make simplesamlphp_auth compatible
 with masquerade.

---
 simplesamlphp_auth.module | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/simplesamlphp_auth.module b/simplesamlphp_auth.module
index 2e8c8a5..d0295d8 100644
--- a/simplesamlphp_auth.module
+++ b/simplesamlphp_auth.module
@@ -230,6 +230,24 @@ function simplesamlphp_auth_user_insert(&$edit, $account, $category = NULL) {
  * Implements hook_user_logout().
  */
 function simplesamlphp_auth_user_logout($account) {
+
+  // Stop full logout when masquerading.
+  if (module_exists('masquerade')) {
+
+    // The masquerade module invokes logout both when switching to and from
+    // a user. These are the cases we need to stop simplesaml from a full
+    // logout.
+    $backtrace = debug_backtrace(TRUE);
+    foreach ($backtrace as $trace_name => $trace_data) {
+      if ($trace_data['function'] == 'masquerade_switch_user') {
+        return;
+      }
+      if ($trace_data['function'] == 'masquerade_switch_back') {
+        return;
+      }
+    }
+  }
+
   global $user;
   global $_simplesamlphp_auth_as;
   global $_simplesamlphp_auth_saml_attributes;
-- 
1.9.1

