Horde::callHook() now throws a Horde_Exception
authorMichael M Slusarz <slusarz@curecanti.org>
Mon, 13 Jul 2009 17:03:50 +0000 (11:03 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Mon, 13 Jul 2009 19:41:47 +0000 (13:41 -0600)
framework/Auth/lib/Horde/Auth.php
framework/Auth/lib/Horde/Auth/Driver.php
framework/Auth/lib/Horde/Auth/Ldap.php
framework/Auth/lib/Horde/Auth/Msad.php
framework/Auth/lib/Horde/Auth/Signup.php
framework/Core/lib/Horde.php
framework/Core/lib/Horde/Registry.php

index b1dfea2..976ab29 100644 (file)
@@ -511,12 +511,15 @@ class Horde_Auth
     {
         $userId = self::addHook(trim($userId));
 
-        if (!empty($GLOBALS['conf']['hooks']['postauthenticate']) &&
-            !Horde::callHook('_horde_hook_postauthenticate', array($userId, $credentials, $realm), 'horde', false)) {
-            if (self::getAuthError() != self::REASON_MESSAGE) {
-                self::setAuthError(self::REASON_FAILED);
-            }
-            return false;
+        if (!empty($GLOBALS['conf']['hooks']['postauthenticate'])) {
+            try {
+                if (!Horde::callHook('_horde_hook_postauthenticate', array($userId, $credentials, $realm), 'horde')) {
+                    if (self::getAuthError() != self::REASON_MESSAGE) {
+                        self::setAuthError(self::REASON_FAILED);
+                    }
+                    return false;
+                }
+            } catch (Horde_Exception $e) {}
         }
 
         /* If we're already set with this userId, don't continue. */
@@ -685,17 +688,13 @@ class Horde_Auth
      * @param string $userId  The authentication backend's user name.
      *
      * @return string  The internal Horde user name.
+     * @throws Horde_Exception
      */
     static public function addHook($userId)
     {
-        if (!empty($GLOBALS['conf']['hooks']['username'])) {
-            $newId = Horde::callHook('_username_hook_frombackend', array($userId));
-            if (!is_a($newId, 'PEAR_Error')) {
-                return $newId;
-            }
-        }
-
-        return $userId;
+        return empty($GLOBALS['conf']['hooks']['username'])
+            ? $userId
+            : Horde::callHook('_username_hook_frombackend', array($userId));
     }
 
     /**
@@ -710,17 +709,13 @@ class Horde_Auth
      * @param string $userId  The internal Horde user name.
      *
      * @return string  The authentication backend's user name.
+     * @throws Horde_Exception
      */
     static public function removeHook($userId)
     {
-        if (!empty($GLOBALS['conf']['hooks']['username'])) {
-            $newId = Horde::callHook('_username_hook_tobackend', array($userId));
-            if (!is_a($newId, 'PEAR_Error')) {
-                return $newId;
-            }
-        }
-
-        return $userId;
+        return empty($GLOBALS['conf']['hooks']['username'])
+            ? $userId
+            : Horde::callHook('_username_hook_tobackend', array($userId));
     }
 
     /**
index 0fa3edc..13f67f3 100644 (file)
@@ -74,12 +74,14 @@ class Horde_Auth_Driver
         $userId = trim($userId);
 
         if (!empty($GLOBALS['conf']['hooks']['preauthenticate'])) {
-            if (!Horde::callHook('_horde_hook_preauthenticate', array($userId, $credentials, $realm), 'horde', false)) {
-                if (Horde_Auth::getAuthError() != Horde_Auth::REASON_MESSAGE) {
-                    Horde_Auth::setAuthError(Horde_Auth::REASON_FAILED);
+            try {
+                if (!Horde::callHook('_horde_hook_preauthenticate', array($userId, $credentials, $realm), 'horde')) {
+                    if (Horde_Auth::getAuthError() != Horde_Auth::REASON_MESSAGE) {
+                        Horde_Auth::setAuthError(Horde_Auth::REASON_FAILED);
+                    }
+                    return false;
                 }
-                return false;
-            }
+            } catch (Horde_Exception $e) {}
         }
 
         /* Store the credentials being checked so that subclasses can modify
index e2dd723..22b0e02 100644 (file)
@@ -325,9 +325,6 @@ class Horde_Auth_Ldap extends Horde_Auth_Driver
         global $conf;
         if (!empty($conf['hooks']['authldap'])) {
             $entry = Horde::callHook('_horde_hook_authldap', array($userId, $credentials));
-            if ($entry instanceof PEAR_Error) {
-                throw new Horde_Exception($entry);
-            }
             $dn = $entry['dn'];
             /* Remove the dn entry from the array. */
             unset($entry['dn']);
@@ -380,9 +377,6 @@ class Horde_Auth_Ldap extends Horde_Auth_Driver
 
         if (!empty($GLOBALS['conf']['hooks']['authldap'])) {
             $entry = Horde::callHook('_horde_hook_authldap', array($userId));
-            if ($entry instanceof PEAR_Error) {
-                throw new Horde_Exception($entry);
-            }
             $dn = $entry['dn'];
         } else {
             /* Search for the user's full DN. */
@@ -419,9 +413,6 @@ class Horde_Auth_Ldap extends Horde_Auth_Driver
 
         if (!empty($GLOBLS['conf']['hooks']['authldap'])) {
             $entry = Horde::callHook('_horde_hook_authldap', array($oldID, $credentials));
-            if ($entry instanceof PEAR_Error) {
-                throw new Horde_Exception($entry);
-            }
             $olddn = $entry['dn'];
             $entry = Horde::callHook('_horde_hook_authldap', array($newID, $credentials));
             $newdn = $entry['dn'];
index 5dc168d..93d94f9 100644 (file)
@@ -76,7 +76,10 @@ class Horde_Auth_Msad extends Horde_Auth_Ldap
         /* Connect to the MSAD server. */
         $this->_connect();
 
-        $entry = Horde::callHook('_horde_hook_authmsad', array($accountName, $credentials), 'horde', null);
+        try {
+            $entry = Horde::callHook('_horde_hook_authmsad', array($accountName, $credentials), 'horde');
+        } catch (Horde_Exception $e) {}
+
         if (!is_null($entry)) {
             $dn = $entry['dn'];
             unset($entry['dn']);
@@ -132,13 +135,14 @@ class Horde_Auth_Msad extends Horde_Auth_Ldap
         /* Connect to the MSAD server. */
         $this->_connect();
 
-        $entry = Horde::callHook('_horde_hook_authmsad', array($accountName), 'horde', null);
-        if (!is_null($entry)) {
-            $dn = $entry['dn'];
-        } else {
+        try {
+            $entry = Horde::callHook('_horde_hook_authmsad', array($accountName), 'horde');
+        } catch (Horde_Exception $e) {}
+
+        $dn = is_null($entry)
             /* Search for the user's full DN. */
-            $dn = $this->_findDN($accountName);
-        }
+            ? $this->_findDN($accountName)
+            : $entry['dn'];
 
         if (!@ldap_delete($this->_ds, $dn)) {
             throw new Horde_Exception(sprintf(_("Horde_Auth_Msad: Unable to remove user \"%s\""), $accountName));
@@ -163,7 +167,10 @@ class Horde_Auth_Msad extends Horde_Auth_Ldap
         /* Connect to the MSAD server. */
         $this->_connect();
 
-        $entry = Horde::callHook('_horde_hook_authmsad', array($oldId, $credentials), 'horde', null);
+        try {
+            $entry = Horde::callHook('_horde_hook_authmsad', array($oldId, $credentials), 'horde');
+        } catch (Horde_Exception $e) {}
+
         if (!is_null($entry)) {
             $olddn = $entry['dn'];
             unset($entry['dn']);
index 7656cd4..bcf3b05 100644 (file)
@@ -70,9 +70,6 @@ class Horde_Auth_Signup
         // Perform any preprocessing if requested.
         if ($conf['signup']['preprocess']) {
             $info = Horde::callHook('_horde_hook_signup_preprocess', array($info));
-            if (is_a($info, 'PEAR_Error')) {
-                return $info;
-            }
         }
 
         // Check to see if the username already exists.
@@ -88,14 +85,14 @@ class Horde_Auth_Signup
         }
 
         $result = true;
+
         // Attempt to add/update any extra data handed in.
         if (!empty($info['extra'])) {
-            $result = false;
-            $result = Horde::callHook('_horde_hook_signup_addextra',
-                                     array($info['user_name'], $info['extra']));
-            if (is_a($result, 'PEAR_Error')) {
-                Horde::logMessage($result, __FILE__, __LINE__, PEAR_LOG_EMERG);
-                return $result;
+            try {
+                return Horde::callHook('_horde_hook_signup_addextra', array($info['user_name'], $info['extra']));
+            } catch (Horde_Exception $e) {
+                Horde::logMessage($e, __FILE__, __LINE__, PEAR_LOG_EMERG);
+                return PEAR::raiseError($e->getMessage(), $e->getCode());
             }
         }
 
@@ -118,11 +115,9 @@ class Horde_Auth_Signup
 
         // Perform any preprocessing if requested.
         if ($conf['signup']['preprocess']) {
-            $info = Horde::callHook('_horde_hook_signup_preprocess',
-                                    array($info));
-            if (is_a($info, 'PEAR_Error')) {
-                return $info;
-            }
+            try {
+                $info = Horde::callHook('_horde_hook_signup_preprocess', array($info));
+            } catch (Horde_Exception $e) {}
         }
 
         // Check to see if the username already exists.
@@ -148,8 +143,9 @@ class Horde_Auth_Signup
         }
 
         if ($conf['signup']['queue']) {
-            $result = Horde::callHook('_horde_hook_signup_queued',
-                                      array($info['user_name'], $info));
+            try {
+                $result = Horde::callHook('_horde_hook_signup_queued', array($info['user_name'], $info));
+            } catch (Horde_Exception $e) {}
         }
 
         if (!empty($conf['signup']['email'])) {
@@ -258,8 +254,11 @@ class HordeSignupForm extends Horde_Form {
         $this->addHidden('', 'url', 'text', false);
 
         /* Use hooks get any extra fields required in signing up. */
-        $extra = Horde::callHook('_horde_hook_signup_getextra');
-        if (!is_a($extra, 'PEAR_Error') && !empty($extra)) {
+        try {
+            $extra = Horde::callHook('_horde_hook_signup_getextra');
+        } catch (Horde_Exception $e) {}
+
+        if (!empty($extra)) {
             if (!isset($extra['user_name'])) {
                 $this->addVariable(_("Choose a username"), 'user_name', 'text', true);
             }
index 1d1dca0..7b25e78 100644 (file)
@@ -1791,16 +1791,10 @@ HTML;
      *                      function.
      * @param string $app   If specified look for hooks in the config directory
      *                      of this app.
-     * @param mixed $error  What to return if $app/config/hooks.php or $hook
-     *                      does not exist. If this is the string 'PEAR_Error'
-     *                      a PEAR error object is returned instead, detailing
-     *                      the failure.
      *
-     * @return mixed  Either the results of the hook or PEAR error on failure.
-     * @todo Throw Horde_Exception
+     * @return mixed  The results of the hook.
      */
-    static public function callHook($hook, $args = array(), $app = 'horde',
-                                    $error = 'PEAR_Error')
+    static public function callHook($hook, $args = array(), $app = 'horde')
     {
         if (!isset(self::$_hooksLoaded[$app])) {
             try {
@@ -1812,20 +1806,16 @@ HTML;
             }
         }
 
-        if (function_exists($hook)) {
-            $result = call_user_func_array($hook, $args);
-            if ($result instanceof PEAR_Error) {
-                self::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
-            }
-            return $result;
-        }
+        if (!function_exists($hook)) {
+            throw new Horde_Exception(sprintf('Hook %s in application %s not called.', $hook, $app));
 
-        if (is_string($error) && strcmp($error, 'PEAR_Error') == 0) {
-            $error = PEAR::raiseError(sprintf('Hook %s in application %s not called.', $hook, $app));
-            self::logMessage($error, __FILE__, __LINE__, PEAR_LOG_DEBUG);
         }
 
-        return $error;
+        try {
+            return call_user_func_array($hook, $args);
+        } catch (Horde_Exception $e) {
+            self::logMessage($result, __FILE__, __LINE__, PEAR_LOG_ERR);
+        }
     }
 
     /**
index 35c1a92..eccf2d7 100644 (file)
@@ -819,7 +819,9 @@ class Horde_Registry
         $this->_appStack[] = $app;
 
         /* Call post-push hook. */
-        Horde::callHook('_horde_hook_post_pushapp', array($app), 'horde', null);
+        try {
+            Horde::callHook('_horde_hook_post_pushapp', array($app), 'horde');
+        } catch (Horde_Exception $e) {}
 
         return true;
     }