mirror of
https://github.com/Ylianst/MeshCentral.git
synced 2024-12-26 23:25:53 -05:00
add user import via csv file (#5978)
Signed-off-by: si458 <simonsmith5521@gmail.com>
This commit is contained in:
parent
3be8ec5add
commit
d2a0946f22
File diff suppressed because it is too large
Load Diff
@ -15533,7 +15533,12 @@
|
||||
|
||||
function p4batchAccountCreate() {
|
||||
if (xxdialogMode) return;
|
||||
var x = "Create many accounts at once by importing a JSON file with the following format:" + '<br /><pre>[\r\n {"user":"x1","pass":"x","email":"x1@x"},\r\n {"user":"x2","pass":"x","resetNextLogin":true}\r\n]</pre><input style=width:370px type=file id=d4importFile accept=".json" onchange=p4batchAccountCreateValidate() />';
|
||||
var x = "Create many accounts at once by importing a JSON or CSV file" + '<br /><br />';
|
||||
x += "JSON file format is as follows:" + '<br />';
|
||||
x += '<pre>[\r\n {"user":"x1","pass":"x","email":"x1@x"},\r\n {"user":"x2","pass":"x","resetNextLogin":true}\r\n]</pre><br />';
|
||||
x += "CSV file format is as follows:" + '<br />';
|
||||
x += '<pre>user,pass,email,resetNextLogin\r\nx1,x,x1@x,\r\nx2,x,,true\r\n</pre><br />';
|
||||
x += '<input style=width:370px type=file id=d4importFile accept=".json,.csv" onchange=p4batchAccountCreateValidate() />';
|
||||
setDialogMode(2, "User Account Import", 3, p4batchAccountCreateEx, x);
|
||||
QE('idx_dlgOkButton', false);
|
||||
}
|
||||
@ -15546,17 +15551,60 @@
|
||||
var fr = new FileReader();
|
||||
fr.onload = function (r) {
|
||||
var j = null;
|
||||
try { j = JSON.parse(r.target.result); } catch (ex) { setDialogMode(2, "User Account Import", 1, null, format("Invalid JSON file: {0}.", ex)); return; }
|
||||
if ((j != null) && (Array.isArray(j))) {
|
||||
var ok = true;
|
||||
for (var i in j) {
|
||||
if ((typeof j[i].user != 'string') || (j[i].user.length < 1) || (j[i].user.length > 64)) { ok = false; }
|
||||
if ((typeof j[i].pass != 'string') || (j[i].pass.length < 1) || (j[i].pass.length > 256)) { ok = false; }
|
||||
if (checkPasswordRequirements(j[i].pass, passRequirements) == false) { ok = false; }
|
||||
if ((j[i].email != null) && ((typeof j[i].email != 'string') || (j[i].email.length < 1) || (j[i].email.length > 128))) { ok = false; }
|
||||
if (Q('d4importFile').value.endsWith('.csv')) {
|
||||
const csvData = r.target.result;
|
||||
const lines = csvData.split('\n');
|
||||
const headers = lines[0].trim().split(',');
|
||||
const jsonArray = [];
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
const currentLine = lines[i].trim();
|
||||
if (currentLine === "") continue; // Skip blank lines
|
||||
const lineArray = currentLine.split(',');
|
||||
// Check if any non-empty field exists
|
||||
let hasNonEmptyField = false;
|
||||
for (let j = 0; j < lineArray.length; j++) {
|
||||
if (lineArray[j].trim() !== "") {
|
||||
hasNonEmptyField = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no non-empty field exists, skip adding this object
|
||||
if (!hasNonEmptyField) continue;
|
||||
const obj = {};
|
||||
for (let j = 0; j < headers.length; j++) {
|
||||
// Only include columns with non-empty headers and non-empty values
|
||||
const value = lineArray[j].trim();
|
||||
if (headers[j].trim() !== "" && value !== "") {
|
||||
// Convert values "true" to true
|
||||
obj[headers[j]] = value.toLowerCase() === "true" ? true : value;
|
||||
}
|
||||
}
|
||||
jsonArray.push(obj);
|
||||
}
|
||||
if (ok == false) { setDialogMode(2, "User Account Import", 1, null, "Invalid JSON file format."); } else { meshserver.send({ action: 'adduserbatch', users: j }); }
|
||||
} else { setDialogMode(2, "User Account Import", 1, null, "Invalid JSON file format."); }
|
||||
j = jsonArray;
|
||||
if ((j != null) && (Array.isArray(j))) {
|
||||
var ok = true;
|
||||
for (var i in j) {
|
||||
if ((typeof j[i].user != 'string') || (j[i].user.length < 1) || (j[i].user.length > 64)) { ok = false; }
|
||||
if ((typeof j[i].pass != 'string') || (j[i].pass.length < 1) || (j[i].pass.length > 256)) { ok = false; }
|
||||
if (checkPasswordRequirements(j[i].pass, passRequirements) == false) { ok = false; }
|
||||
if ((j[i].email != null) && ((typeof j[i].email != 'string') || (j[i].email.length < 1) || (j[i].email.length > 128))) { ok = false; }
|
||||
}
|
||||
if (ok == false) { setDialogMode(2, "User Account Import", 1, null, "Invalid CSV file format."); } else { meshserver.send({ action: 'adduserbatch', users: j }); }
|
||||
} else { setDialogMode(2, "User Account Import", 1, null, "Invalid CSV file format."); }
|
||||
} else {
|
||||
try { j = JSON.parse(r.target.result); } catch (ex) { setDialogMode(2, "User Account Import", 1, null, format("Invalid JSON file: {0}.", ex)); return; }
|
||||
if ((j != null) && (Array.isArray(j))) {
|
||||
var ok = true;
|
||||
for (var i in j) {
|
||||
if ((typeof j[i].user != 'string') || (j[i].user.length < 1) || (j[i].user.length > 64)) { ok = false; }
|
||||
if ((typeof j[i].pass != 'string') || (j[i].pass.length < 1) || (j[i].pass.length > 256)) { ok = false; }
|
||||
if (checkPasswordRequirements(j[i].pass, passRequirements) == false) { ok = false; }
|
||||
if ((j[i].email != null) && ((typeof j[i].email != 'string') || (j[i].email.length < 1) || (j[i].email.length > 128))) { ok = false; }
|
||||
}
|
||||
if (ok == false) { setDialogMode(2, "User Account Import", 1, null, "Invalid JSON file format."); } else { meshserver.send({ action: 'adduserbatch', users: j }); }
|
||||
} else { setDialogMode(2, "User Account Import", 1, null, "Invalid JSON file format."); }
|
||||
}
|
||||
};
|
||||
fr.readAsText(Q('d4importFile').files[0]);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user