[horde] Setting up identities in hooks

Diego Woitasen diegows at xtech.com.ar
Sun May 9 23:37:35 UTC 2010


2010/5/8 Diego Woitasen <diegows at xtech.com.ar>

> Hi,
>  I'm using Horde Groupware (Kolab). I need to setup user identities every
> times the user logins. The informatino is stored in LDAP. I know that
> config/hooks.php is the place to put the code, the code is executed but I
> don't underdand why my modifications to identities and sent to /dev/null.
>
> The example code with LDAP that doesn't work.
>
> if (!function_exists('_horde_hook_postauthenticate')) {
>     function _horde_hook_postauthenticate($userID, $credential, $realm) {
>         require_once 'Horde/Identity.php';
>         $name = "$userID";
>         if (is_null($name)) {
>             $name = Auth::getAuth();
>         }
>
>        Horde::logMessage('Setting up identities.');
>
>         $identities = &Identity::singleton('none',"$name");
>         $identities->init();
>
>         $id_data = array( "id" => "Uno", "fullname" => "Full Uno",
>                         "from_addr" => "uno at kolab-demo.xtech.com.ar");
>         $identities->add($id_data);
>
>         $id_data = array( "id" => "Dos", "fullname" => "Full Dos",
>                         "from_addr" => "dos at kolab-demo.xtech.com.ar");
>         $identities->add($id_data);
>
>         $id_data = array( "id" => "Tres", "fullname" => "Full Tres",
>                         "from_addr" => "tres at kolab-demo.xtech.com.ar");
>         $identities->add($id_data);
>
>         $identities->setDefault(0);
>
>         $identities->save();
>
>         return true;
>     }
>
> Any ideas?
>
> Thanks!
>
> --
> Diego Woitasen
> XTECH
>


I found the solution. The right hook to change identities is
_imp_hook_postlogin. In _horde_
hook_postauthenticate the preferences are not loaded yet, so you can't
change anything.

That's the code to change Identities based on information stored in LDAP.
May be this is useful for somebody else.

if (!function_exists('_imp_hook_postlogin')) {
    function _imp_hook_postlogin($actionID, $isLogin) {
        require_once 'Horde/Identity.php';


        $userdn = $GLOBALS['conf']['kolab']['ldap']['phpdn'];
        $userpw = $GLOBALS['conf']['kolab']['ldap']['phppw'];
        $host = $GLOBALS['conf']['kolab']['ldap']['server'];
        $base = $GLOBALS['conf']['kolab']['ldap']['basedn'];
        $name = Auth::getAuth();

        $identities = &Identity::singleton('none',"$name");
        $identities->_identities = array();

        if(!isset($userdn) or !isset($userpw) or !isset($host) or
                !isset($base)){
            horde::logMessage("_imp_hook_postlogin: LDAP config param
missing",
                                __FILE__, __LINE__);
            return true;
        }

        $ldap = ldap_connect($host);
        if(!$ldap){
            horde::logMessage("_imp_hook_postlogin: LDAP connect fail",
                                __FILE__, __LINE__);
            return true;
        }

        if(!ldap_bind($ldap, $userdn, $userpw)){
            horde::logMessage("_imp_hook_postlogin: LDAP bind fail",
                                __FILE__, __LINE__);
            return true;
        }

        $filter = '(|(mail=%s)(kolabDelegate=%s)(kolabDelegate=sendas-%s))';
        $filter = sprintf($filter, $name, $name, $name);
        $attrs = array("cn", "mail");
        $res = ldap_search($ldap, $base, $filter, $attrs);
        if(!$res){
            horde::logMessage("_imp_hook_postlogin: LDAP search fail",
                                __FILE__, __LINE__);
            return true;
        }

        $info = ldap_get_entries($ldap, $res);
        for($i = 0; $i < $info["count"]; $i++){
            $id_data = array( "id" => $info[$i]["cn"][0],
                            "fullname" => $info[$i]["cn"][0],
                            "from_addr" => $info[$i]["mail"][0]);
            $pos = $identities->add($id_data);
            if($info[$i]["mail"][0] == $name)
                $identities->setDefault($pos);

        }

        $identities->init();
        $identities->save();

        return true;
    }
}


Regads,
 Diego


-- 
Diego Woitasen
XTECH


More information about the horde mailing list