Created
March 17, 2021 22:23
-
-
Save mrsimpson/867d4fc2f485e794112571ceb35bef9d to your computer and use it in GitHub Desktop.
Mass creation of Rocket.Chat users
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generatePassword() { | |
const groups = ["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", | |
"0123456789", | |
"!§$%&/()=?*+-"]; | |
return [12, 2, 1] | |
.map(function (len, i) { | |
return Array(len) | |
.fill(groups[i]).map(function (x) { | |
return x[Math.floor(Math.random() * x.length)] | |
}).join('') | |
}) | |
.concat().join('').split('') | |
.sort(function () { return 0.5 - Math.random() }) | |
.join(''); | |
} | |
function userGenerator(baseUrl, authUser, authToken) { | |
async function createRocketChatUser({ LastName, FirstName, Station, EmailAddress, AssistifyID }) { | |
function translateName(name) { | |
return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase(); | |
} | |
const response = await fetch(baseUrl + "/api/v1/users.create", | |
{ | |
method: "POST", | |
headers: { | |
'Content-Type': 'application/json', | |
'X-User-Id': authUser, | |
'X-Auth-Token': authToken, | |
}, | |
body: JSON.stringify({ | |
email: EmailAddress, | |
name: `${FirstName} ${translateName(LastName)}`, | |
password: generatePassword(), | |
username: AssistifyID, | |
roles: ['eurocargo'], | |
joinDefaultChannels: true, | |
requirePasswordChange: true, | |
sendWelcomeEmail: false, | |
customFields: { Station } | |
}) | |
}) | |
const result = await response.json() | |
if(!response.ok || !result){ | |
console.error(`Could not create ${AssistifyID}:`, result) | |
} | |
} | |
return { | |
generate: async function (users) { | |
if(users && users.length) users.forEach(user => createRocketChatUser(user)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment