[imp] dynamically selecting the login server (based on the user)

Michael Redinger Michael.Redinger at uibk.ac.at
Mon Feb 11 20:58:06 UTC 2008


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I had problems dynamically selecting the login server based on the HowTo
"Dynamically selecting an IMAP server for authentication"
(http://wiki.horde.org/ImapSelect) - eg. I experienced strange problems
with the DIMP mailbox list.
Also, I always thought that this method (messing with the _SESSION
variable) was quite ugly.

My goal was to select the IMAP server based on the user's name - similar
to what is describe in the HOWTO.

I now found a much simpler method - I just wanted to post this, in case
anybody else is interrested in something like this:

For each server you can set 'preferred' to indicate on which web server
this (IMAP) server should be the preferred server.
This method of selecting the server has been around for a very long long
time. It was intended for situations where you use the same horde config
on multiple web servers and select the appropriate IMAP server based on
the web server. Eg.:

$server['imapserver1'] = array(
	'preferred' => 'webmail1.mydomain.com',
        ...
);
$server['imapserver2'] = array(
	'preferred' => 'webmail2.mydomain.com',
        ...
);

When Horde is started on webmail1, imapserver1 is chosen as the
preferred server. On webmail2, it is imapserver2.

However, you can use (or abuse?) this as well for selecting the user
based on the user pattern.
In fact, this method seems much easier and cleaner than using the Horde
preauthentication hook.

Just use a servers.php file as follows:


######
if (!function_exists('set_preferred_server')) {
   function set_preferred_server($userpattern) {

      $user = Util::getFormData('imapuser');

      if (!empty($user)) {
         foreach ($userpattern as $pattern) {
            if ( preg_match("/" . $pattern . "/i", $user)) {
             return $_SERVER['SERVER_NAME'];
            }
         }
      }
      return false;
   }
}


$servers['imapserver1'] = array(
    'preferred' => set_preferred_server(array('^A', '.*-XY$'))
    ...
);


$servers['imapserver2'] = array(
    'preferred' => set_preferred_server(array('.*'))
    ...
);
######



This seems to work very well for me.
As you can see, the server is based on a regexp pattern. Also, the
second server here is used as a "fallback" for all users not matched by
another pattern.


Greetings,
Michael


- --
Michael Redinger
Zentraler Informatikdienst (Central IT Services)
Universitaet Innsbruck
Technikerstrasse 13                    Tel.: ++43 512 507 2335
6020 Innsbruck                         Fax.: ++43 512 507 949 02335
Austria                                Mail: Michael.Redinger at uibk.ac.at
BB98 D2FE 0F2C 2658 3780  3CB1 0FD7 A9D9 65C2 C11D
http://homepage.uibk.ac.at/~c102mr/mred-pubkey.asc
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

iD8DBQFHsLbeD9ep2WXCwR0RApTwAKDXEKWwzRzHxsVcpaVOrK434TkWWgCgxuK0
YS7AVFtFEU7swRUOw41u/pE=
=MVZC
-----END PGP SIGNATURE-----


More information about the imp mailing list