Add --activate and --deactivate user commands

This commit is contained in:
Bryan Ashby 2016-07-20 22:26:56 -06:00
parent a1167acc03
commit 7b73171d5c
1 changed files with 85 additions and 26 deletions

111
oputil.js
View File

@ -45,8 +45,11 @@ commands:
`usage: optutil.js user --user USERNAME <args> `usage: optutil.js user --user USERNAME <args>
valid args: valid args:
--user USERNAME : specify username --user USERNAME : specify username for further actions
--password PASS : specify password (to reset) --password PASS : set new password
--delete : delete user
--activate : activate user
--deactivate : deactivate user
`, `,
Config : Config :
@ -67,6 +70,79 @@ function initConfig(cb) {
config.init(configPath, cb); config.init(configPath, cb);
} }
function initConfigAndDatabases(cb) {
async.series(
[
function init(callback) {
initConfig(callback);
},
function initDb(callback) {
db.initializeDatabases(callback);
},
],
err => {
return cb(err);
}
);
}
function getUser(userName, cb) {
const user = require('./core/user.js');
user.getUserIdAndName(argv.user, function userNameAndId(err, userId) {
if(err) {
process.exitCode = ExitCodes.BAD_ARGS;
return cb(new Error('Failed to retrieve user'));
} else {
let u = new user.User();
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) {
const AccountStatus = require('./core/user.js').User.AccountStatus;
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'));
}
}
);
}
function handleUserCommand() { function handleUserCommand() {
if(true === argv.help || !_.isString(argv.user) || 0 === argv.user.length) { if(true === argv.help || !_.isString(argv.user) || 0 === argv.user.length) {
process.exitCode = ExitCodes.ERROR; process.exitCode = ExitCodes.ERROR;
@ -79,34 +155,13 @@ function handleUserCommand() {
return console.error('Invalid password'); return console.error('Invalid password');
} }
var user;
async.waterfall( async.waterfall(
[ [
function init(callback) { function init(callback) {
initConfig(callback); initAndGetUser(argv.user, callback);
}, },
function initDb(callback) { function setNewPass(user, callback) {
db.initializeDatabases(callback); user.setNewAuthCredentials(argv.password, function credsSet(err) {
},
function getUser(callback) {
user = require('./core/user.js');
user.getUserIdAndName(argv.user, function userNameAndId(err, userId) {
if(err) {
process.exitCode = ExitCodes.BAD_ARGS;
callback(new Error('Failed to retrieve user'));
} else {
callback(null, userId);
}
});
},
function setNewPass(userId, callback) {
assert(_.isNumber(userId));
assert(userId > 0);
let u = new user.User();
u.userId = userId;
u.setNewAuthCredentials(argv.password, function credsSet(err) {
if(err) { if(err) {
process.exitCode = ExitCodes.ERROR; process.exitCode = ExitCodes.ERROR;
callback(new Error('Failed setting password')); callback(new Error('Failed setting password'));
@ -124,6 +179,10 @@ function handleUserCommand() {
} }
} }
); );
} else if(argv.activate) {
setAccountStatus(argv.user, true);
} else if(argv.deactivate) {
setAccountStatus(argv.user, false);
} }
} }