=== added directory 'lib/eventum/auth'
=== added file 'lib/eventum/auth/class.abstract_auth_backend.php'
--- lib/eventum/auth/class.abstract_auth_backend.php	1970-01-01 00:00:00 +0000
+++ lib/eventum/auth/class.abstract_auth_backend.php	2009-09-29 17:48:29 +0000
@@ -0,0 +1,52 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
+// +----------------------------------------------------------------------+
+// | Eventum - Issue Tracking System                                      |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2009 Sun Microsystem Inc.                              |
+// |                                                                      |
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation; either version 2 of the License, or    |
+// | (at your option) any later version.                                  |
+// |                                                                      |
+// | 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:                           |
+// |                                                                      |
+// | Free Software Foundation, Inc.                                       |
+// | 59 Temple Place - Suite 330                                          |
+// | Boston, MA 02111-1307, USA.                                          |
+// +----------------------------------------------------------------------+
+// | Authors: Elan Ruusamäe <glen@delfi.ee>                               |
+// +----------------------------------------------------------------------+
+//
+
+/**
+ * Abstract class that all auth backends should extend. This is so any new
+ * auth methods added in future releases won't break existing backends.
+ *
+ * @author Elan Ruusamäe <glen@delfi.ee>
+ */
+class Abstract_Auth_Backend
+{
+
+    /**
+     * Checks whether the provided password match against the email
+     * address provided.
+     *
+     * @see     class.auth.php
+     * @access  public
+     * @param   string $email The email address to check for
+     * @param   string $password The password of the user to check for
+     * @return  boolean
+     */
+    function isCorrectPassword($email, $password)
+    {
+        return false;
+    }
+}

=== added file 'lib/eventum/auth/class.default.php'
--- lib/eventum/auth/class.default.php	1970-01-01 00:00:00 +0000
+++ lib/eventum/auth/class.default.php	2009-09-29 17:47:19 +0000
@@ -0,0 +1,74 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
+// +----------------------------------------------------------------------+
+// | Eventum - Issue Tracking System                                      |
+// +----------------------------------------------------------------------+
+// | Copyright (c) 2009 Sun Microsystem Inc.                              |
+// |                                                                      |
+// | This program is free software; you can redistribute it and/or modify |
+// | it under the terms of the GNU General Public License as published by |
+// | the Free Software Foundation; either version 2 of the License, or    |
+// | (at your option) any later version.                                  |
+// |                                                                      |
+// | 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:                           |
+// |                                                                      |
+// | Free Software Foundation, Inc.                                       |
+// | 59 Temple Place - Suite 330                                          |
+// | Boston, MA 02111-1307, USA.                                          |
+// +----------------------------------------------------------------------+
+// | Authors: Elan Ruusamäe <glen@delfi.ee>                               |
+// +----------------------------------------------------------------------+
+//
+
+
+/**
+ * Default auth backend. This is the original backend used by Eventum.
+ *
+ * @author Elan Ruusamäe <glen@delfi.ee>
+ */
+class Default_Auth_Backend extends Abstract_Auth_Backend
+{
+
+    /**
+     * Hashes the password according to APP_HASH_TYPE constant
+     *
+     * @access  public
+     * @param   string $password The plain text password
+     * @return  string The hashed password
+     */
+    private static function hashPassword($password)
+    {
+        if (APP_HASH_TYPE == 'MD5-64') {
+            return base64_encode(pack('H*',md5($password)));
+        } else {
+            // default to md5
+            return md5($password);
+        }
+    }
+
+    /**
+     * Checks whether the provided password match against the email
+     * address provided.
+     *
+     * @access  public
+     * @param   string $email The email address to check for
+     * @param   string $password The password of the user to check for
+     * @return  boolean
+     */
+    public static function isCorrectPassword($email, $password)
+    {
+        $usr_id = User::getUserIDByEmail($email, true);
+        $user = User::getDetails($usr_id);
+        if ($user['usr_password'] != self::hashPassword($password)) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+}

=== modified file 'lib/eventum/class.auth.php'
--- lib/eventum/class.auth.php	2009-07-28 15:08:00 +0000
+++ lib/eventum/class.auth.php	2009-09-29 17:58:15 +0000
@@ -39,6 +39,114 @@
 class Auth
 {
     /**
+     * Returns the list of available auth backends by listing the class
+     * files in the backend directory.
+     *
+     * @access  public
+     * @return  array Associative array of filename => name
+     */
+    public static function getBackendList()
+    {
+        $files = Misc::getFileList(APP_INC_PATH . '/auth');
+        $list = array();
+        for ($i = 0; $i < count($files); $i++) {
+            // make sure we only list the customer backends
+            if (preg_match('/^class\./', $files[$i])) {
+                // display a prettyfied backend name in the admin section
+                preg_match('/class\.(.*)\.php/', $files[$i], $matches);
+                if ($matches[1] == "abstract_auth_backend") {
+                    continue;
+                }
+                $name = ucwords(str_replace('_', ' ', $matches[1]));
+                $list[$files[$i]] = $name;
+            }
+        }
+        return $list;
+    }
+
+    /**
+     * Returns the auth backend class file associated with the given
+     * project ID.
+     *
+     * @access  public
+     * @param   integer $prj_id The project ID
+     * @return  string The auth backend class filename
+     */
+    private static function _getBackendNameByProject($prj_id)
+    {
+        static $backends;
+
+        if (isset($backends[$prj_id])) {
+            return $backends[$prj_id];
+        }
+
+        $stmt = "SELECT
+                    prj_id,
+                    prj_auth_backend
+                 FROM
+                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project
+                 ORDER BY
+                    prj_id";
+        $res = DB_Helper::getInstance()->getAssoc($stmt);
+        if (PEAR::isError($res)) {
+            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
+            return '';
+        } else {
+            $backends = $res;
+            return @$backends[$prj_id];
+        }
+    }
+
+
+    /**
+     * Includes the appropriate auth backend class associated with the
+     * given project ID, instantiates it and returns the class.
+     *
+     * @access  private
+     * @param   integer $prj_id The project ID
+     * @return  boolean
+     */
+    private static function &_getBackend($prj_id)
+    {
+        static $setup_backends;
+
+        if (empty($setup_backends[$prj_id])) {
+            $backend_class = self::_getBackendNameByProject($prj_id);
+            if (empty($backend_class)) {
+                $setup_backends[$prj_id] = false;
+            } else {
+                $file_name_chunks = explode(".", $backend_class);
+                $class_name = $file_name_chunks[1] . "_Auth_Backend";
+
+                require_once APP_INC_PATH . "/auth/$backend_class";
+
+                $setup_backends[$prj_id] = new $class_name;
+            }
+        }
+        return $setup_backends[$prj_id];
+    }
+
+
+    /**
+     * Checks whether the given project ID is setup to use customer integration
+     * or not.
+     *
+     * @access  public
+     * @param   integer $prj_id The project ID
+     * @return  boolean
+     */
+    public static function hasAuthIntegration($prj_id)
+    {
+        $backend = self::_getBackendNameByProject($prj_id);
+        if (empty($backend)) {
+            return false;
+        } else {
+            return true;
+        }
+    }
+
+
+    /**
      * Method used to get private key used for hashing session cookies.
      *
      * @access  public
@@ -409,23 +517,21 @@
      * Checks whether the provided password match against the email
      * address provided.
      *
+     * @see     class.auth.php
      * @access  public
      * @param   string $email The email address to check for
      * @param   string $password The password of the user to check for
      * @return  boolean
      */
-    function isCorrectPassword($email, $password)
+    function isCorrectPassword($prj_id, $email, $password)
     {
-        $usr_id = User::getUserIDByEmail($email, true);
-        $user = User::getDetails($usr_id);
-        if ($user['usr_password'] != self::hashPassword($password)) {
+        if (!self::hasAuthIntegration($prj_id)) {
             return false;
-        } else {
-            return true;
         }
+        $backend =& self::_getBackend($prj_id);
+        return $backend->isCorrectPassword($prj_id, $email, $password);
     }
 
-
     /**
      * Gets the current user ID.
      *
@@ -546,25 +652,6 @@
         $_COOKIE[APP_PROJECT_COOKIE] = base64_encode(serialize($cookie));
     }
 
-
-    /**
-     * Hashes the password according to APP_HASH_TYPE constant
-     *
-     * @access  public
-     * @param   string $password The plain text password
-     * @return  string The hashed password
-     */
-    function hashPassword($password)
-    {
-        if (APP_HASH_TYPE == 'MD5-64') {
-            return base64_encode(pack('H*',md5($password)));
-        } else {
-            // default to md5
-            return md5($password);
-        }
-    }
-
-
     /**
      * Sets a cookie in the browser
      *
@@ -581,5 +668,4 @@
             setcookie($name, $value, $expiration, APP_COOKIE_URL, APP_COOKIE_DOMAIN);
         }
     }
-
 }

