Re: LDAP configuration and hooks

David Wells <[email protected]> Fri, 6 Dec 2019 18:14:13 -0300
Newsgroups gmane.comp.horde.user
Message-ID <[email protected]>
El 30/11/2019 a las 10:08 a. m., Jens Wahnes escribi=F3:
> David Wells wrote:
>> When I modify the hooks.local.php file so that it reads
>>
>>> <?php
>>> class Horde_Hooks
>>> {
>>> =A0=A0=A0 public function prefs_init($pref, $value, $username, $scope_o=
b)
>>> =A0=A0=A0 {
>>> =A0=A0=A0=A0=A0=A0=A0 switch ($pref) {
>>> =A0=A0=A0=A0=A0=A0=A0 case 'from_addr':
>>>
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if (is_null($username)) {
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return $value;
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
>>>
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $ldap =3D $GLOBALS['injector']->getIn=
stance('Horde_Ldap');
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 try {
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $result =3D $ldap->search(
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $GLOBALS['con=
f']['ldap']['user']['basedn'],
>>> Horde_Ldap_Filter::create('userPrincipalName', 'equals', $username),
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 array('attrib=
utes' =3D> array('mail'))
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 );
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 if ($result->count()) {
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $entry =3D $r=
esult->shiftEntry();
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return $entry=
->getValue('mail', 'single');
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 } catch (Horde_Ldap_Exception $e) {
>>> =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 }
> =

> I'm not sure if that can actually work.
> =

> Here's an excerpt of what we use inside Horde_Hooks that does the job:
> =

>  =A0=A0=A0 public function prefs_init($pref, $value, $username, $scope_ob)
>  =A0=A0=A0 {
>  =A0=A0=A0=A0=A0 // [...]
> =

>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 $factory =3D =

> $GLOBALS['injector']->getInstance('Horde_Core_Factory_Ldap');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 $ldap =3D $factory->create('horde', $factory=
->getConfig('auth'));
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 try {
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $userdn =3D $ldap->findUserDN($usernam=
e);
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $user =3D $ldap->getEntry($userdn, arr=
ay('mail','sn'));
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 $email =3D $user->getValue('mail', 'si=
ngle');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 } catch (Horde_Ldap_Exception $e) {
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Horde::log("prefs_init: user $username=
: Error looking up =

> LDAP information", 'WARN');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Horde::log($e, 'WARN');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return $value;
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 } catch (Horde_Exception_NotFound $e) {
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Horde::log("prefs_init: user $username=
: User not present in =

> LDAP", 'WARN');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 Horde::log($e, 'WARN');
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 return $value;
>  =A0=A0=A0=A0=A0=A0=A0=A0=A0 }
> =

>  =A0=A0=A0=A0 // [...]
>  =A0=A0=A0 }
> =

> =

> =

> For all I know, it doesn't matter if the file is named hooks.php or =

> hooks.local.php.
> =

> =

> Jens
Thank you very much for your reply!

Your code didn't work "as is" for me but it was a good starting point. I =

mixed a bit of your code with the hooks.php.dist sample and got it =

working. Here is the code that did the trick for future reference.
> $username =3D '[email protected]';
> $factory =3D $GLOBALS['injector']->getInstance('Horde_Core_Factory_Ldap');
> $ldap =3D $factory->create('horde', $factory->getConfig('auth'));
> $result =3D $ldap->search($GLOBALS['conf']['ldap']['user']['basedn'], Hor=
de_Ldap_Filter::create('userPrincipalName', 'equals', $username), array('at=
tributes' =3D> array('mail', 'cn')));
>     if ($result->count())
> 	{
>         $entry =3D $result->shiftEntry();
>         $mail =3D $entry->getValue('mail', 'single');
>         $fullname =3D $entry->getValue('cn', 'single');
> 	}
> print $mail;
> print $fullname;

The piece of code that just wasn't working is the findUserDN method =

which reported the following exception
> exception 'Horde_Exception_NotFound' with message 'Could not fetch entry =
[email protected]: no entry found' in /usr/lib64/=
php/Horde/Ldap.php:1080

If someone could help me figure out what could be wrong with this I =

would appreciate it but at least I can go on with this task.

Thank you very much again!
Best regards,
David Wells.

-- =

Horde mailing list
Frequently Asked Questions: http://horde.org/faq/
To unsubscribe, mail: [email protected]