6.0.0-git
2024-10-15
Last Modified 2013-09-04 by Jan Schneider

Dynamically selecting an IMAP server for authentication

This information is valid for Horde 5 or later only. See ImapSelectH4 for Horde 4 or ImapSelectH3 for Horde 3.

For Horde 5 or later there already exists an example hook for dynamic IMAP server selection in IMP (imp/config/hooks.php.dist). For the sake of completeness, here is a stripped down copy of the example:

class IMP_Hooks
{
    /**
     * AUTHENTICATION HOOK: pre-authentication actions.
     *
     * See horde/config/hooks.php.dist for more information.
     *
     * IMP uses the following credentials:
     *   - password: (string) The password for mail server authentication.
     *   - server: (string) [optional] Use this server key (see
     *             config/backends.php).
     *   - transparent: (boolean) If $credentials['authMethod'] is
     *                  'transparent', and you want IMP to use the
     *                  userId/credentials generated in the preauthenticate
     *                  hook, this must be true. If false, IMP will instead
     *                  try to authenticate using hordeauth.
     *
     * The following credentials will exist in $credentials, but changing
     * these values has no effect on authentication:
     *   - imp_server_key: (string; 'authenticate' only) The backend server
     *                     key selected on the login page.
     */
     public function preauthenticate($userId, $credentials)
     {
         if ($credentials['authMethod'] == 'authenticate') {
             // Example: Load-balance - pick IMAP server based on first
             // letter in username. Server entries 'server_[a-z]' must
             // be defined in config/backends.local.php.
             $credentials['server'] = 'server_' . substr($userId, 0, 1);
             return array(
                 'credentials' => $credentials,
                 'userId' => $userId
             );
         }
         return true;
    }
}