From aa487d612e08842a051674582447a82a707bc397 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Thu, 30 Jul 2020 17:33:33 +0200 Subject: [PATCH 1/7] add style for icons with class 'error' or 'success' --- styles/bootstrap/application.css | 3 +++ 1 file changed, 3 insertions(+) diff --git a/styles/bootstrap/application.css b/styles/bootstrap/application.css index c74824ddb..fe7a512ca 100644 --- a/styles/bootstrap/application.css +++ b/styles/bootstrap/application.css @@ -215,6 +215,9 @@ ul.jqtree-tree li.jqtree_common > .jqtree-element:hover { background-color: #E0E0E0; } +i.success {color: #00b000;} +i.error {color: #b00000;} + i.initstate {color: #ff9900;} i.released {color: #00b000;} i.rejected {color: #b00000;} From 25f37fae7a5d7eb5a8188f05d56b30f86b32dec7 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Thu, 30 Jul 2020 17:34:06 +0200 Subject: [PATCH 2/7] much more reliable import of users --- op/op.ImportUsers.php | 118 ++++++++++++++++++++------ views/bootstrap/class.ImportUsers.php | 36 +++++++- 2 files changed, 125 insertions(+), 29 deletions(-) diff --git a/op/op.ImportUsers.php b/op/op.ImportUsers.php index 210055c7d..c2c1e94de 100644 --- a/op/op.ImportUsers.php +++ b/op/op.ImportUsers.php @@ -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 "
";print_r($newusers);echo "
"; $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; } diff --git a/views/bootstrap/class.ImportUsers.php b/views/bootstrap/class.ImportUsers.php index b05ad6d54..fd0b036fe 100644 --- a/views/bootstrap/class.ImportUsers.php +++ b/views/bootstrap/class.ImportUsers.php @@ -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 "\n"; echo "
\n"; - if($log) { + if($newusers) { echo "\n"; - echo "\n"; - echo "\n"; + echo ""; + foreach($colmap as $col) { + echo "\n"; + } + echo ""; + echo "\n"; + echo ""; + foreach($newusers as $uhash=>$newuser) { + foreach($colmap as $i=>$coldata) { + echo "\n"; + } + echo ""; + echo "\n"; + } + echo "\n"; + /* foreach($log as $item) { $class = $item['type'] == 'success' ? 'success' : 'error'; echo "\n"; } + */ echo "
".getMLText('id')."".getMLText('message')."
".$col[2]."".getMLText('message')."
"; + echo call_user_func($colmap[$i][1], $colmap[$i][2], $newuser); + echo ""; + if(isset($newuser['__logs__'])) { + foreach($newuser['__logs__'] as $item) { + $class = $item['type'] == 'success' ? 'success' : 'error'; + echo " ".htmlspecialchars($item['msg'])."
"; + } + } + foreach($log[$uhash] as $item) { + $class = $item['type'] == 'success' ? 'success' : 'error'; + echo " ".htmlspecialchars($item['msg'])."
"; + } + echo "
".$item['id']."".htmlspecialchars($item['msg'])."
"; } echo "
\n"; From cd5c603f7a376c424b8b87d1c4f043cf3c6dff86 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Thu, 30 Jul 2020 17:35:03 +0200 Subject: [PATCH 3/7] add item for 5.1.19 --- CHANGELOG | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG b/CHANGELOG index d4b9bf453..ffbdcd9d5 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -3,6 +3,7 @@ -------------------------------------------------------------------------------- - add hooks showDocumentAttribute and showDocumentContentAttribute in Search view - fix layout problems of select2 menu, add option for adding an icon to each option +- better import for users from csv file -------------------------------------------------------------------------------- Changes in version 5.1.18 From 7c2959be02fb479249f149ce316263dfb73f6e33 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Sat, 1 Aug 2020 13:47:24 +0200 Subject: [PATCH 4/7] add fold marks --- SeedDMS_Core/Core/inc.ClassIterator.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/SeedDMS_Core/Core/inc.ClassIterator.php b/SeedDMS_Core/Core/inc.ClassIterator.php index 3dd89148e..ca6bdccbe 100644 --- a/SeedDMS_Core/Core/inc.ClassIterator.php +++ b/SeedDMS_Core/Core/inc.ClassIterator.php @@ -1,4 +1,6 @@ getName()."
"; } */ -class FolderFilterIterator extends FilterIterator { /* {{{ */ +class FolderFilterIterator extends \FilterIterator { /* {{{ */ public function __construct(Iterator $iterator , $filter ) { parent::__construct($iterator); $this->userFilter = $filter; @@ -196,7 +198,7 @@ class FolderFilterIterator extends FilterIterator { /* {{{ */ echo $ff->getID().': '.$ff->getName()."
"; } */ -class RecursiveFolderIterator extends FolderIterator implements RecursiveIterator { /* {{{ */ +class RecursiveFolderIterator extends FolderIterator implements \RecursiveIterator { /* {{{ */ public function hasChildren() { /* {{{ */ $db = $this->_dms->getDB(); From b6ac58eb3d50d21ed000676db1365549fb9f2afb Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Sat, 1 Aug 2020 13:47:49 +0200 Subject: [PATCH 5/7] add some documentation and new class SeedDMS_FolderTree --- inc/inc.Utils.php | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/inc/inc.Utils.php b/inc/inc.Utils.php index c9aa3625b..5a3d7c9a8 100644 --- a/inc/inc.Utils.php +++ b/inc/inc.Utils.php @@ -632,6 +632,18 @@ function seed_pass_verify($password, $hash) { /* {{{ */ return $hash == md5($password); } /* }}} */ +/** + * Class for creating encrypted api keys + * + * + * _encryptionKey); + * $kkk = $CSRF->create_api_key(); + * echo $kkk; + * echo $CSRF->check_api_key($kkk) ? 'valid' : 'invalid'; + * ?> + * + */ class SeedDMS_CSRF { /* {{{ */ protected $secret; @@ -679,7 +691,15 @@ class SeedDMS_CSRF { /* {{{ */ } /* }}} */ } /* }}} */ -//$CSRF = new SeedDMS_CSRF($settings->_encryptionKey); -//$kkk = $CSRF->create_api_key(); -//echo $kkk; -//echo $CSRF->check_api_key($kkk) ? 'valid' : 'invalid'; +class SeedDMS_FolderTree { /* {{{ */ + + public function __construct($folder, $callback) { /* {{{ */ + $iter = new \SeedDMS\RecursiveFolderIterator($folder); + $iter2 = new RecursiveIteratorIterator($iter, RecursiveIteratorIterator:: SELF_FIRST); + foreach($iter2 as $ff) { + call_user_func($callback, $ff); +// echo $ff->getID().': '.$ff->getFolderPathPlain().'-'.$ff->getName()."
"; + } + } /* }}} */ + +} /* }}} */ From 487950c37c2fa4baac20af9f7745394d27abdd60 Mon Sep 17 00:00:00 2001 From: Uwe Steinmann Date: Sat, 1 Aug 2020 13:49:10 +0200 Subject: [PATCH 6/7] use new class SeedDMS_FolderTree for iterating over all folders --- views/bootstrap/class.Indexer.php | 78 ++++++++++++++++++++++++++++++- 1 file changed, 77 insertions(+), 1 deletion(-) diff --git a/views/bootstrap/class.Indexer.php b/views/bootstrap/class.Indexer.php index a28cffd5f..e0934f963 100644 --- a/views/bootstrap/class.Indexer.php +++ b/views/bootstrap/class.Indexer.php @@ -18,6 +18,76 @@ */ require_once("class.Bootstrap.php"); +/** + * Class for processing a single folder + * + * SeedDMS_View_Indexer_Process_Folder::process() is used as a callable when + * iterating over all folders recursively. + */ +class SeedDMS_View_Indexer_Process_Folder { /* {{{ */ + protected $forceupdate; + + protected $index; + + protected $indexconf; + + public function __construct($index, $indexconf, $forceupdate) { /* {{{ */ + $this->index = $index; + $this->indexconf = $indexconf; + $this->forceupdate = $forceupdate; + } /* }}} */ + + public function process($folder) { /* {{{ */ + $documents = $folder->getDocuments(); + if($documents) { + echo "
".$folder->getFolderPathPlain()."
"; + foreach($documents as $document) { + echo "
".$document->getId().":".htmlspecialchars($document->getName()); + /* If the document wasn't indexed before then just add it */ + $lucenesearch = new $this->indexconf['Search']($this->index); + if(!($hit = $lucenesearch->getDocument($document->getId()))) { + echo " getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting').""; + /* + try { + $index->addDocument(new $indexconf['IndexedDocument']($dms, $document, $this->converters ? $this->converters : null, false, $this->timeout)); + echo "(document added)"; + } catch(Exception $e) { + echo $indent."(adding document failed '".$e->getMessage()."')"; + } + */ + } else { + /* Check if the attribute created is set or has a value older + * than the lastet content. Documents without such an attribute + * where added when a new document was added to the dms. In such + * a case the document content wasn't indexed. + */ + try { + $created = (int) $hit->getDocument()->getFieldValue('created'); + } catch (/* Zend_Search_Lucene_ */Exception $e) { + $created = 0; + } + $content = $document->getLatestContent(); + if($created >= $content->getDate() && !$this->forceupdate) { + echo "getID()."\" class=\"indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_document_unchanged').""; + } else { + $this->index->delete($hit->id); + echo " getID()."\" class=\"indexme indexstatus\" data-docid=\"".$document->getID()."\">".getMLText('index_waiting').""; + /* + try { + $index->addDocument(new $indexconf['IndexedDocument']($dms, $document, $this->converters ? $this->converters : null, false, $this->timeout)); + echo $indent."(document updated)"; + } catch(Exception $e) { + echo $indent."(updating document failed)"; + } + */ + } + } + echo "
"; + } + } + } /* }}} */ +} /* }}} */ + /** * Class which outputs the html page for Indexer view * @@ -205,6 +275,9 @@ $(document).ready( function() { ?>