[horde] LDAP configuration and hooks
Jan Schneider
jan at horde.org
Sat Dec 7 17:35:57 UTC 2019
Zitat von David Wells <dwells at alfavinil.com>:
> El 30/11/2019 a las 10:08 a. m., Jens Wahnes escribió:
>> David Wells wrote:
>>> When I modify the hooks.local.php file so that it reads
>>>
>>>> <?php
>>>> class Horde_Hooks
>>>> {
>>>> public function prefs_init($pref, $value, $username, $scope_ob)
>>>> {
>>>> switch ($pref) {
>>>> case 'from_addr':
>>>>
>>>> if (is_null($username)) {
>>>> return $value;
>>>> }
>>>>
>>>> $ldap = $GLOBALS['injector']->getInstance('Horde_Ldap');
>>>> try {
>>>> $result = $ldap->search(
>>>> $GLOBALS['conf']['ldap']['user']['basedn'],
>>>> Horde_Ldap_Filter::create('userPrincipalName', 'equals', $username),
>>>> array('attributes' => array('mail'))
>>>> );
>>>> if ($result->count()) {
>>>> $entry = $result->shiftEntry();
>>>> return $entry->getValue('mail', 'single');
>>>> }
>>>> } catch (Horde_Ldap_Exception $e) {
>>>> }
>>
>> I'm not sure if that can actually work.
>>
>> Here's an excerpt of what we use inside Horde_Hooks that does the job:
>>
>> public function prefs_init($pref, $value, $username, $scope_ob)
>> {
>> // [...]
>>
>> $factory =
>> $GLOBALS['injector']->getInstance('Horde_Core_Factory_Ldap');
>> $ldap = $factory->create('horde', $factory->getConfig('auth'));
>> try {
>> $userdn = $ldap->findUserDN($username);
>> $user = $ldap->getEntry($userdn, array('mail','sn'));
>> $email = $user->getValue('mail', 'single');
>> } catch (Horde_Ldap_Exception $e) {
>> Horde::log("prefs_init: user $username: Error looking
>> up LDAP information", 'WARN');
>> Horde::log($e, 'WARN');
>> return $value;
>> } catch (Horde_Exception_NotFound $e) {
>> Horde::log("prefs_init: user $username: User not
>> present in LDAP", 'WARN');
>> Horde::log($e, 'WARN');
>> return $value;
>> }
>>
>> // [...]
>> }
>>
>>
>>
>> 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 = 'dwells at fliawells.com.ar';
>> $factory = $GLOBALS['injector']->getInstance('Horde_Core_Factory_Ldap');
>> $ldap = $factory->create('horde', $factory->getConfig('auth'));
>> $result = $ldap->search($GLOBALS['conf']['ldap']['user']['basedn'],
>> Horde_Ldap_Filter::create('userPrincipalName', 'equals',
>> $username), array('attributes' => array('mail', 'cn')));
>> if ($result->count())
>> {
>> $entry = $result->shiftEntry();
>> $mail = $entry->getValue('mail', 'single');
>> $fullname = $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 userPrincipalName=dwells at fliawells.com.ar: 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.
Well, the message is pretty clear.
--
Jan Schneider
The Horde Project
https://www.horde.org/
More information about the horde
mailing list