mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 04:11:26 +08:00
450 lines
9.4 KiB
JavaScript
Executable File
450 lines
9.4 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
/**
|
|
* Setup the debug paramater.
|
|
*/
|
|
|
|
process.env.DEBUG = process.env.TALK_DEBUG;
|
|
|
|
/**
|
|
* Module dependencies.
|
|
*/
|
|
|
|
const program = require('commander');
|
|
const pkg = require('../package.json');
|
|
const prompt = require('prompt');
|
|
const User = require('../models/user');
|
|
const mongoose = require('../services/mongoose');
|
|
const util = require('../util');
|
|
const Table = require('cli-table');
|
|
|
|
// Regeister the shutdown criteria.
|
|
util.onshutdown([
|
|
() => mongoose.disconnect()
|
|
]);
|
|
|
|
/**
|
|
* Prompts for input and registers a user based on those.
|
|
*/
|
|
function createUser(options) {
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (options.flag_mode) {
|
|
return resolve({
|
|
email: options.email,
|
|
password: options.password,
|
|
displayName: options.name,
|
|
role: options.role
|
|
});
|
|
}
|
|
|
|
prompt.start();
|
|
|
|
prompt.get([
|
|
{
|
|
name: 'email',
|
|
description: 'Email',
|
|
format: 'email',
|
|
required: true
|
|
},
|
|
{
|
|
name: 'password',
|
|
description: 'Password',
|
|
hidden: true,
|
|
required: true
|
|
},
|
|
{
|
|
name: 'confirmPassword',
|
|
description: 'Confirm Password',
|
|
hidden: true,
|
|
required: true
|
|
},
|
|
{
|
|
name: 'displayName',
|
|
description: 'Display Name',
|
|
required: true
|
|
},
|
|
{
|
|
name: 'role',
|
|
description: 'User Role',
|
|
required: false
|
|
}
|
|
], (err, result) => {
|
|
if (err) {
|
|
return reject(err);
|
|
}
|
|
|
|
if (result.password !== result.confirmPassword) {
|
|
return reject(new Error('Passwords do not match'));
|
|
}
|
|
|
|
resolve(result);
|
|
});
|
|
})
|
|
.then((result) => {
|
|
return User.createLocalUser(result.email.trim(), result.password.trim(), result.displayName.trim())
|
|
.then((user) => {
|
|
console.log(`Created user ${user.id}.`);
|
|
|
|
return User
|
|
.addRoleToUser(user.id, result.role.trim())
|
|
.then(() => {
|
|
console.log(`Added the admin ${result.role.trim()} to User ${user.id}.`);
|
|
util.shutdown();
|
|
});
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown();
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Deletes a user.
|
|
*/
|
|
function deleteUser(userID) {
|
|
User
|
|
.findOneAndRemove({
|
|
id: userID
|
|
})
|
|
.then(() => {
|
|
console.log('Deleted user');
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Changes the password for a user.
|
|
*/
|
|
function passwd(userID) {
|
|
prompt.start();
|
|
|
|
prompt.get([
|
|
{
|
|
name: 'password',
|
|
description: 'Password',
|
|
hidden: true,
|
|
required: true
|
|
},
|
|
{
|
|
name: 'confirmPassword',
|
|
description: 'Confirm Password',
|
|
hidden: true,
|
|
required: true
|
|
}
|
|
], (err, result) => {
|
|
if (err) {
|
|
console.error(err);
|
|
util.shutdown();
|
|
return;
|
|
}
|
|
|
|
if (result.password !== result.confirmPassword) {
|
|
console.error(new Error('Password mismatch'));
|
|
util.shutdown(1);
|
|
return;
|
|
}
|
|
|
|
User
|
|
.changePassword(userID, result.password)
|
|
.then(() => {
|
|
console.log('Password changed.');
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Updates the user from the options array.
|
|
*/
|
|
function updateUser(userID, options) {
|
|
const updates = [];
|
|
|
|
if (options.email && typeof options.email === 'string' && options.email.length > 0) {
|
|
let q = User.update({
|
|
'id': userID,
|
|
'profiles.provider': 'local'
|
|
}, {
|
|
$set: {
|
|
'profiles.$.id': options.email
|
|
}
|
|
});
|
|
|
|
updates.push(q);
|
|
}
|
|
|
|
if (options.name && typeof options.name === 'string' && options.name.length > 0) {
|
|
let q = User.update({
|
|
'id': userID
|
|
}, {
|
|
$set: {
|
|
displayName: options.name
|
|
}
|
|
});
|
|
|
|
updates.push(q);
|
|
}
|
|
|
|
Promise
|
|
.all(updates.map((q) => q.exec()))
|
|
.then(() => {
|
|
console.log(`User ${userID} updated.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Lists all the users registered in the database.
|
|
*/
|
|
function listUsers() {
|
|
User
|
|
.all()
|
|
.then((users) => {
|
|
let table = new Table({
|
|
head: [
|
|
'ID',
|
|
'Display Name',
|
|
'Profiles',
|
|
'Roles',
|
|
'Status',
|
|
'State'
|
|
]
|
|
});
|
|
|
|
users.forEach((user) => {
|
|
table.push([
|
|
user.id,
|
|
user.displayName,
|
|
user.profiles.map((p) => p.provider).join(', '),
|
|
user.roles.join(', '),
|
|
user.status,
|
|
user.disabled ? 'Disabled' : 'Enabled'
|
|
]);
|
|
});
|
|
|
|
console.log(table.toString());
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Merges two users using the specified ID's.
|
|
* @param {String} dstUserID id of the user to which is the target of the merge
|
|
* @param {String} srcUserID id of the user to which is the source of the merge
|
|
*/
|
|
function mergeUsers(dstUserID, srcUserID) {
|
|
User
|
|
.mergeUsers(dstUserID, srcUserID)
|
|
.then(() => {
|
|
console.log(`User ${srcUserID} was merged into user ${dstUserID}.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Adds a role to a user
|
|
* @param {String} userUD id of the user to add the role to
|
|
* @param {String} role the role to add
|
|
*/
|
|
function addRole(userID, role) {
|
|
User
|
|
.addRoleToUser(userID, role)
|
|
.then(() => {
|
|
console.log(`Added the ${role} role to User ${userID}.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Removes a role from a user
|
|
* @param {String} userUD id of the user to remove the role from
|
|
* @param {String} role the role to remove
|
|
*/
|
|
function removeRole(userID, role) {
|
|
User
|
|
.removeRoleFromUser(userID, role)
|
|
.then(() => {
|
|
console.log(`Removed the ${role} role from User ${userID}.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Ban a user
|
|
* @param {String} userID id of the user to ban
|
|
*/
|
|
function ban(userID) {
|
|
User
|
|
.setStatus(userID, 'banned', '')
|
|
.then(() => {
|
|
console.log(`Banned the User ${userID}.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Unban a user
|
|
* @param {String} userUD id of the user to remove the role from
|
|
*/
|
|
function unban(userID) {
|
|
User
|
|
.setStatus(userID, 'active', '')
|
|
.then(() => {
|
|
console.log(`Unban the User ${userID}.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Disable a given user.
|
|
* @param {String} userID the ID of a user to disable
|
|
*/
|
|
function disableUser(userID) {
|
|
User
|
|
.disableUser(userID)
|
|
.then(() => {
|
|
console.log(`User ${userID} was disabled.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Enabled a given user.
|
|
* @param {String} userID the ID of a user to enable
|
|
*/
|
|
function enableUser(userID) {
|
|
User
|
|
.enableUser(userID)
|
|
.then(() => {
|
|
console.log(`User ${userID} was enabled.`);
|
|
util.shutdown();
|
|
})
|
|
.catch((err) => {
|
|
console.error(err);
|
|
util.shutdown(1);
|
|
});
|
|
}
|
|
|
|
//==============================================================================
|
|
// Setting up the program command line arguments.
|
|
//==============================================================================
|
|
|
|
program
|
|
.version(pkg.version);
|
|
|
|
program
|
|
.command('create')
|
|
.option('--email [email]', 'Email to use')
|
|
.option('--password [password]', 'Password to use')
|
|
.option('--name [name]', 'Name to use')
|
|
.option('--role [role]', 'Role to add')
|
|
.option('-f, --flag_mode', 'Source from flags instead of prompting')
|
|
.description('create a new user')
|
|
.action(createUser);
|
|
|
|
program
|
|
.command('delete <userID>')
|
|
.description('delete a user')
|
|
.action(deleteUser);
|
|
|
|
program
|
|
.command('passwd <userID>')
|
|
.description('change a password for a user')
|
|
.action(passwd);
|
|
|
|
program
|
|
.command('update <userID>')
|
|
.option('--email [email]', 'Email to use')
|
|
.option('--name [name]', 'Name to use')
|
|
.description('update a user')
|
|
.action(updateUser);
|
|
|
|
program
|
|
.command('list')
|
|
.description('list all the users in the database')
|
|
.action(listUsers);
|
|
|
|
program
|
|
.command('merge <dstUserID> <srcUserID>')
|
|
.description('merge srcUser into the dstUser')
|
|
.action(mergeUsers);
|
|
|
|
program
|
|
.command('addrole <userID> <role>')
|
|
.description('adds a role to a given user')
|
|
.action(addRole);
|
|
|
|
program
|
|
.command('removerole <userID> <role>')
|
|
.description('removes a role from a given user')
|
|
.action(removeRole);
|
|
|
|
program
|
|
.command('ban <userID>')
|
|
.description('ban a given user')
|
|
.action(ban);
|
|
|
|
program
|
|
.command('uban <userID>')
|
|
.description('unban a given user')
|
|
.action(unban);
|
|
|
|
program
|
|
.command('disable <userID>')
|
|
.description('disable a given user from logging in')
|
|
.action(disableUser);
|
|
|
|
program
|
|
.command('enable <userID>')
|
|
.description('enable a given user from logging in')
|
|
.action(enableUser);
|
|
|
|
program.parse(process.argv);
|
|
|
|
// If there is no command listed, output help.
|
|
if (!process.argv.slice(2).length) {
|
|
program.outputHelp();
|
|
util.shutdown();
|
|
}
|