@rws-air/usercreator
Version:
Script to add users to usermanagement environments
134 lines (133 loc) • 4.65 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.exec = exports.UsermanagementAdder = void 0;
const yamlreader_1 = require("@favware/yamlreader");
const node_fetch_1 = __importDefault(require("node-fetch"));
const path_1 = __importDefault(require("path"));
const yargs_1 = __importDefault(require("yargs"));
class UsermanagementAdder {
/**
* Constructs a new {@link UsermanagementAdder}
* @param file The YAML file of userdata to read
* @param user The admin user name to create users with
* @param pass The password for the admin user
* @param url The URL of the usermanagement backend
*/
constructor(file, user, pass, url) {
this.file = file;
this.username = user;
this.password = pass;
this.url = url;
}
/**
* Runs the {@link UsermanagementAdder}
*/
async run() {
const users = (0, yamlreader_1.readYaml)(this.file);
for (const user of users)
await this.addUserToAPI(user);
}
async addUserToAPI(user) {
try {
const response = await (0, node_fetch_1.default)(`${this.url}/api/v1/users`, {
body: JSON.stringify({
email: user.email,
firstName: user.firstname,
lastName: user.lastname,
middleName: user.middlename ? user.middlename : ''
}),
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`
}
});
const data = await response.json();
console.log(`Added user with email ${user.email}, they got ID ${data.id}`);
for (const role of user.roles)
await this.addRoleToUser(data.id, role);
}
catch (err) {
console.error(err);
}
}
async addRoleToUser(userId, role) {
console.log(`Adding ${role} to user with ID ${userId}`);
try {
await (0, node_fetch_1.default)(`${this.url}/api/v1/users/${userId}/roles`, {
body: JSON.stringify({ name: role }),
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Basic ${Buffer.from(`${this.username}:${this.password}`).toString('base64')}`
}
});
console.log(`Added role ${role} to user with id ${userId}`);
console.log('');
}
catch (err) {
console.error(`an error occurred adding ${role} to user with ID ${userId}. Stacktrace:`);
console.debug(err);
}
}
}
exports.UsermanagementAdder = UsermanagementAdder;
/**
* Executes the usermanagement usercreator
* Uses yargs to parse arguments
*/
const exec = () => {
const argv = yargs_1.default
.usage([
'Usermanagement user adding script',
'',
'\tUsage:',
'\t\tusercreator -f /path/to/yaml/file -u <username> -p <password> --url <url>',
'\t\tusercreator --help'
].join(''))
.example('', 'usercreator -f users.yaml -u root -p root --url http://localhost:9003')
.option('file', {
alias: 'f',
describe: 'The YAML file to use as users list',
type: 'string',
demand: true
})
.option('username', {
alias: 'u',
describe: 'The username to use as "root" user when uploading',
type: 'string',
demand: true
})
.option('password', {
alias: 'p',
describe: 'The password to use as "root" user when uploading',
type: 'string',
demand: true
})
.option('url', {
alias: 's',
describe: 'The url to upload to',
type: 'string',
demand: true
}).argv;
if (argv instanceof Promise) {
console.error('argv is of type Promise which is not supported yet');
}
else {
if (argv.url.slice(-1) === '/')
argv.url = argv.url.slice(0, -1);
try {
new UsermanagementAdder(path_1.default.resolve(argv.file), argv.username, argv.password, argv.url).run();
}
catch (err) {
console.error(err);
}
}
};
exports.exec = exec;
(0, exports.exec)();
exports.default = exports.exec;