From 23d376be072ff7ea765a978cfb11596d85b3c71f Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 10 Oct 2012 15:00:20 -0400
Subject: [PATCH] Refactored Auth plugins to avoid dependence on generic
 ServiceLocatorAwareInterface; injections are now more targeted for clearer
 dependencies.  Also added some missing comments.

---
 .../VuFind/src/VuFind/Auth/AbstractBase.php   | 51 +++++++++-------
 module/VuFind/src/VuFind/Auth/MultiAuth.php   | 58 ++++++++++++++++++-
 .../VuFind/Db/Table/DbTableAwareInterface.php | 57 ++++++++++++++++++
 .../ServiceManager/AbstractPluginManager.php  | 24 ++++++++
 .../tests/unit-tests/src/Auth/LDAPTest.php    |  2 +-
 .../unit-tests/src/Auth/MultiAuthTest.php     |  3 +-
 .../tests/unit-tests/src/Auth/SIP2Test.php    |  2 +-
 7 files changed, 172 insertions(+), 25 deletions(-)
 create mode 100644 module/VuFind/src/VuFind/Db/Table/DbTableAwareInterface.php

diff --git a/module/VuFind/src/VuFind/Auth/AbstractBase.php b/module/VuFind/src/VuFind/Auth/AbstractBase.php
index 4d9f9328350..5cb14527c25 100644
--- a/module/VuFind/src/VuFind/Auth/AbstractBase.php
+++ b/module/VuFind/src/VuFind/Auth/AbstractBase.php
@@ -28,9 +28,7 @@
  */
 namespace VuFind\Auth;
 use VuFind\Config\Reader as ConfigReader,
-    VuFind\Exception\Auth as AuthException,
-    Zend\ServiceManager\ServiceLocatorAwareInterface,
-    Zend\ServiceManager\ServiceLocatorInterface;
+    VuFind\Exception\Auth as AuthException;
 
 /**
  * Abstract authentication base class
@@ -42,17 +40,28 @@ 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 implements ServiceLocatorAwareInterface
+abstract class AbstractBase implements \VuFind\Db\Table\DbTableAwareInterface
 {
+    /**
+     * Has the configuration been validated?
+     *
+     * @param bool
+     */
     protected $configValidated = false;
+
+    /**
+     * Configuration settings
+     *
+     * @param \Zend\Config\Config
+     */
     protected $config = null;
 
     /**
-     * Service locator
+     * Database table plugin manager
      *
-     * @var ServiceLocatorInterface
+     * @var \VuFind\Db\Table\PluginManager
      */
-    protected $serviceLocator;
+    protected $tableManager;
 
     /**
      * Get configuration (load automatically if not previously set).  Throw an
@@ -186,30 +195,32 @@ abstract class AbstractBase implements ServiceLocatorAwareInterface
      */
     public function getUserTable()
     {
-        return $this->getServiceLocator()->getServiceLocator()
-            ->get('DbTablePluginManager')->get('User');
+        return $this->getDbTableManager()->get('User');
     }
 
     /**
-     * Set the service locator.
-     *
-     * @param ServiceLocatorInterface $serviceLocator Locator to register
+     * Get the table plugin manager.  Throw an exception if it is missing.
      *
-     * @return AbstractBase
+     * @throws \Exception
+     * @return \VuFind\Db\Table\PluginManager\PluginManager
      */
-    public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
+    public function getDbTableManager()
     {
-        $this->serviceLocator = $serviceLocator;
-        return $this;
+        if (null === $this->tableManager) {
+            throw new \Exception('DB table manager missing.');
+        }
+        return $this->tableManager;
     }
 
     /**
-     * Get the service locator.
+     * Set the table plugin manager.
      *
-     * @return \Zend\ServiceManager\ServiceLocatorInterface
+     * @param \VuFind\Db\Table\PluginManagerPluginManager $manager Plugin manager
+     *
+     * @return void
      */
-    public function getServiceLocator()
+    public function setDbTableManager(\VuFind\Db\Table\PluginManager $manager)
     {
-        return $this->serviceLocator;
+        $this->tableManager = $manager;
     }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Auth/MultiAuth.php b/module/VuFind/src/VuFind/Auth/MultiAuth.php
index 5c84393b148..dcc1a8cf5fa 100644
--- a/module/VuFind/src/VuFind/Auth/MultiAuth.php
+++ b/module/VuFind/src/VuFind/Auth/MultiAuth.php
@@ -63,11 +63,41 @@ use VuFind\Exception\Auth as AuthException;
  */
 class MultiAuth extends AbstractBase
 {
+    /**
+     * Filter configuration for credentials
+     *
+     * @var array
+     */
     protected $filters = array();
+
+    /**
+     * Authentication methods to try
+     *
+     * @var array
+     */
     protected $methods = array();
+
+    /**
+     * Username input
+     *
+     * @var string
+     */
     protected $username;
+
+    /**
+     * Password input
+     *
+     * @var string
+     */
     protected $password;
 
+    /**
+     * Plugin manager for obtaining other authentication objects
+     *
+     * @var PluginManager
+     */
+    protected $manager;
+
     /**
      * Validate configuration parameters.  This is a support method for getConfig(),
      * so the configuration MUST be accessed using $this->config; do not call
@@ -172,7 +202,7 @@ class MultiAuth extends AbstractBase
      */
     protected function authUser($request)
     {
-        $manager = $this->getServiceLocator();
+        $manager = $this->getPluginManager();
 
         // Try authentication methods until we find one that works:
         foreach ($this->methods as $method) {
@@ -203,4 +233,30 @@ class MultiAuth extends AbstractBase
         }
         return $user;
     }
+
+    /**
+     * Set the manager for loading other authentication plugins.
+     *
+     * @param PluginManager $manager Plugin manager
+     *
+     * @return void
+     */
+    public function setPluginManager(PluginManager $manager)
+    {
+        $this->manager = $manager;
+    }
+
+    /**
+     * Get the manager for loading other authentication plugins.
+     *
+     * @throws \Exception
+     * @return PluginManager
+     */
+    public function getPluginManager()
+    {
+        if (null === $this->manager) {
+            throw new \Exception('Plugin manager missing.');
+        }
+        return $this->manager;
+    }
 }
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/Db/Table/DbTableAwareInterface.php b/module/VuFind/src/VuFind/Db/Table/DbTableAwareInterface.php
new file mode 100644
index 00000000000..32a60bbbefe
--- /dev/null
+++ b/module/VuFind/src/VuFind/Db/Table/DbTableAwareInterface.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Marker interface for classes that depend on the \VuFind\Db\Table\PluginManager
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Db_Table
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+namespace VuFind\Db\Table;
+
+/**
+ * Marker interface for classes that depend on the \VuFind\Db\Table\PluginManager
+ *
+ * @category VuFind2
+ * @package  Db_Table
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org   Main Site
+ */
+interface DbTableAwareInterface
+{
+    /**
+     * Get the plugin manager.  Throw an exception if it is missing.
+     *
+     * @throws \Exception
+     * @return PluginManager
+     */
+    public function getDbTableManager();
+
+    /**
+     * Set the plugin manager.
+     *
+     * @param PluginManager $manager Plugin manager
+     *
+     * @return void
+     */
+    public function setDbTableManager(PluginManager $manager);
+}
\ No newline at end of file
diff --git a/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php b/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
index d7abc47fcd0..7e71e9f88ad 100644
--- a/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
+++ b/module/VuFind/src/VuFind/ServiceManager/AbstractPluginManager.php
@@ -40,6 +40,30 @@ use Zend\ServiceManager\AbstractPluginManager as Base,
  */
 abstract class AbstractPluginManager extends Base
 {
+    /**
+     * Constructor
+     *
+     * Make sure table gateways are properly initialized.
+     *
+     * @param null|ConfigInterface $configuration Configuration settings (optional)
+     */
+    public function __construct(
+        \Zend\ServiceManager\ConfigInterface $configuration = null
+    ) {
+        parent::__construct($configuration);
+        $initializer = function ($instance, $manager) {
+            if ($instance instanceof \VuFind\Db\Table\DbTableAwareInterface) {
+                $instance->setDbTableManager(
+                    $manager->getServiceLocator()->get('DbTablePluginManager')
+                );
+            }
+            if (method_exists($instance, 'setPluginManager')) {
+                $instance->setPluginManager($manager);
+            }
+        };
+        $this->addInitializer($initializer, false);
+    }
+
     /**
      * Validate the plugin
      *
diff --git a/module/VuFind/tests/unit-tests/src/Auth/LDAPTest.php b/module/VuFind/tests/unit-tests/src/Auth/LDAPTest.php
index c95ed5efcee..0e8f30d1893 100644
--- a/module/VuFind/tests/unit-tests/src/Auth/LDAPTest.php
+++ b/module/VuFind/tests/unit-tests/src/Auth/LDAPTest.php
@@ -37,7 +37,7 @@ use VuFind\Auth\LDAP, Zend\Config\Config;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://www.vufind.org  Main Page
  */
-class LDAPTest extends \VuFindTest\Unit\TestCase
+class LDAPTest extends \VuFindTest\Unit\DbTestCase
 {
     /**
      * Get an authentication object.
diff --git a/module/VuFind/tests/unit-tests/src/Auth/MultiAuthTest.php b/module/VuFind/tests/unit-tests/src/Auth/MultiAuthTest.php
index 1f65861ffef..4cc23b562c3 100644
--- a/module/VuFind/tests/unit-tests/src/Auth/MultiAuthTest.php
+++ b/module/VuFind/tests/unit-tests/src/Auth/MultiAuthTest.php
@@ -37,7 +37,7 @@ use VuFind\Auth\MultiAuth, Zend\Config\Config;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://www.vufind.org  Main Page
  */
-class MultiAuthTest extends \VuFindTest\Unit\TestCase
+class MultiAuthTest extends \VuFindTest\Unit\DbTestCase
 {
     /**
      * Get an authentication object.
@@ -60,7 +60,6 @@ class MultiAuthTest extends \VuFindTest\Unit\TestCase
         );
         $obj = clone($this->getAuthManager()->get('MultiAuth'));
         $obj->setConfig($config);
-        $obj->setServiceLocator($serviceLocator);
         return $obj;
     }
 
diff --git a/module/VuFind/tests/unit-tests/src/Auth/SIP2Test.php b/module/VuFind/tests/unit-tests/src/Auth/SIP2Test.php
index 94b99b45bc1..9cfa93ab693 100644
--- a/module/VuFind/tests/unit-tests/src/Auth/SIP2Test.php
+++ b/module/VuFind/tests/unit-tests/src/Auth/SIP2Test.php
@@ -37,7 +37,7 @@ use VuFind\Auth\SIP2, Zend\Config\Config;
  * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
  * @link     http://www.vufind.org  Main Page
  */
-class SIP2Test extends \VuFindTest\Unit\TestCase
+class SIP2Test extends \VuFindTest\Unit\DbTestCase
 {
     /**
      * Get an authentication object.
-- 
GitLab