From 680d96d068c0fa7dcdcdf06f56256539e5886b09 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 17 Jan 2014 15:18:12 +0000
Subject: [PATCH] Provide facility to get the auth backend for the
 authenticated user Reafactor logOut to use the piggyback on auth backend

---
 include/class.auth.php   | 26 +++++++++++++++++++-------
 include/class.client.php |  5 +++--
 include/class.staff.php  |  5 +++--
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/include/class.auth.php b/include/class.auth.php
index 8746c5b23..9913c43e8 100644
--- a/include/class.auth.php
+++ b/include/class.auth.php
@@ -11,8 +11,11 @@ abstract class AuthenticatedUser {
     abstract function getId();
     abstract function getUsername();
     abstract function getRole();
-    abstract function logOut();
 
+    //Backend used to authenticate the user
+    abstract function getAuthBackend();
+
+    //Authentication key
     function setAuthKey($key) {
         $this->authkey = $key;
     }
@@ -20,6 +23,15 @@ abstract class AuthenticatedUser {
     function getAuthKey() {
         return $this->authkey;
     }
+
+    // logOut the user
+    function logOut() {
+
+        if ($bk = $this->getAuthBackend())
+            return $bk->signOut($this);
+
+        return false;
+    }
 }
 
 interface AuthDirectorySearch {
@@ -293,12 +305,15 @@ abstract class StaffAuthenticationBackend  extends AuthenticationBackend {
         return true;
     }
 
+    /* Base signOut
+     *
+     * Backend should extend the signout and perform any additional signout
+     * it requires.
+     */
+
     static function signOut($staff) {
         global $ost;
 
-        list($id, $auth) = explode(':', $_SESSION['_auth']['staff']['key']);
-        //TODO: Lookup the backed and request logout..
-
         $_SESSION['_auth']['staff'] = array();
         $ost->logDebug('Staff logout',
                 sprintf("%s logged out [%s]",
@@ -395,9 +410,6 @@ abstract class UserAuthenticationBackend  extends AuthenticationBackend {
     static function signOut($user) {
         global $ost;
 
-        list($id, $auth) = explode(':', $_SESSION['_auth']['user']['key']);
-        //TODO: Lookup the backed and request logout..
-
         $_SESSION['_auth']['user'] = array();
         $ost->logDebug('User logout',
                 sprintf("%s logged out [%s]",
diff --git a/include/class.client.php b/include/class.client.php
index 0fa312b5e..93aa1b07f 100644
--- a/include/class.client.php
+++ b/include/class.client.php
@@ -179,8 +179,9 @@ class  EndUser extends AuthenticatedUser {
         return $this->isOwner() ? 'owner' : 'collaborator';
     }
 
-    function logOut() {
-        return UserAuthenticationBackend::signOut($this);
+    function getAuthBackend() {
+        list($authkey,) = explode(':', $this->getAuthKey());
+        return UserAuthenticationBackend::getBackend($authkey);
     }
 
     function getTicketStats() {
diff --git a/include/class.staff.php b/include/class.staff.php
index cf6238fcc..e353c5cd9 100644
--- a/include/class.staff.php
+++ b/include/class.staff.php
@@ -105,8 +105,9 @@ class Staff extends AuthenticatedUser {
         return 'staff';
     }
 
-    function logOut() {
-        return StaffAuthenticationBackend::signOut($this);
+    function getAuthBackend() {
+        list($authkey, ) = explode(':', $this->getAuthKey());
+        return StaffAuthenticationBackend::getBackend($authkey);
     }
 
     /*compares user password*/
-- 
GitLab