diff --git a/bin/cli b/bin/cli new file mode 100755 index 000000000..bec5ea84f --- /dev/null +++ b/bin/cli @@ -0,0 +1,29 @@ +#!/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'); + +//============================================================================== +// Setting up the program command line arguments. +//============================================================================== + +program + .version(pkg.version) + .command('settings', 'work with the application settings') + .command('users', 'work with the application auth') + .parse(process.argv); + +// If there is no command listed, output help. +if (!process.argv.slice(2).length) { + program.outputHelp(); +} diff --git a/bin/cli-settings b/bin/cli-settings new file mode 100755 index 000000000..0920473da --- /dev/null +++ b/bin/cli-settings @@ -0,0 +1,42 @@ +#!/usr/bin/env node + +/** + * Setup the debug paramater. + */ + +process.env.DEBUG = process.env.TALK_DEBUG; + +/** + * Module dependencies. + */ + +const program = require('commander'); + +//============================================================================== +// Setting up the program command line arguments. +//============================================================================== + +program + .command('init') + .description('initilizes the talk settings') + .action(() => { + const mongoose = require('../mongoose'); + const Setting = require('../models/setting'); + const defaults = {id: '1', moderation: 'pre'}; + + Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) + .then(() => { + console.log('Created settings object.'); + mongoose.disconnect(); + }).catch((err) => { + console.error(`failed to create the settings object ${JSON.stringify(err)}`); + throw new Error(err); // just to be safe + }); + }); + +program.parse(process.argv); + +// If there is no command listed, output help. +if (!process.argv.slice(2).length) { + program.outputHelp(); +} diff --git a/bin/cli-users b/bin/cli-users new file mode 100755 index 000000000..74aa902eb --- /dev/null +++ b/bin/cli-users @@ -0,0 +1,408 @@ +#!/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'); + +/** + * Prompts for input and registers a user based on those. + */ +function createUser(options) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + return new Promise((resolve, reject) => { + + if (options.flag_mode) { + return resolve({ + email: options.email, + password: options.password, + displayName: options.name, + }); + } + + 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 + } + ], (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}.`); + mongoose.disconnect(); + }).catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * Deletes a user. + */ +function deleteUser(userID) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .findOneAndRemove({ + id: userID + }) + .then(() => { + console.log('Deleted user'); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * Changes the password for a user. + */ +function passwd(userID) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + 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); + mongoose.disconnect(); + return; + } + + if (result.password !== result.confirmPassword) { + console.error(new Error('Password mismatch')); + mongoose.disconnect(); + return; + } + + User + .changePassword(userID, result.password) + .then(() => { + console.log('Password changed.'); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); + }); +} + +/** + * Updates the user from the options array. + */ +function updateUser(userID, options) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + 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.`); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * Lists all the users registered in the database. + */ +function listUsers() { + const Table = require('cli-table'); + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .find() + .then((users) => { + let table = new Table({ + head: [ + 'ID', + 'Display Name', + 'Profiles', + 'Roles', + 'State' + ] + }); + + users.forEach((user) => { + table.push([ + user.id, + user.displayName, + user.profiles.map((p) => p.provider).join(', '), + user.roles.join(', '), + user.disabled ? 'Disabled' : 'Enabled' + ]); + }); + + console.log(table.toString()); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * 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) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .mergeUsers(dstUserID, srcUserID) + .then(() => { + console.log(`User ${srcUserID} was merged into user ${dstUserID}.`); + mongoose.disconnect(); + }).catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * 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) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .addRoleToUser(userID, role) + .then(() => { + console.log(`Added the ${role} role to User ${userID}.`); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * 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) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .removeRoleFromUser(userID, role) + .then(() => { + console.log(`Removed the ${role} role from User ${userID}.`); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * Disable a given user. + * @param {String} userID the ID of a user to disable + */ +function disableUser(userID) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .disableUser(userID) + .then(() => { + console.log(`User ${userID} was disabled.`); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +/** + * Enabled a given user. + * @param {String} userID the ID of a user to enable + */ +function enableUser(userID) { + const User = require('../models/user'); + const mongoose = require('../mongoose'); + + User + .enableUser(userID) + .then(() => { + console.log(`User ${userID} was enabled.`); + mongoose.disconnect(); + }) + .catch((err) => { + console.error(err); + mongoose.disconnect(); + }); +} + +//============================================================================== +// 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('-f, --flag_mode', 'Source from flags instead of prompting') + .description('create a new user') + .action(createUser); + +program + .command('delete ') + .description('delete a user') + .action(deleteUser); + +program + .command('passwd ') + .description('change a password for a user') + .action(passwd); + +program + .command('update ') + .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 ') + .description('merge srcUser into the dstUser') + .action(mergeUsers); + +program + .command('addrole ') + .description('adds a role to a given user') + .action(addRole); + +program + .command('removerole ') + .description('removes a role from a given user') + .action(removeRole); + +program + .command('disable ') + .description('disable a given user from logging in') + .action(disableUser); + +program + .command('enable ') + .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(); +} diff --git a/bin/init.js b/bin/init.js deleted file mode 100644 index d8be5a6fa..000000000 --- a/bin/init.js +++ /dev/null @@ -1,12 +0,0 @@ -const mongoose = require('../mongoose'); -const Setting = require('../models/setting'); -const defaults = {id: '1', moderation: 'pre'}; - -Setting.update({id: '1'}, {$setOnInsert: defaults}, {upsert: true}) - .then(() => { - console.log('Created settings object.'); - mongoose.disconnect(); - }).catch((err) => { - console.error(`failed to create the settings object ${JSON.stringify(err)}`); - throw new Error(err); // just to be safe - }); diff --git a/bin/www b/bin/www index 94408f5e8..af91c2c40 100755 --- a/bin/www +++ b/bin/www @@ -40,7 +40,7 @@ server.on('listening', onListening); */ function normalizePort(val) { - var port = parseInt(val, 10); + let port = parseInt(val, 10); if (isNaN(port)) { // named pipe @@ -64,23 +64,21 @@ function onError(error) { throw error; } - var bind = typeof port === 'string' - ? 'Pipe ' + port - : 'Port ' + port; + let bind = typeof port === 'string' + ? `Pipe ${ port}` + : `Port ${ port}`; // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': - console.error(bind + ' requires elevated privileges'); - process.exit(1); + console.error(`${bind} requires elevated privileges`); break; case 'EADDRINUSE': - console.error(bind + ' is already in use'); - process.exit(1); + console.error(`${bind} is already in use`); break; - default: - throw error; } + + throw error; } /** @@ -88,9 +86,9 @@ function onError(error) { */ function onListening() { - var addr = server.address(); - var bind = typeof addr === 'string' - ? 'pipe ' + addr - : 'port ' + addr.port; - debug('Listening on ' + bind); + let addr = server.address(); + let bind = typeof addr === 'string' + ? `pipe ${ addr}` + : `port ${ addr.port}`; + debug(`Listening on ${ bind}`); } diff --git a/models/user.js b/models/user.js index 4592ec847..fe06b9ab2 100644 --- a/models/user.js +++ b/models/user.js @@ -1,45 +1,278 @@ - const mongoose = require('../mongoose'); const uuid = require('uuid'); -const Schema = mongoose.Schema; +const bcrypt = require('bcrypt'); -const UserProfileSchema = new Schema({ +const SALT_ROUNDS = 10; + +const UserSchema = new mongoose.Schema({ id: { type: String, default: uuid.v4, unique: true }, - display_name: String, - auth_user_id: String + displayName: String, + disabled: Boolean, + password: String, + profiles: [{ + id: String, + provider: String + }], + roles: [String] +}); + +// Add the indixies on the user profile data. +UserSchema.index({ + 'profiles.id': 1, + 'profiles.provider': 1 }, { - timestamps: { - createdAt: 'created_at', - updatedAt: 'updated_at' - } + unique: true, + background: false }); /** - * Finds a user by the id. - * @param {String} id identifier of the user (uuid) -*/ -UserProfileSchema.statics.findById = function(id) { - return UserProfile.findOne({id}); + * toJSON overrides to remove the password field from the json + * output. + */ +UserSchema.options.toJSON = {}; +UserSchema.options.toJSON.hide = 'password profiles roles disabled'; +UserSchema.options.toJSON.transform = (doc, ret, options) => { + if (options.hide) { + options.hide.split(' ').forEach((prop) => { + delete ret[prop]; + }); + } + + return ret; +}; + +/** + * Finds a user given their email address that we have for them in the system + * and ensures that the retuned user matches the password passed in as well. + * @param {string} email - email to look up the user by + * @param {string} password - password to match against the found user + * @param {Function} done [description] + */ +UserSchema.statics.findLocalUser = function(email, password) { + return User.findOne({ + profiles: { + $elemMatch: { + id: email, + provider: 'local' + } + } + }) + .then((user) => { + if (!user) { + return false; + } + + return new Promise((resolve, reject) => { + bcrypt.compare(password, user.password, (err, res) => { + if (err) { + return reject(err); + } + + if (!res) { + return resolve(false); + } + + return resolve(user); + }); + }); + }); +}; + +/** + * Merges two users together by taking all the profiles on a given user and + * pushing them into the source user followed by deleting the destination user's + * user account. This will not merge the roles associated with the source user. + * @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 + * @return {Promise} resolves when the users are merged + */ +UserSchema.statics.mergeUsers = function(dstUserID, srcUserID) { + let srcUser, dstUser; + + return Promise.all([ + User.findOne({id: dstUserID}).exec(), + User.findOne({id: srcUserID}).exec() + ]).then((users) => { + dstUser = users[0]; + srcUser = users[1]; + + srcUser.profiles.forEach((profile) => { + dstUser.profiles.push(profile); + }); + + return srcUser.remove(); + }).then(() => dstUser.save()); +}; + +/** + * Finds a user given a social profile and if the user does not exist, creates + * them. + * @param {Object} profile - User social/external profile + * @param {Function} done [description] + */ +UserSchema.statics.findOrCreateExternalUser = function(profile) { + return User.findOne({ + profiles: { + $elemMatch: { + id: profile.id, + provider: profile.provider + } + } + }) + .then((user) => { + if (user) { + return user; + } + + // The user was not found, lets create them! + user = new User({ + displayName: profile.displayName, + roles: [], + profiles: [ + { + id: profile.id, + provider: profile.provider + } + ] + }); + + return user.save(); + }); +}; + +UserSchema.statics.changePassword = function(id, password) { + return new Promise((resolve, reject) => { + bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => { + if (err) { + return reject(err); + } + + resolve(hashedPassword); + }); + }) + .then((hashedPassword) => { + return User.update({id}, { + $set: { + password: hashedPassword + } + }); + }); +}; + +/** + * Creates the local user with a given email, password, and name. + * @param {String} email email of the new user + * @param {String} password plaintext password of the new user + * @param {String} displayName name of the display user + * @param {Function} done callback + */ +UserSchema.statics.createLocalUser = function(email, password, displayName) { + return new Promise((resolve, reject) => { + bcrypt.hash(password, SALT_ROUNDS, (err, hashedPassword) => { + if (err) { + return reject(err); + } + + let user = new User({ + displayName: displayName, + password: hashedPassword, + roles: [], + profiles: [ + { + id: email, + provider: 'local' + } + ] + }); + + user.save((err) => { + if (err) { + return reject(err); + } + + return resolve(user); + }); + }); + }); +}; + +/** + * Disables a given user account. + * @param {String} id id of a user + * @param {Function} done callback after the operation is complete + */ +UserSchema.statics.disableUser = function(id) { + return User.update({ + id: id + }, { + $set: { + disabled: true + } + }); +}; + +/** + * Enables a given user account. + * @param {String} id id of a user + * @param {Function} done callback after the operation is complete + */ +UserSchema.statics.enableUser = function(id) { + return User.update({ + id: id + }, { + $set: { + disabled: false + } + }); +}; + +/** + * Adds a role to a user. + * @param {String} id id of a user + * @param {String} role role to add + * @param {Function} done callback after the operation is complete + */ +UserSchema.statics.addRoleToUser = function(id, role) { + return User.update({ + id: id + }, { + $addToSet: { + roles: role + } + }); +}; + +/** + * Removes a role from a user. + * @param {String} id id of a user + * @param {String} role role to remove + * @param {Function} done callback after the operation is complete + */ +UserSchema.statics.removeRoleFromUser = function(id, role) { + return User.update({ + id: id + }, { + $pull: { + roles: role + } + }); }; /** * Finds users in an array of idd. * @param {String} idd array of user identifiers (uuid) */ -UserProfileSchema.statics.findByIdArray = function(ids) { - return UserProfile.find({ +UserSchema.statics.findByIdArray = function(ids) { + return User.find({ 'id': {$in: ids} }); }; -// TO DO: methods -// modifications to user as statics -// find by auth user id +const User = mongoose.model('User', UserSchema); -const UserProfile = mongoose.model('UserProfile', UserProfileSchema); - -module.exports = UserProfile; +module.exports = User; +module.exports.Schema = UserSchema; diff --git a/mongoose.js b/mongoose.js index 8d9bb3bfb..9062816e6 100644 --- a/mongoose.js +++ b/mongoose.js @@ -1,4 +1,5 @@ const mongoose = require('mongoose'); +const debug = require('debug')('talk:db'); const enabled = require('debug').enabled; const url = process.env.TALK_MONGO_URL || 'mongodb://localhost'; @@ -11,11 +12,14 @@ if (enabled('talk:db')) { try { mongoose.connect(url, (err) => { - if (err) {throw err;} - console.log('Connected to MongoDB!'); + if (err) { + throw err; + } + + debug('Connected to MongoDB!'); }); } catch (err) { - console.log('Cannot stablish a connection with MongoDB'); + console.error('Cannot stablish a connection with MongoDB', err); } module.exports = mongoose; diff --git a/package.json b/package.json index 979475b5b..b2e7b938b 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "start": "./bin/www", "build": "webpack --config webpack.config.js --bail", "build-watch": "webpack --config webpack.config.dev.js --watch", - "lint": "eslint .", + "lint": "eslint bin/* .", "pretest": "npm install", "test": "mocha tests --recursive", "test-watch": "mocha tests --recursive -w", @@ -43,12 +43,15 @@ }, "homepage": "https://github.com/coralproject/talk#readme", "dependencies": { + "bcrypt": "^0.8.7", "body-parser": "^1.15.2", + "commander": "^2.9.0", "debug": "^2.2.0", "ejs": "^2.5.2", "express": "^4.14.0", "mongoose": "^4.6.5", "morgan": "^1.7.0", + "prompt": "^1.0.0", "uuid": "^2.0.3" }, "devDependencies": {