mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-03-11 16:35:38 +00:00
much more reliable import of users
This commit is contained in:
parent
aa487d612e
commit
25f37fae7a
|
@ -31,11 +31,19 @@ function getBaseData($colname, $coldata, $objdata) { /* {{{ */
|
|||
return $objdata;
|
||||
} /* }}} */
|
||||
|
||||
function renderBaseData($colname, $objdata) { /* {{{ */
|
||||
return $objdata[$colname];
|
||||
} /* }}} */
|
||||
|
||||
function getPasswordPlainData($colname, $coldata, $objdata) { /* {{{ */
|
||||
$objdata['passenc'] = seed_pass_hash($coldata);
|
||||
return $objdata;
|
||||
} /* }}} */
|
||||
|
||||
function renderPasswordPlainData($colname, $objdata) { /* {{{ */
|
||||
return $objdata[$colname];
|
||||
} /* }}} */
|
||||
|
||||
function getQuotaData($colname, $coldata, $objdata) { /* {{{ */
|
||||
$objdata[$colname] = SeedDMS_Core_File::parse_filesize($coldata);
|
||||
return $objdata;
|
||||
|
@ -46,6 +54,9 @@ function getFolderData($colname, $coldata, $objdata) { /* {{{ */
|
|||
if($coldata) {
|
||||
if($folder = $dms->getFolder((int)$coldata)) {
|
||||
$objdata['homefolder'] = $folder;
|
||||
} else {
|
||||
$objdata['homefolder'] = null;
|
||||
$objdata['__logs__'][] = array('type'=>'error', 'msg'=> "No such folder with id '".(int) $coldata."'");
|
||||
}
|
||||
} else {
|
||||
$objdata['homefolder'] = null;
|
||||
|
@ -53,14 +64,30 @@ function getFolderData($colname, $coldata, $objdata) { /* {{{ */
|
|||
return $objdata;
|
||||
} /* }}} */
|
||||
|
||||
function renderFolderData($colname, $objdata) { /* {{{ */
|
||||
return is_object($objdata[$colname]) ? $objdata[$colname]->getName() : '';
|
||||
} /* }}} */
|
||||
|
||||
function getGroupData($colname, $coldata, $objdata) { /* {{{ */
|
||||
global $dms;
|
||||
if(!isset($objdata['groups']))
|
||||
$objdata['groups'] = [];
|
||||
if($group = $dms->getGroupByName($coldata)) {
|
||||
$objdata['groups'][] = $group;
|
||||
} else {
|
||||
$objdata['groups'] = [];
|
||||
$objdata['__logs__'][] = array('type'=>'error', 'msg'=> "No such group with name '".$coldata."'");
|
||||
}
|
||||
return $objdata;
|
||||
} /* }}} */
|
||||
|
||||
function renderGroupData($colname, $objdata) { /* {{{ */
|
||||
$html = '';
|
||||
foreach($objdata[$colname] as $g)
|
||||
$html .= $g->getName().';';
|
||||
return $html;
|
||||
} /* }}} */
|
||||
|
||||
function getRoleData($colname, $coldata, $objdata) { /* {{{ */
|
||||
switch($coldata) {
|
||||
case 'admin':
|
||||
|
@ -69,18 +96,28 @@ function getRoleData($colname, $coldata, $objdata) { /* {{{ */
|
|||
case 'guest':
|
||||
$role = 2;
|
||||
break;
|
||||
case 'user':
|
||||
$role = 0;
|
||||
break;
|
||||
default:
|
||||
$role = 0;
|
||||
$objdata['__logs__'][] = array('type'=>'error', 'msg'=> "No such role with name '".$coldata."'");
|
||||
}
|
||||
$objdata['role'] = $role;
|
||||
return $objdata;
|
||||
} /* }}} */
|
||||
|
||||
function renderRoleData($colname, $objdata) { /* {{{ */
|
||||
return ($objdata[$colname] == 1 ? 'admin' : ($objdata[$colname] == 2 ? 'guest' : 'user'));
|
||||
} /* }}} */
|
||||
|
||||
if (!$user->isAdmin()) {
|
||||
UI::exitError(getMLText("admin_tools"),getMLText("access_denied"));
|
||||
}
|
||||
|
||||
$log = array();
|
||||
$newusers = array();
|
||||
$csvheader = array();
|
||||
if (isset($_FILES['userdata']) && $_FILES['userdata']['error'] == 0) {
|
||||
if(!is_uploaded_file($_FILES["userdata"]["tmp_name"]))
|
||||
UI::exitError(getMLText("document_title", array("documentname" => $document->getName())),getMLText("error_occured"));
|
||||
|
@ -91,26 +128,37 @@ if (isset($_FILES['userdata']) && $_FILES['userdata']['error'] == 0) {
|
|||
$csvdelim = ';';
|
||||
$csvencl = '"';
|
||||
if($fp = fopen($_FILES['userdata']['tmp_name'], 'r')) {
|
||||
/* First of all build up a column map, which contains for each columen
|
||||
* the column name
|
||||
* (taken from the first line of the csv file), a function for getting
|
||||
* interpreting the data from the csv file and a function to return the
|
||||
* interpreted data as a string.
|
||||
* The column map will only contain entries for known column (whose head
|
||||
* line is one of 'login', 'email', 'name', 'role', 'homefolder', etc.)
|
||||
* Unknown columns will be skipped and the index in the column map will
|
||||
* be left out.
|
||||
*/
|
||||
$colmap = array();
|
||||
if($header = fgetcsv($fp, 0, $csvdelim, $csvencl)) {
|
||||
foreach($header as $i=>$colname) {
|
||||
if($csvheader = fgetcsv($fp, 0, $csvdelim, $csvencl)) {
|
||||
foreach($csvheader as $i=>$colname) {
|
||||
$colname = trim($colname);
|
||||
if(substr($colname, 0, 5) == 'group') {
|
||||
$colmap[$i] = array("getGroupData", $colname);
|
||||
$colmap[$i] = array("getGroupData", "renderGroupData", $colname);
|
||||
} elseif(in_array($colname, array('role'))) {
|
||||
$colmap[$i] = array("getRoleData", $colname);
|
||||
$colmap[$i] = array("getRoleData", "renderRoleData", $colname);
|
||||
} elseif(in_array($colname, array('homefolder'))) {
|
||||
$colmap[$i] = array("getFolderData", $colname);
|
||||
$colmap[$i] = array("getFolderData", "renderFolderData", $colname);
|
||||
} elseif(in_array($colname, array('quota'))) {
|
||||
$colmap[$i] = array("getQuotaData", $colname);
|
||||
$colmap[$i] = array("getQuotaData", "renderQuotaData", $colname);
|
||||
} elseif(in_array($colname, array('password'))) {
|
||||
$colmap[$i] = array("getPasswordPlainData", $colname);
|
||||
/* getPasswordPlainData() will set 'passenc' */
|
||||
$colmap[$i] = array("getPasswordPlainData", "renderPasswordPlainData", 'passenc');
|
||||
} elseif(in_array($colname, array('login', 'name', 'passenc', 'email', 'comment', 'group'))) {
|
||||
$colmap[$i] = array("getBaseData", $colname);
|
||||
$colmap[$i] = array("getBaseData", "renderBaseData", $colname);
|
||||
} elseif(substr($colname, 0, 5) == 'attr:') {
|
||||
$kk = explode(':', $colname, 2);
|
||||
if(($attrdef = $dms->getAttributeDefinitionByName($kk[1])) || ($attrdef = $dms->getAttributeDefinition((int) $kk[1]))) {
|
||||
$colmap[$i] = array("getAttributeData", $attrdef);
|
||||
$colmap[$i] = array("getAttributeData", "renderAttributeData", $attrdef);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -121,61 +169,71 @@ if (isset($_FILES['userdata']) && $_FILES['userdata']['error'] == 0) {
|
|||
$userids = array();
|
||||
foreach($allusers as $muser)
|
||||
$userids[$muser->getLogin()] = $muser;
|
||||
/* Run through all records in the csv file and fill $newusers.
|
||||
* $newusers will contain an associated array for each record, with
|
||||
* the key being the column name. The array may be shorter than
|
||||
* the number of columns, because $colmap may not contain a mapping
|
||||
* for each column.
|
||||
*/
|
||||
$newusers = array();
|
||||
while(!feof($fp)) {
|
||||
if($data = fgetcsv($fp, 0, $csvdelim, $csvencl)) {
|
||||
$md = array();
|
||||
foreach($data as $i=>$coldata) {
|
||||
/* First check if a column mapping exists. It could be missing
|
||||
* because the column has a not known header or it is missing.
|
||||
*/
|
||||
if(isset($colmap[$i])) {
|
||||
$md = call_user_func($colmap[$i][0], $colmap[$i][1], $coldata, $md);
|
||||
$md = call_user_func($colmap[$i][0], $colmap[$i][2], $coldata, $md);
|
||||
}
|
||||
}
|
||||
if($md)
|
||||
$newusers[] = $md;
|
||||
if($md && $md['login'])
|
||||
$newusers[$md['login']] = $md;
|
||||
}
|
||||
}
|
||||
// echo "<pre>";print_r($newusers);echo "</pre>";
|
||||
$makeupdate = !empty($_POST['update']);
|
||||
foreach($newusers as $u) {
|
||||
foreach($newusers as $uhash=>$u) {
|
||||
$log[$uhash] = [];
|
||||
if($eu = $dms->getUserByLogin($u['login'])) {
|
||||
if(isset($u['name']) && $u['name'] != $eu->getFullName()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Name of user updated. '".$u['name']."' != '".$eu->getFullName()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Name of user updated. '".$u['name']."' != '".$eu->getFullName()."'");
|
||||
if($makeupdate)
|
||||
$eu->setFullName($u['name']);
|
||||
}
|
||||
if(isset($u['email']) && $u['email'] != $eu->getEmail()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Email of user updated. '".$u['email']."' != '".$eu->getEmail()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Email of user updated. '".$u['email']."' != '".$eu->getEmail()."'");
|
||||
if($makeupdate)
|
||||
$eu->setEmail($u['email']);
|
||||
}
|
||||
if(isset($u['passenc']) && $u['passenc'] != $eu->getPwd()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Encrypted password of user updated. '".$u['passenc']."' != '".$eu->getPwd()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Encrypted password of user updated. '".$u['passenc']."' != '".$eu->getPwd()."'");
|
||||
if($makeupdate)
|
||||
$eu->setPwd($u['passenc']);
|
||||
}
|
||||
if(isset($u['comment']) && $u['comment'] != $eu->getComment()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Comment of user updated. '".$u['comment']."' != '".$eu->getComment()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Comment of user updated. '".$u['comment']."' != '".$eu->getComment()."'");
|
||||
if($makeupdate)
|
||||
$eu->setComment($u['comment']);
|
||||
}
|
||||
if(isset($u['language']) && $u['language'] != $eu->getLanguage()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Language of user updated. '".$u['language']."' != '".$eu->getLanguage()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Language of user updated. '".$u['language']."' != '".$eu->getLanguage()."'");
|
||||
if($makeupdate)
|
||||
$eu->setLanguage($u['language']);
|
||||
}
|
||||
if(isset($u['quota']) && $u['quota'] != $eu->getQuota()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Quota of user updated. '".$u['quota']."' != '".$eu->getQuota()."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Quota of user updated. '".$u['quota']."' != '".$eu->getQuota()."'");
|
||||
if($makeupdate)
|
||||
$eu->setQuota($u['language']);
|
||||
}
|
||||
if(isset($u['homefolder']) && $u['homefolder']->getId() != $eu->getHomeFolder()) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Homefolder of user updated. '".(is_object($u['homefolder']) ? $u['homefolder']->getId() : '')."' != '".($eu->getHomeFolder() ? $eu->getHomeFolder() : '')."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Homefolder of user updated. '".(is_object($u['homefolder']) ? $u['homefolder']->getId() : '')."' != '".($eu->getHomeFolder() ? $eu->getHomeFolder() : '')."'");
|
||||
if($makeupdate)
|
||||
$eu->setHomeFolder($u['homefolder']);
|
||||
}
|
||||
$func = function($o) {return $o->getID();};
|
||||
if(isset($u['groups']) && implode(',',array_map($func, $u['groups'])) != implode(',',array_map($func, $eu->getGroups()))) {
|
||||
$log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Groups of user updated. '".implode(',',array_map($func, $u['groups']))."' != '".implode(',',array_map($func, $eu->getGroups()))."'");
|
||||
$log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "Groups of user updated. '".implode(',',array_map($func, $u['groups']))."' != '".implode(',',array_map($func, $eu->getGroups()))."'");
|
||||
if($makeupdate) {
|
||||
foreach($eu->getGroups() as $g)
|
||||
$eu->leaveGroup($g);
|
||||
|
@ -183,15 +241,21 @@ if (isset($_FILES['userdata']) && $_FILES['userdata']['error'] == 0) {
|
|||
$eu->joinGroup($g);
|
||||
}
|
||||
}
|
||||
// $log[] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "User '".$eu->getLogin()."' updated.");
|
||||
// $log[$uhash][] = array('id'=>$eu->getLogin(), 'type'=>'success', 'msg'=> "User '".$eu->getLogin()."' updated.");
|
||||
} else {
|
||||
if(!empty($_POST['addnew'])) {
|
||||
if(!empty($u['login']) && !empty($u['name']) && !empty($u['email'])) {
|
||||
if(!empty($u['login']) && !empty($u['name']) && !empty($u['email'])) {
|
||||
if(!empty($_POST['addnew'])) {
|
||||
$ret = $dms->addUser($u['login'], '', $u['name'], $u['email'], !empty($u['language']) ? $u['language'] : 'en_GB', 'bootstrap', !empty($u['comment']) ? $u['comment'] : '', $u['role']);
|
||||
var_dump($ret);
|
||||
if($ret)
|
||||
$log[$uhash][] = array('id'=>$u['login'], 'type'=>'success', 'msg'=> "User '".$u['name']."' added.");
|
||||
else
|
||||
$log[$uhash][] = array('id'=>$u['login'], 'type'=>'error', 'msg'=> "User '".$u['name']."' could not be added.");
|
||||
} else {
|
||||
// $log[$uhash][] = array('id'=>$u['login'], 'type'=>'success', 'msg'=> "User '".$u['name']."' can be added.");
|
||||
}
|
||||
} else {
|
||||
$log[$uhash][] = array('id'=>$u['login'], 'type'=>'error', 'msg'=> "Too much data missing");
|
||||
}
|
||||
$log[] = array('id'=>$u['login'], 'type'=>'success', 'msg'=> "User '".$u['name']."' added.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -202,6 +266,8 @@ $tmp = explode('.', basename($_SERVER['SCRIPT_FILENAME']));
|
|||
$view = UI::factory($theme, $tmp[1], array('dms'=>$dms, 'user'=>$user));
|
||||
if($view) {
|
||||
$view->setParam('log', $log);
|
||||
$view->setParam('newusers', $newusers);
|
||||
$view->setParam('colmap', $colmap);
|
||||
$view($_GET);
|
||||
exit;
|
||||
}
|
||||
|
|
|
@ -41,6 +41,8 @@ class SeedDMS_View_ImportUsers extends SeedDMS_Bootstrap_Style {
|
|||
$dms = $this->params['dms'];
|
||||
$user = $this->params['user'];
|
||||
$log = $this->params['log'];
|
||||
$newusers = $this->params['newusers'];
|
||||
$colmap = $this->params['colmap'];
|
||||
|
||||
$this->htmlStartPage(getMLText("import_users"));
|
||||
$this->globalNavigation();
|
||||
|
@ -81,14 +83,42 @@ class SeedDMS_View_ImportUsers extends SeedDMS_Bootstrap_Style {
|
|||
|
||||
echo "</div>\n";
|
||||
echo "<div class=\"span8\">\n";
|
||||
if($log) {
|
||||
if($newusers) {
|
||||
echo "<table class=\"table table-condensed\">\n";
|
||||
echo "<tr><th>".getMLText('id')."</th>\n";
|
||||
echo "<th>".getMLText('message')."</th></tr>\n";
|
||||
echo "<tr>";
|
||||
foreach($colmap as $col) {
|
||||
echo "<th>".$col[2]."</th>\n";
|
||||
}
|
||||
echo "<th>".getMLText('message')."</th>";
|
||||
echo "</tr>\n";
|
||||
echo "<tr>";
|
||||
foreach($newusers as $uhash=>$newuser) {
|
||||
foreach($colmap as $i=>$coldata) {
|
||||
echo "<td>";
|
||||
echo call_user_func($colmap[$i][1], $colmap[$i][2], $newuser);
|
||||
echo "</td>\n";
|
||||
}
|
||||
echo "<td>";
|
||||
if(isset($newuser['__logs__'])) {
|
||||
foreach($newuser['__logs__'] as $item) {
|
||||
$class = $item['type'] == 'success' ? 'success' : 'error';
|
||||
echo "<i class=\"icon-circle ".$class."\"></i> ".htmlspecialchars($item['msg'])."<br />";
|
||||
}
|
||||
}
|
||||
foreach($log[$uhash] as $item) {
|
||||
$class = $item['type'] == 'success' ? 'success' : 'error';
|
||||
echo "<i class=\"icon-circle ".$class."\"></i> ".htmlspecialchars($item['msg'])."<br />";
|
||||
}
|
||||
echo "</td>";
|
||||
echo "</tr>\n";
|
||||
}
|
||||
echo "</tr>\n";
|
||||
/*
|
||||
foreach($log as $item) {
|
||||
$class = $item['type'] == 'success' ? 'success' : 'error';
|
||||
echo "<tr class=\"".$class."\"><td>".$item['id']."</td><td>".htmlspecialchars($item['msg'])."</td></tr>\n";
|
||||
}
|
||||
*/
|
||||
echo "</table>";
|
||||
}
|
||||
echo "</div>\n";
|
||||
|
|
Loading…
Reference in New Issue
Block a user