From adac3d96f9801cd0be9c131592e57c21153e5234 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Tue, 11 Sep 2012 14:23:33 -0400
Subject: [PATCH] Created support method for loading user table (preparation
 for service locator).

---
 .../VuFind/src/VuFind/Auth/AbstractBase.php   | 39 ++++++++++++++++++-
 module/VuFind/src/VuFind/Auth/Database.php    |  7 ++--
 module/VuFind/src/VuFind/Auth/ILS.php         |  5 +--
 module/VuFind/src/VuFind/Auth/LDAP.php        |  5 +--
 module/VuFind/src/VuFind/Auth/SIP2.php        |  5 +--
 module/VuFind/src/VuFind/Auth/Shibboleth.php  |  5 +--
 6 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/module/VuFind/src/VuFind/Auth/AbstractBase.php b/module/VuFind/src/VuFind/Auth/AbstractBase.php
index aeeda812636..904f72b88e6 100644
--- a/module/VuFind/src/VuFind/Auth/AbstractBase.php
+++ b/module/VuFind/src/VuFind/Auth/AbstractBase.php
@@ -28,7 +28,9 @@
  */
 namespace VuFind\Auth;
 use VuFind\Config\Reader as ConfigReader,
-    VuFind\Exception\Auth as AuthException;
+    VuFind\Exception\Auth as AuthException,
+    Zend\ServiceManager\ServiceLocatorAwareInterface,
+    Zend\ServiceManager\ServiceLocatorInterface;
 
 /**
  * Abstract authentication base class
@@ -40,7 +42,7 @@ use VuFind\Config\Reader as ConfigReader,
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://www.vufind.org  Main Page
  */
-abstract class AbstractBase
+abstract class AbstractBase implements ServiceLocatorAwareInterface
 {
     protected $configValidated = false;
     protected $config = null;
@@ -169,4 +171,37 @@ abstract class AbstractBase
         // By default, account creation is not supported.
         return false;
     }
+
+    /**
+     * Get access to the user table.
+     *
+     * @return \VuFind\Db\Table\User
+     */
+    public function getUserTable()
+    {
+        return new \VuFind\Db\Table\User();
+    }
+
+    /**
+     * Set the service locator.
+     *
+     * @param ServiceLocatorInterface $serviceLocator Locator to register
+     *
+     * @return AbstractBase
+     */
+    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
+    {
+        $this->serviceLocator = $serviceLocator;
+        return $this;
+    }
+
+    /**
+     * Get the service locator.
+     *
+     * @return \Zend\ServiceManager\ServiceLocatorInterface
+     */
+    public function getServiceLocator()
+    {
+        return $this->serviceLocator;
+    }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Auth/Database.php b/module/VuFind/src/VuFind/Auth/Database.php
index c1f4b57fc18..14293e7618e 100644
--- a/module/VuFind/src/VuFind/Auth/Database.php
+++ b/module/VuFind/src/VuFind/Auth/Database.php
@@ -28,7 +28,7 @@
  * @link     http://vufind.org/wiki/building_an_authentication_handler Wiki
  */
 namespace VuFind\Auth;
-use VuFind\Db\Table\User as UserTable, VuFind\Exception\Auth as AuthException;
+use VuFind\Exception\Auth as AuthException;
 
 /**
  * Database authentication class
@@ -65,8 +65,7 @@ class Database extends AbstractBase
         }
 
         // Validate the credentials:
-        $table = new UserTable();
-        $user = $table->getByUsername($this->username, false);
+        $user = $this->getUserTable()->getByUsername($this->username, false);
         if (!is_object($user) || !$user->checkPassword($this->password)) {
             throw new AuthException('authentication_error_invalid');
         }
@@ -116,7 +115,7 @@ class Database extends AbstractBase
         }
 
         // Make sure we have a unique username
-        $table = new UserTable();
+        $table = $this->getUserTable();
         if ($table->getByUsername($params['username'], false)) {
             throw new AuthException('That username is already taken');
         }
diff --git a/module/VuFind/src/VuFind/Auth/ILS.php b/module/VuFind/src/VuFind/Auth/ILS.php
index 68b3e98aa0b..4df3e73d00a 100644
--- a/module/VuFind/src/VuFind/Auth/ILS.php
+++ b/module/VuFind/src/VuFind/Auth/ILS.php
@@ -28,7 +28,7 @@
  */
 namespace VuFind\Auth;
 use VuFind\Connection\Manager as ConnectionManager,
-    VuFind\Db\Table\User as UserTable, VuFind\Exception\Auth as AuthException;
+    VuFind\Exception\Auth as AuthException;
 
 /**
  * ILS authentication module.
@@ -123,8 +123,7 @@ class ILS extends AbstractBase
         }
 
         // Check to see if we already have an account for this user:
-        $table = new UserTable();
-        $user = $table->getByUsername($info[$usernameField]);
+        $user = $this->getUserTable()->getByUsername($info[$usernameField]);
 
         // No need to store the ILS password in VuFind's main password field:
         $user->password = "";
diff --git a/module/VuFind/src/VuFind/Auth/LDAP.php b/module/VuFind/src/VuFind/Auth/LDAP.php
index b730af25dfa..33a9b729e68 100644
--- a/module/VuFind/src/VuFind/Auth/LDAP.php
+++ b/module/VuFind/src/VuFind/Auth/LDAP.php
@@ -27,7 +27,7 @@
  * @link     http://vufind.org/wiki/building_an_authentication_handler Wiki
  */
 namespace VuFind\Auth;
-use VuFind\Db\Table\User as UserTable, VuFind\Exception\Auth as AuthException;
+use VuFind\Exception\Auth as AuthException;
 
 /**
  * LDAP authentication class
@@ -194,8 +194,7 @@ class LDAP extends AbstractBase
         );
 
         // User object to populate from LDAP:
-        $table = new UserTable();
-        $user = $table->getByUsername($this->username);
+        $user = $this->getUserTable()->getByUsername($this->username);
 
         // Loop through LDAP response and map fields to database object based
         // on configuration settings:
diff --git a/module/VuFind/src/VuFind/Auth/SIP2.php b/module/VuFind/src/VuFind/Auth/SIP2.php
index 9f520c4960c..36a761c23c1 100644
--- a/module/VuFind/src/VuFind/Auth/SIP2.php
+++ b/module/VuFind/src/VuFind/Auth/SIP2.php
@@ -27,7 +27,7 @@
  * @link     http://vufind.org/wiki/building_an_authentication_handler Wiki
  */
 namespace VuFind\Auth;
-use VuFind\Db\Table\User as UserTable, VuFind\Exception\Auth as AuthException;
+use VuFind\Exception\Auth as AuthException;
 
 /**
  * SIP2 authentication module.
@@ -128,8 +128,7 @@ class SIP2 extends AbstractBase
      */
     protected function processSIP2User($info, $username, $password)
     {
-        $table = new UserTable();
-        $user = $table->getByUsername($info['variable']['AA'][0]);
+        $user = $this->getUserTable()->getByUsername($info['variable']['AA'][0]);
 
         // This could potentially be different depending on the ILS.  Name could be
         // Bob Wicksall or Wicksall, Bob. This is currently assuming Wicksall, Bob
diff --git a/module/VuFind/src/VuFind/Auth/Shibboleth.php b/module/VuFind/src/VuFind/Auth/Shibboleth.php
index aee35e1e4ba..4df21a65176 100644
--- a/module/VuFind/src/VuFind/Auth/Shibboleth.php
+++ b/module/VuFind/src/VuFind/Auth/Shibboleth.php
@@ -27,7 +27,7 @@
  * @link     http://www.vufind.org  Main Page
  */
 namespace VuFind\Auth;
-use VuFind\Db\Table\User as UserTable, VuFind\Exception\Auth as AuthException;
+use VuFind\Exception\Auth as AuthException;
 
 /**
  * Shibboleth authentication module.
@@ -92,8 +92,7 @@ class Shibboleth extends AbstractBase
         }
 
         // If we made it this far, we should log in the user!
-        $table = new UserTable();
-        $user = $table->getByUsername($username);
+        $user = $this->getUserTable()->getByUsername($username);
 
         // Has the user configured attributes to use for populating the user table?
         $attribsToCheck = array(
-- 
GitLab