From 0f461c264a6d9adf346eb0c1559f755afe90e187 Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Sun, 26 Jul 2009 20:13:54 -0600 Subject: [PATCH] Convert to new hooks code. --- framework/Auth/lib/Horde/Auth.php | 55 +++++++++++++---------- framework/Auth/lib/Horde/Auth/Ldap.php | 29 +++++++----- framework/Auth/lib/Horde/Auth/Msad.php | 31 ++++++------- framework/Auth/lib/Horde/Auth/Signup.php | 76 +++++++++++++++----------------- framework/Core/lib/Horde/Registry.php | 9 +++- 5 files changed, 107 insertions(+), 93 deletions(-) diff --git a/framework/Auth/lib/Horde/Auth.php b/framework/Auth/lib/Horde/Auth.php index f5e9eafc2..1e2f3edee 100644 --- a/framework/Auth/lib/Horde/Auth.php +++ b/framework/Auth/lib/Horde/Auth.php @@ -766,8 +766,7 @@ class Horde_Auth } /** - * Applies a hook defined by the function _username_hook_frombackend() to - * the given user name if this function exists and user hooks are enabled. + * Applies the username_frombackend hook to the given user name. * * This method should be called if a authentication backend's user name * needs to be converted to a (unique) Horde user name. The backend's user @@ -781,14 +780,15 @@ class Horde_Auth */ static public function addHook($userId) { - return empty($GLOBALS['conf']['hooks']['username']) - ? $userId - : Horde::callHook('_username_hook_frombackend', array($userId)); + try { + return Horde::callHook('username_frombackend', array($userId)); + } catch (Horde_Exception_HookNotSet $e) { + return $userId; + } } /** - * Applies a hook defined by the function _username_hook_tobackend() to - * the given user name if this function exists and user hooks are enabled. + * Applies the username_tobackend hook to the given user name. * * This method should be called if a Horde user name needs to be converted * to an authentication backend's user name or displayed to the user. The @@ -802,9 +802,11 @@ class Horde_Auth */ static public function removeHook($userId) { - return empty($GLOBALS['conf']['hooks']['username']) - ? $userId - : Horde::callHook('_username_hook_tobackend', array($userId)); + try { + return Horde::callHook('username_tobackend', array($userId)); + } catch (Horde_Exception_HookNotSet $e) { + return $userId; + } } /** @@ -823,23 +825,28 @@ class Horde_Auth { $ret_array = array($userId, $credentials); - if (!empty($GLOBALS['conf']['hooks'][$type])) { - $result = Horde::callHook('_horde_hook_' . $type, array($userId, $credentials, $app), 'horde'); - if ($result === false) { - if (self::getAuthError() != self::REASON_MESSAGE) { - self::setAuthError(self::REASON_FAILED); - } - throw new Horde_Auth_Exception($type . ' hook failed'); + try { + $result = Horde::callHook($type, array($userId, $credentials), $app); + } catch (Horde_Exception $e) { + throw new Horde_Auth_Exception($e->getMessage()); + } catch (Horde_Exception_HookNotSet $e) { + return $ret_array; + } + + if ($result === false) { + if (self::getAuthError() != self::REASON_MESSAGE) { + self::setAuthError(self::REASON_FAILED); } + throw new Horde_Auth_Exception($type . ' hook failed'); + } - if (is_array($result)) { - if (isset($result['userId'])) { - $ret_array[0] = $result['userId']; - } + if (is_array($result)) { + if (isset($result['userId'])) { + $ret_array[0] = $result['userId']; + } - if (isset($result['credentials'])) { - $ret_array[1] = $result['credentials']; - } + if (isset($result['credentials'])) { + $ret_array[1] = $result['credentials']; } } diff --git a/framework/Auth/lib/Horde/Auth/Ldap.php b/framework/Auth/lib/Horde/Auth/Ldap.php index 666a43da4..c7b9567ef 100644 --- a/framework/Auth/lib/Horde/Auth/Ldap.php +++ b/framework/Auth/lib/Horde/Auth/Ldap.php @@ -22,6 +22,9 @@ * DEFAULT: NONE (system default will be used) * * + * 'preauthenticate' hook should return LDAP connection information in the + * 'ldap' credentials key. + * * Copyright 1999-2009 The Horde Project (http://www.horde.org/) * * See the enclosed file COPYING for license information (LGPL). If you did @@ -322,10 +325,11 @@ class Horde_Auth_Ldap extends Horde_Auth_Base /* Connect to the LDAP server. */ $this->_connect(); - global $conf; - if (!empty($conf['hooks']['authldap'])) { - $entry = Horde::callHook('_horde_hook_authldap', array($userId, $credentials)); + list($userId, $credentials) = Horde_Auth::runHook($userId, $credentials, $this->_app, 'preauthenticate'); + if (isset($credentials['ldap'])) { + $entry = $credentials['ldap']; $dn = $entry['dn']; + /* Remove the dn entry from the array. */ unset($entry['dn']); } else { @@ -350,6 +354,7 @@ class Horde_Auth_Ldap extends Horde_Auth_Base $entry['shadowLastChange'] = floor(time() / 86400); } } + $result = @ldap_add($this->_ds, $dn, $entry); if (!$result) { @@ -375,9 +380,9 @@ class Horde_Auth_Ldap extends Horde_Auth_Base /* Connect to the LDAP server. */ $this->_connect(); - if (!empty($GLOBALS['conf']['hooks']['authldap'])) { - $entry = Horde::callHook('_horde_hook_authldap', array($userId)); - $dn = $entry['dn']; + list($userId, $credentials) = Horde_Auth::runHook($userId, array(), $this->_app, 'preauthenticate'); + if (isset($credentials['ldap'])) { + $dn = $credentials['ldap']['dn']; } else { /* Search for the user's full DN. */ $dn = $this->_findDN($userId); @@ -411,12 +416,12 @@ class Horde_Auth_Ldap extends Horde_Auth_Base /* Connect to the LDAP server. */ $this->_connect(); - if (!empty($GLOBLS['conf']['hooks']['authldap'])) { - $entry = Horde::callHook('_horde_hook_authldap', array($oldID, $credentials)); - $olddn = $entry['dn']; - $entry = Horde::callHook('_horde_hook_authldap', array($newID, $credentials)); - $newdn = $entry['dn']; - unset($entry['dn']); + list($oldID, $old_credentials) = Horde_Auth::runHook($oldID, $credentials, $this->_app, 'preauthenticate'); + if (isset($old_credentials['ldap'])) { + $olddn = $old_credentials['ldap']['dn']; + list($newID, $new_credentials) = Horde_Auth::runHook($newID, $credentials, $this->_app, 'preauthenticate'); + $newdn = $new_credentials['ldap']['dn']; + unset($new_credentials['ldap']['dn']); } else { /* Search for the user's full DN. */ $dn = $this->_findDN($oldID); diff --git a/framework/Auth/lib/Horde/Auth/Msad.php b/framework/Auth/lib/Horde/Auth/Msad.php index 6903d1158..47a278266 100644 --- a/framework/Auth/lib/Horde/Auth/Msad.php +++ b/framework/Auth/lib/Horde/Auth/Msad.php @@ -76,13 +76,13 @@ 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'); - if (!is_null($entry)) { - $dn = $entry['dn']; - unset($entry['dn']); + list($accountName, $credentials) = Horde_Auth::runHook($accountName, $credentials, $this->_app, 'preauthenticate'); + if (isset($credentials['ldap'])) { + $dn = $credentials['ldap']['dn']; } else { - $basedn = (isset($credentials['basedn'])) ? - $credentials['basedn'] : $this->_params['basedn']; + $basedn = isset($credentials['basedn']) + ? $credentials['basedn'] + : $this->_params['basedn']; /* Set a default CN */ $dn = 'cn=' . $accountName . ',' . $basedn; @@ -114,7 +114,7 @@ class Horde_Auth_Msad extends Horde_Auth_Ldap $success = @ldap_add($this->_ds, $dn, $entry); if (!$success) { - throw new Horde_Auth_Exception(sprintf(_("Auth_msad: Unable to add user \"%s\". This is what the server said: "), $accountName) . ldap_error($this->_ds)); + throw new Horde_Auth_Exception(sprintf(_("Horde_Auth_Msad: Unable to add user \"%s\". This is what the server said: "), $accountName) . ldap_error($this->_ds)); } @ldap_close($this->_ds); @@ -132,11 +132,13 @@ class Horde_Auth_Msad extends Horde_Auth_Ldap /* Connect to the MSAD server. */ $this->_connect(); - $entry = Horde::callHook('_horde_hook_authmsad', array($accountName), 'horde'); - $dn = is_null($entry) + list($accountName, $credentials) = Horde_Auth::runHook($accountName, $credentials, $this->_app, 'preauthenticate'); + if (isset($credentials['ldap'])) { + $dn = $credentials['ldap']['dn']; + } else { /* Search for the user's full DN. */ - ? $this->_findDN($accountName) - : $entry['dn']; + $dn = $this->_findDN($accountName); + } if (!@ldap_delete($this->_ds, $dn)) { throw new Horde_Auth_Exception(sprintf(_("Horde_Auth_Msad: Unable to remove user \"%s\""), $accountName)); @@ -161,10 +163,9 @@ 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'); - if (!is_null($entry)) { - $olddn = $entry['dn']; - unset($entry['dn']); + list($oldId, $credentials) = Horde_Auth::runHook($oldId, $credentials, $this->_app, 'preauthenticate'); + if (isset($credentials['ldap'])) { + $olddn = $credentials['ldap']['dn']; } else { /* Search for the user's full DN. */ $dn = $this->_findDN($oldId); diff --git a/framework/Auth/lib/Horde/Auth/Signup.php b/framework/Auth/lib/Horde/Auth/Signup.php index 36bd7aa66..cdb873cc2 100644 --- a/framework/Auth/lib/Horde/Auth/Signup.php +++ b/framework/Auth/lib/Horde/Auth/Signup.php @@ -59,44 +59,32 @@ class Horde_Auth_Signup * been compiled, relying on the hooks.php file. * * @params mixed $info Reference to array of parameteres to be passed - * to hook + * to hook. * - * @return mixed PEAR_Error if any errors, otherwise true. + * @throws Horde_Exception */ - public function addSignup(&$info) + public function addSignup($info) { - global $auth, $conf; + global $auth; // Perform any preprocessing if requested. - if ($conf['signup']['preprocess']) { - $info = Horde::callHook('_horde_hook_signup_preprocess', array($info)); - } + try { + $info = Horde::callHook('signup_preprocess', array($info)); + } catch (Horde_Exception_HookNotSet $e) {} // Check to see if the username already exists. if ($auth->exists($info['user_name']) || $this->exists($info['user_name'])) { - return PEAR::raiseError(sprintf(_("Username \"%s\" already exists."), $info['user_name'])); + throw new Horde_Exception(sprintf(_("Username \"%s\" already exists."), $info['user_name'])); } // Attempt to add the user to the system. - $result = $auth->addUser($info['user_name'], array('password' => $info['password'])); - if (is_a($result, 'PEAR_Error')) { - return $result; - } - - $result = true; + $auth->addUser($info['user_name'], array('password' => $info['password'])); // Attempt to add/update any extra data handed in. if (!empty($info['extra'])) { - 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()); - } - } - - return $result; + Horde::callHook('signup_addextra', array($info['user_name'], $info['extra'])); + } catch (Horde_Exception_HookNotSet $e) {} } /** @@ -105,8 +93,7 @@ class Horde_Auth_Signup * @params mixed $info Reference to array of parameteres to be passed * to hook * - * @return mixed PEAR_Error if any errors, otherwise true. - * + * @throws Horde_Exception * @throws Horde_Mime_Exception */ public function queueSignup(&$info) @@ -114,14 +101,14 @@ class Horde_Auth_Signup global $auth, $conf; // Perform any preprocessing if requested. - if ($conf['signup']['preprocess']) { - $info = Horde::callHook('_horde_hook_signup_preprocess', array($info)); - } + try { + $info = Horde::callHook('signup_preprocess', array($info)); + } catch (Horde_Exception_HookNotSet $e) {} // Check to see if the username already exists. if ($auth->exists($info['user_name']) || $this->exists($info['user_name'])) { - return PEAR::raiseError(sprintf(_("Username \"%s\" already exists."), $info['user_name'])); + throw new Horde_Exception(sprintf(_("Username \"%s\" already exists."), $info['user_name'])); } // If it's a unique username, go ahead and queue the request. @@ -140,9 +127,9 @@ class Horde_Auth_Signup return $result; } - if ($conf['signup']['queue']) { - $result = Horde::callHook('_horde_hook_signup_queued', array($info['user_name'], $info)); - } + try { + Horde::callHook('signup_queued', array($info['user_name'], $info)); + } catch (Horde_Exception_HookNotSet $e) {} if (!empty($conf['signup']['email'])) { $link = Horde_Util::addParameter(Horde::url($GLOBALS['registry']->get('webroot', 'horde') . '/admin/signup_confirm.php', true, -1), @@ -171,11 +158,11 @@ class Horde_Auth_Signup * @params mixed $info Reference to array of parameteres to be passed * to hook * - * @return mixed PEAR_Error if any errors, otherwise true. + * @throws Horde_Exception */ - protected function &_queueSignup(&$info) + protected function _queueSignup(&$info) { - return PEAR::raiseError('Not implemented'); + throw new Horde_Exception('Not implemented'); } /** @@ -183,31 +170,34 @@ class Horde_Auth_Signup * * @param string $username The username to retrieve the queued info for. * - * @return object The bject for the requested signup. + * @return object The object for the requested signup. + * @throws Horde_Exception */ public function getQueuedSignup($username) { - return PEAR::raiseError('Not implemented'); + throw new Horde_Exception('Not implemented'); } /** * Get the queued information for all pending signups. * * @return array An array of objects, one for each signup in the queue. + * @throws Horde_Exception */ public function getQueuedSignups() { - return PEAR::raiseError('Not implemented'); + throw new Horde_Exception('Not implemented'); } /** * Remove a queued signup. * * @param string $username The user to remove from the signup queue. + * @throws Horde_Exception */ public function removeQueuedSignup($username) { - return PEAR::raiseError('Not implemented'); + throw new Horde_Exception('Not implemented'); } /** @@ -216,10 +206,11 @@ class Horde_Auth_Signup * @param string $name The signups's name. * * @return object A new signup object. + * @throws Horde_Exception */ public function newSignup($name) { - return PEAR::raiseError('Not implemented'); + throw new Horde_Exception('Not implemented'); } } @@ -250,7 +241,10 @@ 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'); + try { + $extra = Horde::callHook('signup_getextra'); + } catch (Horde_Exception_HookNotSet $e) {} + if (!empty($extra)) { if (!isset($extra['user_name'])) { $this->addVariable(_("Choose a username"), 'user_name', 'text', true); diff --git a/framework/Core/lib/Horde/Registry.php b/framework/Core/lib/Horde/Registry.php index 665cde81a..6d3f3faa3 100644 --- a/framework/Core/lib/Horde/Registry.php +++ b/framework/Core/lib/Horde/Registry.php @@ -25,6 +25,7 @@ class Horde_Registry const AUTH_FAILURE = 1; const NOT_ACTIVE = 2; const PERMISSION_DENIED = 3; + const HOOK_FATAL = 4; /** * Singleton value. @@ -788,6 +789,7 @@ class Horde_Registry * Horde_Registry::AUTH_FAILURE * Horde_Registry::NOT_ACTIVE * Horde_Registry::PERMISSION_DENIED + * Horde_Registry::HOOK_FATAL */ public function pushApp($app, $options = array()) { @@ -856,7 +858,12 @@ class Horde_Registry $this->_appStack[] = $app; /* Call post-push hook. */ - Horde::callHook('_horde_hook_post_pushapp', array($app), 'horde'); + try { + Horde::callHook('pushapp', array(), $app); + } catch (Horde_Exception $e) { + $e->setCode(self::HOOK_FATAL); + throw $e; + } catch (Horde_Exception_HookNotSet $e) {} /* Do login tasks. */ if (Horde_Auth::getAuth()) { -- 2.11.0