Commit c90f52db authored by Côme Chilliet's avatar Côme Chilliet
Browse files

:ambulance: fix(core) Fix create_missing_trees to be able to snapshot in...

:ambulance: fix(core) Fix create_missing_trees to be able to snapshot in locality, amond other things

Changed code so that common department classes are hardcoded to speed up
 process and avoid weird classes like residentialPerson instead of
 locality.
Also fixed code so that MUST attributes from all classes of the
 hierarchy are filled whether autodetection is used or not.

issue #5861
Showing with 73 additions and 67 deletions
+73 -67
...@@ -733,7 +733,7 @@ class LDAP ...@@ -733,7 +733,7 @@ class LDAP
$real_path = substr($target, 0, strlen($target) - strlen($this->basedn) - 1); $real_path = substr($target, 0, strlen($target) - strlen($this->basedn) - 1);
if ($target == $this->basedn) { if ($target == $this->basedn) {
$l = array("dummy"); $l = array('dummy');
} else { } else {
$l = array_reverse(ldap_explode_dn($real_path, 0)); $l = array_reverse(ldap_explode_dn($real_path, 0));
} }
...@@ -744,111 +744,117 @@ class LDAP ...@@ -744,111 +744,117 @@ class LDAP
$classes = $this->get_objectclasses(); $classes = $this->get_objectclasses();
foreach ($l as $part) { foreach ($l as $part) {
if ($part != "dummy") { if ($part != 'dummy') {
$cdn = "$part,$cdn"; $cdn = "$part,$cdn";
} }
/* Ignore referrals */ /* Ignore referrals */
if ($ignoreReferralBases) { if ($ignoreReferralBases) {
$found = FALSE;
foreach ($this->referrals as $ref) { foreach ($this->referrals as $ref) {
if ($ref['BASE'] == $cdn) { if ($ref['BASE'] == $cdn) {
$found = TRUE; continue 2;
break;
} }
} }
if ($found) {
continue;
}
} }
$this->cat ($srp, $cdn); $this->cat ($srp, $cdn);
$attrs = $this->fetch($srp); $attrs = $this->fetch($srp);
/* Create missing entry? */ /* Create missing entry? */
if (!count($attrs)) { if (count($attrs)) {
$type = preg_replace('/^([^=]+)=.*$/', '\\1', $cdn); continue;
$param = preg_replace('/^[^=]+=([^,]+).*$/', '\\1', $cdn); }
$param = preg_replace(array('/\\\\,/','/\\\\"/'), array(',','"'), $param);
$na = array(); $type = preg_replace('/^([^=]+)=.*$/', '\\1', $cdn);
$param = preg_replace('/^[^=]+=([^,]+).*$/', '\\1', $cdn);
$param = preg_replace(array('/\\\\,/','/\\\\"/'), array(',','"'), $param);
/* Automatic or traditional? */ $attrs = array($type => $param);
if (count($classes)) {
/* Hardcoded classes */
switch ($type) {
case 'ou':
$attrs['objectClass'] = array('organizationalUnit');
break;
case 'd':
$attrs['objectClass'] = array('domain');
break;
case 'dc':
$attrs['objectClass'] = array('dcObject');
break;
case 'o':
$attrs['objectClass'] = array('organization');
break;
case 'l':
$attrs['objectClass'] = array('locality');
break;
case 'c':
$attrs['objectClass'] = array('country');
break;
default:
/* Fallback to autodetection of objectClass */
if (!count($classes)) {
msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": not supported'), $type), FATAL_ERROR_DIALOG);
exit();
}
/* Get name of first matching objectClass */ /* Get name of first matching objectClass */
$ocname = ""; $attrs['objectClass'] = array();
foreach ($classes as $class) { foreach ($classes as $class) {
if (isset($class['MUST']) && in_array($type, $class['MUST'])) { if (isset($class['MUST']) && in_array($type, $class['MUST'])) {
/* Look for first class that is structural... */
/* Look for first classes that is structural... */
if (isset($class['STRUCTURAL'])) { if (isset($class['STRUCTURAL'])) {
$ocname = $class['NAME']; $attrs['objectClass'] = array($class['NAME']);
break; break;
} }
/* Look for classes that are auxiliary... */ /* Look for class that is auxiliary... */
if (isset($class['AUXILIARY'])) { if (empty($attrs['objectClass']) && isset($class['AUXILIARY'])) {
$ocname = $class['NAME']; $attrs['objectClass'] = array($class['NAME']);
} }
} elseif (empty($attrs['objectClass']) && isset($class['MAY']) && in_array($type, $class['MAY'])) {
/* Better than nothing */
$attrs['objectClass'] = array($class['NAME']);
} }
} }
/* Bail out, if we've nothing to do... */ /* Bail out, if we've nothing to do... */
if ($ocname == '') { if (empty($attrs['objectClass'])) {
msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": no object class found!'), $type), FATAL_ERROR_DIALOG); msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": no object class found!'), $type), FATAL_ERROR_DIALOG);
exit(); exit();
} }
}
/* Assemble_entry */ $ocname = $attrs['objectClass'][0];
$na['objectClass'] = array($ocname); while (isset($classes[$ocname]['SUP']) && ($classes[$ocname]['SUP'] != 'top')) {
if (isset($classes[$ocname]['AUXILIARY'])) { $ocname = $classes[$ocname]['SUP'];
$na['objectClass'][] = $classes[$ocname]['SUP']; $attrs['objectClass'][] = $ocname;
} }
if ($type == 'dc') {
/* This is bad actually, but - tell me a better way? */
$na['objectClass'][] = 'organization';
$na['o'] = $param;
}
$na[$type] = $param;
// Fill in MUST values - but do not overwrite existing ones. if (isset($classes[$ocname]['AUXILIARY'])) {
if (is_array($classes[$ocname]['MUST'])) { /* AUXILIARY class, we have to add a STRUCTURAL one */
foreach ($classes[$ocname]['MUST'] as $attr) { $attrs['objectClass'][] = 'organization';
if (isset($na[$attr]) && !empty($na[$attr])) { }
continue;
} foreach ($attrs['objectClass'] as $ocname) {
$na[$attr] = 'filled'; // Fill in MUST values - but do not overwrite existing ones.
if (is_array($classes[$ocname]['MUST'])) {
foreach ($classes[$ocname]['MUST'] as $attr) {
if (empty($attrs[$attr])) {
$attrs[$attr] = $param;
} }
} }
} else {
/* Use alternative add... */
switch ($type) {
case 'ou':
$na['objectClass'] = 'organizationalUnit';
$na['ou'] = $param;
break;
case 'dc':
$na['objectClass'] = array('dcObject', 'top', 'organization');
$na['dc'] = $param;
$na['o'] = $param;
break;
default:
msg_dialog::display(_('Internal error'), sprintf(_('Cannot automatically create subtrees with RDN "%s": not supported'), $type), FATAL_ERROR_DIALOG);
exit();
}
} }
$this->cd($cdn); }
$this->add($na); $this->cd($cdn);
$this->add($attrs);
if (!$this->success()) { if (!$this->success()) {
@DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $cdn, 'dn'); @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $cdn, 'dn');
@DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $na, 'Content'); @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $attrs, 'Content');
@DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->get_error(), 'LDAP error'); @DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->get_error(), 'LDAP error');
msg_dialog::display(_('LDAP error'), msgPool::ldaperror($this->get_error(), $cdn, LDAP_ADD, get_class()), LDAP_ERROR); msg_dialog::display(_('LDAP error'), msgPool::ldaperror($this->get_error(), $cdn, LDAP_ADD, get_class()), LDAP_ERROR);
return FALSE; return FALSE;
}
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment