Thomas Bruederli
2012-11-09 3f250a0fe63725f3daca904d93fd63d5eb437c56
Add config options to automatically generate LDAP attributes for new entries
2 files modified
34 ■■■■■ changed files
config/main.inc.php.dist 5 ●●●●● patch | view | raw | blame | history
program/include/rcube_ldap.php 29 ●●●●● patch | view | raw | blame | history
config/main.inc.php.dist
@@ -636,6 +636,11 @@
  ),
  // Map of contact sub-objects (attribute name => objectClass(es)), e.g. 'c' => 'country'
  'sub_fields' => array(),
  // Generate values for the following LDAP attributes automatically when creating a new record
  'autovalues' => array(
  // 'uid'  => 'md5(microtime())',               // You may specify PHP code snippets which are then eval'ed
  // 'mail' => '{givenname}.{sn}@mydomain.com',  // or composite strings with placeholders for existing attributes
  ),
  'sort'          => 'cn',    // The field to sort the listing by.
  'scope'         => 'sub',   // search mode: sub|base|list
  'filter'        => '(objectClass=inetOrgPerson)',      // used for basic listing (if not empty) and will be &'d with search queries. example: status=act
program/include/rcube_ldap.php
@@ -160,7 +160,7 @@
        }
        // make sure LDAP_rdn field is required
        if (!empty($this->prop['LDAP_rdn']) && !in_array($this->prop['LDAP_rdn'], $this->prop['required_fields'])) {
        if (!empty($this->prop['LDAP_rdn']) && !in_array($this->prop['LDAP_rdn'], $this->prop['required_fields']) && !in_array($this->prop['LDAP_rdn'], array_keys($this->prop['autovalues']))) {
            $this->prop['required_fields'][] = $this->prop['LDAP_rdn'];
        }
@@ -1086,6 +1086,9 @@
        $newentry = $this->_map_data($save_cols);
        $newentry['objectClass'] = $this->prop['LDAP_Object_Classes'];
        // add automatically generated attributes
        $this->add_autovalues($newentry);
        // Verify that the required fields are set.
        $missing = null;
        foreach ($this->prop['required_fields'] as $fld) {
@@ -1389,6 +1392,30 @@
        }
    }
    /**
     * Generate missing attributes as configured
     *
     * @param array LDAP record attributes
     */
    protected function add_autovalues(&$attrs)
    {
        $attrvals = array();
        foreach ($attrs as $k => $v) {
            $attrvals['{'.$k.'}'] = is_array($v) ? $v[0] : $v;
        }
        foreach ((array)$this->prop['autovalues'] as $lf => $templ) {
            if (empty($attrs[$lf])) {
                // replace {attr} placeholders with concrete attribute values
                $templ = preg_replace('/\{\w+\}/', '', strtr($templ, $attrvals));
                if (strpos($templ, '(') !== false)
                    $attrs[$lf] = eval("return ($templ);");
                else
                    $attrs[$lf] = $templ;
            }
        }
    }
    /**
     * Execute the LDAP search based on the stored credentials