From: hickert Date: Mon, 27 Jun 2011 12:09:58 +0000 (+0000) Subject: Updated csv import X-Git-Url: https://git.tokkee.org/?p=gosa.git;a=commitdiff_plain;h=0b47986de90eb8639bfd76cd77ebb6d9ed01b3c0 Updated csv import -Allow to leave uid blank for csv imports git-svn-id: https://oss.gonicus.de/repositories/gosa/branches/2.6@20919 594d385d-05f5-0310-b6e9-bd551577e9d8 --- diff --git a/gosa-plugins/ldapmanager/addons/ldapmanager/class_csvimport.inc b/gosa-plugins/ldapmanager/addons/ldapmanager/class_csvimport.inc index fb0c41db2..c56d6b245 100644 --- a/gosa-plugins/ldapmanager/addons/ldapmanager/class_csvimport.inc +++ b/gosa-plugins/ldapmanager/addons/ldapmanager/class_csvimport.inc @@ -158,63 +158,82 @@ class csvimport extends plugin $dat = $this->csvinfo['sorteddata'][$i]; /* If not all needed vars set .... */ - if(( empty($dat['sn']))||( empty($dat['givenName']))||( empty($dat['uid']))|| - (!isset($dat['sn']))||(!isset($dat['givenName']))||(!isset($dat['uid']))){ + if(!isset($dat['givenName']) || empty($dat['givenName']) || !isset($dat['sn']) || empty($dat['sn'])){ /* Output Error */ - msg_dialog::display(_("Error"), _("Need at least 'sn', 'givenName' and 'uid' to create users."), ERROR_DIALOG); - } else { + msg_dialog::display(_("Error"), _("Need at least 'sn' and 'givenName' to create users!"), ERROR_DIALOG); + $this->csvinfo['sorteddata'][$i]['status']=""._("failed").""; + $failing = $i+1; + continue; + } + + /* Automatically generate 'uid' while it wasn't given yet. + */ + if(!isset($dat['uid']) || empty($dat['uid'])){ + + // Generate uids and take the first result. + $attributes= array('sn' => $dat['sn'], 'givenName' => $dat['givenName']); + $uids = gen_uids ($this->config->get_cfg_value("idGenerator"), $attributes); + if (!count($uids) || empty($uids[0])){ + $msgs = sprintf(_("Failed to generate 'uid' for entry '%s'!"), ($i+1)); + $this->csvinfo['sorteddata'][$i]['status']=""._("failed")." ". $msgs; + $failing = $i+1; + continue; + }else{ + $this->csvinfo['sorteddata'][$i]['uid'] = array_pop($uids); + $dat['uid'] = $this->csvinfo['sorteddata'][$i]['uid']; + } + } - /* Register usertab to trigger edit dialog */ - $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], 'new'); - $keys = array_keys($dat); - - /* Set all submitted vars */ - if(is_array($keys)){ - foreach($keys as $key) { - if($key != "status"){ - $this->usertab->$key = $dat[$key]; - foreach($this->usertab->by_object as $pname => $plugin){ - if(isset($this->usertab->by_object[$pname]->$key)){ - $this->usertab->by_object[$pname]->$key = $dat[$key]; - } + /* Register usertab to trigger edit dialog */ + $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], 'new'); + $keys = array_keys($dat); + + /* Set all submitted vars */ + if(is_array($keys)){ + foreach($keys as $key) { + if($key != "status"){ + $this->usertab->$key = $dat[$key]; + foreach($this->usertab->by_object as $pname => $plugin){ + if(isset($this->usertab->by_object[$pname]->$key)){ + $this->usertab->by_object[$pname]->$key = $dat[$key]; } } } } + } - // Adapt template values. - if($this->csvinfo['tplid']!=0){ - $tpl_dn = $this->csvinfo['arrtempls'][$this->csvinfo['tplid']]['dn']; - $this->usertab->adapt_from_template($tpl_dn,array("uid","cn","givenName","sn")); - $tpl_base = preg_replace("/^[^,]+,".preg_quote(get_people_ou(), '/i')."/", '', $tpl_dn); - $this->usertab->by_object['user']->base= $tpl_base; - } - - // Setting user Password - if((isset($dat['userPassword']))&&(!empty($dat['userPassword']))){ - $password = $dat['userPassword']; - }else{ - $rand_str=""; - $feed = "0123456789abcdefghijklmnopqrstuvwxyz"; - for ($e=0; $e < 8; $e++) - { - $rand_str .= substr($feed, rand(0, strlen($feed)-1), 1); - } - $password = $rand_str; + // Adapt template values. + if($this->csvinfo['tplid']!=0){ + $tpl_dn = $this->csvinfo['arrtempls'][$this->csvinfo['tplid']]['dn']; + $this->usertab->adapt_from_template($tpl_dn,array("uid","cn","givenName","sn")); + $tpl_base = preg_replace("/^[^,]+,".preg_quote(get_people_ou(), '/i')."/", '', $tpl_dn); + $this->usertab->by_object['user']->base= $tpl_base; + } + + // Setting user Password + if((isset($dat['userPassword']))&&(!empty($dat['userPassword']))){ + $password = $dat['userPassword']; + }else{ + $rand_str=""; + $feed = "0123456789abcdefghijklmnopqrstuvwxyz"; + for ($e=0; $e < 8; $e++) + { + $rand_str .= substr($feed, rand(0, strlen($feed)-1), 1); } + $password = $rand_str; + } - /* Insert in the LDAP tree */ - if(count($this->usertab->check())) { - msg_dialog::displayChecks($this->usertab->check()); - $this->csvinfo['sorteddata'][$i]['status']=""._("failed").""; - $failing = $i+1; - break; - } else { - $this->csvinfo['sorteddata'][$i]['status']=_("ok"); - $this->usertab->save(); - change_password($this->usertab->dn,$password); - } + /* Insert in the LDAP tree */ + if(count($this->usertab->check())) { + msg_dialog::displayChecks($this->usertab->check()); + $this->csvinfo['sorteddata'][$i]['status']=""._("failed").""; + $failing = $i+1; + break; + } else { + $this->csvinfo['sorteddata'][$i]['status']=_("ok"); + $this->usertab->save(); + change_password($this->usertab->dn,$password); } } @@ -266,7 +285,7 @@ class csvimport extends plugin if($this->csvinfo['shownrowscount']> $this->csvinfo['rows'])$this->csvinfo['shownrowscount']=$this->csvinfo['rows']; $dataouts = array(); - for($i =$begin; $i <= $end; $i++) { + for($i =$begin; ($i <= $end) && (isset($this->csvinfo['sorteddata'][($i)])); $i++) { if(is_array($this->csvinfo['sorteddata'][($i)])){ foreach($this->csvinfo['sorteddata'][($i)] as $dat){ $dataouts[]=$dat; diff --git a/gosa-plugins/ldapmanager/addons/ldapmanager/contentcsv.tpl b/gosa-plugins/ldapmanager/addons/ldapmanager/contentcsv.tpl index b89ba7ca3..5ee7c7aab 100644 --- a/gosa-plugins/ldapmanager/addons/ldapmanager/contentcsv.tpl +++ b/gosa-plugins/ldapmanager/addons/ldapmanager/contentcsv.tpl @@ -1,5 +1,5 @@

- {t}The CSV import plugin provides methods to generate user accounts from a file containing Comma Seperated Values. The administrator can decide which columns should be transfered to which attribute. Note that you must have at least the UID, GIVENNAME and SURNAME set.{/t} + {t}The CSV import plugin provides methods to generate user accounts from a file containing Comma Seperated Values. The administrator can decide which columns should be transfered to which attribute. Note that you must have at least the GIVENNAME and SURNAME set.{/t}