2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const printUsageAndSetExitCode = require('./oputil_common.js').printUsageAndSetExitCode;
|
|
|
|
const ExitCodes = require('./oputil_common.js').ExitCodes;
|
|
|
|
const argv = require('./oputil_common.js').argv;
|
|
|
|
const initConfigAndDatabases = require('./oputil_common.js').initConfigAndDatabases;
|
2017-02-20 18:31:24 +00:00
|
|
|
const getHelpFor = require('./oputil_help.js').getHelpFor;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
const async = require('async');
|
2017-02-18 20:21:18 +00:00
|
|
|
const _ = require('lodash');
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
exports.handleUserCommand = handleUserCommand;
|
|
|
|
|
|
|
|
function handleUserCommand() {
|
|
|
|
if(true === argv.help || !_.isString(argv.user) || 0 === argv.user.length) {
|
2017-02-20 18:31:24 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(_.isString(argv.password)) {
|
|
|
|
if(0 === argv.password.length) {
|
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
return console.error('Invalid password');
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initAndGetUser(argv.user, callback);
|
|
|
|
},
|
|
|
|
function setNewPass(user, callback) {
|
|
|
|
user.setNewAuthCredentials(argv.password, function credsSet(err) {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
callback(new Error('Failed setting password'));
|
|
|
|
} else {
|
|
|
|
callback(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err) {
|
|
|
|
if(err) {
|
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info('Password set');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else if(argv.activate) {
|
|
|
|
setAccountStatus(argv.user, true);
|
|
|
|
} else if(argv.deactivate) {
|
|
|
|
setAccountStatus(argv.user, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getUser(userName, cb) {
|
2017-02-18 20:21:18 +00:00
|
|
|
const User = require('../../core/user.js');
|
|
|
|
User.getUserIdAndName(argv.user, function userNameAndId(err, userId) {
|
2017-02-16 03:27:16 +00:00
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
return cb(new Error('Failed to retrieve user'));
|
|
|
|
} else {
|
2017-02-18 20:21:18 +00:00
|
|
|
let u = new User();
|
2017-02-16 03:27:16 +00:00
|
|
|
u.userId = userId;
|
|
|
|
return cb(null, u);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function initAndGetUser(userName, cb) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initConfigAndDatabases(callback);
|
|
|
|
},
|
|
|
|
function getUserObject(callback) {
|
|
|
|
getUser(argv.user, (err, user) => {
|
|
|
|
if(err) {
|
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
return callback(null, user);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err, user) => {
|
|
|
|
return cb(err, user);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setAccountStatus(userName, active) {
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initAndGetUser(argv.user, callback);
|
|
|
|
},
|
|
|
|
function activateUser(user, callback) {
|
2017-02-18 20:21:18 +00:00
|
|
|
const AccountStatus = require('../../core/user.js').AccountStatus;
|
2017-02-16 03:27:16 +00:00
|
|
|
user.persistProperty('account_status', active ? AccountStatus.active : AccountStatus.inactive, callback);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
err => {
|
|
|
|
if(err) {
|
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info('User ' + ((true === active) ? 'activated' : 'deactivated'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|