From 96b29d8919db485290f4709fde4d5160b567d02c Mon Sep 17 00:00:00 2001 From: Roland Gruber Date: Mon, 6 Aug 2018 20:41:07 +0200 Subject: [PATCH] replace existing accounts during upload --- lam/HISTORY | 1 + lam/help/help.inc | 2 ++ lam/lib/upload.inc | 25 +++++++++++++++++++++- lam/templates/upload/massBuildAccounts.php | 6 +++++- lam/templates/upload/masscreate.php | 22 +++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/lam/HISTORY b/lam/HISTORY index b2f161d5..e172210c 100644 --- a/lam/HISTORY +++ b/lam/HISTORY @@ -1,5 +1,6 @@ September 2018 6.5 - Password change possible via LDAP EXOP operation (set LDAP_EXOP as password hash) + - Upload: allow to overwrite existing accounts - Kolab updates - LAM Pro: -> Auto deletion of entries with dynamic directory services support (requires PHP 7.2). diff --git a/lam/help/help.inc b/lam/help/help.inc index 36258b9e..c248c9c1 100644 --- a/lam/help/help.inc +++ b/lam/help/help.inc @@ -231,6 +231,8 @@ $helpArray = array ( // profile editor, file upload "301" => array ("Headline" => _("RDN identifier"), "Text" => _("This is the identifier for the relative DN value. It must be one of the given allowed LDAP attributes (e.g. user accounts usually use \"uid\" while groups use \"cn\").")), + "302" => array ("Headline" => _("Overwrite"), + "Text" => _("Set to true to overwrite an existing LDAP entry.")), "360" => array ("Headline" => _("Profile name"), "Text" => _("Name under which the profile will be saved. If a profile with the same name exists, it will be overwritten.")), "361" => array ("Headline" => _("DN suffix"), diff --git a/lam/lib/upload.inc b/lam/lib/upload.inc index 3395f1e0..1804c067 100644 --- a/lam/lib/upload.inc +++ b/lam/lib/upload.inc @@ -6,7 +6,7 @@ use accountContainer; $Id$ This code is part of LDAP Account Manager (http://www.ldap-account-manager.org/) - Copyright (C) 2016 - 2017 Roland Gruber + Copyright (C) 2016 - 2018 Roland Gruber This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -142,6 +142,12 @@ class Uploader { } } if ($preActionOk) { + $errors = $this->deleteOldEntryIfNeeded($dn, $this->accounts[$position]); + if (!empty($errors)) { + $_SESSION['mass_errors'] = array_merge($_SESSION['mass_errors'], $errors); + $_SESSION['mass_failed'][] = $position; + return; + } // add LDAP entry $success = @ldap_add($_SESSION['ldap']->server(), $dn, $attrs); if (!$success) { @@ -156,6 +162,23 @@ class Uploader { } } + /** + * Deletes an existing entry if overwrite option is used. + * + * @param string $dn DN + * @param array $attributes attribute list + * @return array list of error messages + */ + private function deleteOldEntryIfNeeded($dn, $attributes) { + if (isset($attributes['INFO.overwrite']) && ($attributes['INFO.overwrite'] === 'true')) { + $attrs = ldapGetDN($dn, array('dn')); + if ($attrs !== null) { + return deleteDN($dn, true); + } + } + return array(); + } + /** * Starts the file upload. * diff --git a/lam/templates/upload/massBuildAccounts.php b/lam/templates/upload/massBuildAccounts.php index a555c91a..0fc06abb 100644 --- a/lam/templates/upload/massBuildAccounts.php +++ b/lam/templates/upload/massBuildAccounts.php @@ -202,8 +202,8 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) { if ($accounts !== false) { $rdnList = getRDNAttributes($type->getId(), $selectedModules); $suffix = $type->getSuffix(); - // set DN foreach ($accounts as $i => $account) { + // set DN // check against list of possible RDN attributes if (!in_array($data[$i][$ids['dn_rdn']], $rdnList) || !isset($account[$data[$i][$ids['dn_rdn']]])) { $errors[] = array(_('Account %s:') . ' dn_rdn ' . $account[$data[$i][$ids['dn_rdn']]], _("Invalid RDN attribute!"), array($i)); @@ -214,6 +214,10 @@ if ($_FILES['inputfile'] && ($_FILES['inputfile']['size'] > 0)) { else $account_dn = $account_dn . $data[$i][$ids['dn_suffix']]; $accounts[$i]['dn'] = $account_dn; } + // set overwrite + if (isset($ids['overwrite']) && ($data[$i][$ids['overwrite']] === 'true')) { + $accounts[$i]['INFO.overwrite'] = 'true'; + } } // print errors if DN could not be built if (sizeof($errors) > 0) { diff --git a/lam/templates/upload/masscreate.php b/lam/templates/upload/masscreate.php index 8a8eb053..c1584de9 100644 --- a/lam/templates/upload/masscreate.php +++ b/lam/templates/upload/masscreate.php @@ -324,6 +324,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { $row->add($dnTitle, 12); $titles = array(_('Name'), _("Identifier"), _("Example value"), _("Default value"), _("Possible values")); $data = array(); + // DN suffix $dnSuffixRowCells = array(); $nameGroup = new htmlGroup(); $help = new htmlHelpLink('361'); @@ -340,6 +341,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { $dnSuffixRowCells[] = new htmlOutputText($type->getSuffix()); $dnSuffixRowCells[] = new htmlOutputText(''); $data[] = $dnSuffixRowCells; + // RDN $dnRDNRowCells = array(); $rdnText = new htmlOutputText(_("RDN identifier")); $rdnText->setMarkAsRequired(true); @@ -360,6 +362,24 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { $dnRDNRowCells[] = new htmlOutputText(implode(", ", $rdnAttributes)); $dnRDNRowCells[] = new htmlHelpLink('301'); $data[] = $dnRDNRowCells; + // replace existing + $replaceRowCells = array(); + $nameGroup = new htmlGroup(); + $help = new htmlHelpLink('302'); + $help->setCSSClasses(array('hide-on-mobile')); + $nameGroup->addElement($help); + $nameGroup->addElement(new htmlSpacer('0.25rem', '16px')); + $nameGroup->addElement(new htmlOutputText(_("Overwrite"))); + $help = new htmlHelpLink('302'); + $help->setCSSClasses(array('hide-on-tablet')); + $nameGroup->addElement($help); + $replaceRowCells[] = $nameGroup; + $replaceRowCells[] = new htmlOutputText('overwrite'); + $replaceRowCells[] = new htmlOutputText('false'); + $replaceRowCells[] = new htmlOutputText('false'); + $replaceRowCells[] = new htmlOutputText('true, false'); + $data[] = $replaceRowCells; + $table = new htmlResponsiveTable($titles, $data); $table->setCSSClasses(array('alternating-color')); $row->add($table, 12); @@ -434,6 +454,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { // DN attributes $sampleCSV_head[] = "\"dn_suffix\""; $sampleCSV_head[] = "\"dn_rdn\""; + $sampleCSV_head[] = "\"overwrite\""; // module attributes foreach ($modules as $moduleName) { if (sizeof($columns[$moduleName]) < 1) { @@ -447,6 +468,7 @@ function showMainPage(\LAM\TYPES\ConfiguredType $type, $selectedModules) { // DN attributes $sampleCSV_row[] = "\"" . $type->getSuffix() . "\""; $sampleCSV_row[] = "\"" . $RDNs[0] . "\""; + $sampleCSV_row[] = "\"false\""; // module attributes foreach ($modules as $moduleName) { if (sizeof($columns[$moduleName]) < 1) {