[turba] Re: problem with duplicate dn with LDAP backend

andreas oster aoster at novanetwork.de
Thu Apr 15 03:32:30 PDT 2004


andreas oster wrote:
> hello list,
> 
> I have run into a little problem with turba. I have set up a shared LDAp
> addressbook. I use a modified mozilla-schema and changed the appropriate
> settings in the attributes.php and sources.php file and everything works
> like a charm. Only the possibility of running into duplicate dn-problems
> gives me a headache. I would like to use 'uid' created from sn and  some
> numer (Date?) in the dn. Is this possible ? How can I do this. I tried
> some things but unfortunately my PHP skills are only rudimental, and 
> therefor didn't get very far :(
> 
> Can somebody help me with this problem/task ?
> 
> Thanks for your kind help
> 
> Andreas
> 
Hello List,

I post the complete bunch of changes I have made to the sources to get
my system behave the way I want. The source is head from 13.01.04, not
quiet current but I don't think that there have been a lot of changes
in the respective code. I hope this is of any use for someone else.

turba/conf/sources.php

$cfgSources['localldap'] = array(
     'title' => _("Company Directory"),
     'type' => 'ldap',

     'params' => array(
     'server' => 'ldap.something.com',
     'port' => 389,
     'root' => 'ou=AddressBook,dc=something,dc=com',
     'bind_dn' => 'cn=someuser,dc=something,dc=com',
     'bind_password' => '*******',
     'sizelimit' => 200,
     'dn' => array('uid'),
     'objectclass' => array('top', 'person', 'inetOrgPerson', 
'zillaPerson'),
     'charset' => 'utf-8',
     'version' => 3
     ),

     'map' => array(
     '__key' => 'dn',
     'name' => array('fields' => array('firstname', 'lastname'), 
'format' => '%s %s'),
     'firstname' => 'givenName',
     'lastname' => 'sn',
     'email' => 'mail',
     'company' => 'o',
     'businessCategory' => 'businessCategory',
     'companyTitle' => 'title',
     'companyDepartment' => 'ou',
     'companyStreet' => 'postalAddress',
     'companyCity' => 'l',
     'companyZip' => 'postalCode',
     'companyState' => 'st',
     'companyCountry' => 'c',
     'workPhone' => 'telephoneNumber',
     'cellPhone' => 'mobile',
     'fax' => 'facsimileTelephoneNumber',
     'companyURL' => 'workurl',
     'nickname' => 'xmozillanickname',
     'homeAddress' => 'homepostaladdress',
     'homeCity' => 'mozillaHomeLocalityName',
     'homeState' => 'mozillaHomeState',
     'homeZip' => 'mozillaHomePostalCode',
     'homeAddress' => 'mozillaHomeCountryName',
     'homePhone' => 'homePhone',
     'homeEmail' => 'mozillaSecondEmail',
     'homefax' => 'otherfacsimileTelephoneNumber',
     'homeURL' => 'homeurl',
     'notes' => 'comment'
     ),


turba/lib/Source.php

     /**
      * Adds a new entry to the contact source.
      *
      * @param array $attributes  The attributes of the new object to add.
      *
      * @return mixed             The new __key value on success, or a
      *                           PEAR_Error object on failure.
      */
     function addObject($attributes)
     {
         if ($this->readonly) {
             return false;
         }

         // Always generate a new key.
         $attributes['__key'] = 
$this->driver->makeKey($this->toDriverKeys($attri

         if (!isset($attributes['__type'])) {
             $attributes['__type'] = 'Object';
         }
         if (isset($this->map['__owner'])) {
             $attributes['__owner'] = Auth::getAuth();
         }

         $key = $attributes['__key'];
         $attributes = $this->toDriverKeys($attributes);
         $result = $this->driver->addObject($attributes);

         if (is_a($result, 'PEAR_Error')) {
             return $result;
         }

         /* Log the creation of this item in the history log. */
         $history = &Horde_History::singleton();
         $history->log($this->getGUID($key), array('action' => 'add'), 
true);

	/** added
         $key = $result;
	/** added end

         return $key;
     }


turba/lib/Driver/ldap.php

     /**
      * Adds the specified entry to the LDAP directory.
      *
      * @param array $attributes  The initial attributes for the new object.
      */
     function addObject($attributes)
     {
         if (empty($attributes['dn'])) {
             return PEAR::raiseError('Tried to add an object with no dn: 
[' . ser
         } elseif (empty($this->_params['objectclass'])) {
             return PEAR::raiseError('Tried to add an object with no 
objectclass:
         }

         // construct uid
         $uid = $attributes['givenName'] . $attributes['sn'] . 
date("dmyGi");
         // construct dn
         $dn = "uid=" . $uid . "," . $this->_params['root'];

         unset($attributes['dn']);

         $attributes['uid'] = $uid;
         // construct cn
         $attributes['cn'] = $attributes['givenName'] . " " . 
$attributes['sn'];

        // Put the Objectclass into the attributes array
         if (!is_array($this->_params['objectclass'])) {
             $attributes['objectclass'] = $this->_params['objectclass'];
         } else {
             $i = 0;
             foreach ($this->_params['objectclass'] as $objectclass) {
                 $attributes['objectclass'][$i] = $objectclass;
                 $i++;
             }
         }

         // Don't add empty attributes.
         $attributes = array_filter($attributes, array($this, 
'_emptyAttributeFil

         // Encode entries.
         foreach ($attributes as $key => $val) {
             if (!is_array($val)) {
                 $attributes[$key] = String::convertCharset($val, 
NLS::getCharset
             }
         }

         if (!@ldap_add($this->_ds, $dn, $attributes)) {
             return PEAR::raiseError('Failed to add an object: [' . 
ldap_errno($t
         } else {
         // return constructed cn
             return $dn;
         }
     }


best regards

Andreas



More information about the turba mailing list