Skip to content

Instantly share code, notes, and snippets.

@tegud
Created December 22, 2014 18:38
Show Gist options
  • Save tegud/823d6b2d1d0c80d82e17 to your computer and use it in GitHub Desktop.
Save tegud/823d6b2d1d0c80d82e17 to your computer and use it in GitHub Desktop.
Untangle Importer

#Untangled importer

Input format

The script expects a CSV file called attendees.csv to be in the same directory, with the following Format:

  1. Last Name
  2. First Name
  3. Email address

e.g.

Elliott,Steve,[email protected]

The CSV parsing is extremely naive, if you have more complex input (commas in values), then you're probably best pulling in a csv parsing module.

Output

Default output is to attendeeUsers.json in the same directory. Password is 12 random characters by default.

var fs = require('fs');
var crypto = require('crypto');
function randomValueBase64 (len) {
return crypto.randomBytes(Math.ceil(len * 3 / 4))
.toString('base64') // convert to base64 format
.slice(0, len) // return required number of characters
.replace(/\+/g, '0') // replace '+' with '0'
.replace(/\//g, '0'); // replace '/' with '0'
}
var inputData = fs.readFileSync(__dirname + '/attendees.csv', 'utf-8');
var lines = inputData.split('\r\n');
var users = [];
for(var x = 0; x < lines.length; x++) {
if(!lines[x].length) { continue; }
var rows = lines[x].split(',');
var last = rows[0].replace(/ /g, '');
var first = rows[1].replace(/ /g, '');
var email = rows[2];
users.push({
username: (first[0] + last).toLowerCase(),
firstName: first,
lastName: last,
email: email,
password: randomValueBase64(12),
javaClass: "com.untangle.uvm.LocalDirectoryUser",
expirationTime: 0
});
}
fs.writeFileSync(__dirname + '/attendeeUsers.json', JSON.stringify(users));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment