+ oputil.js user info

* Fix up some help messaging
* Don't allow del of +op
This commit is contained in:
Bryan Ashby 2018-12-16 00:26:28 -07:00
parent 6d45d74a47
commit 422a925daa
2 changed files with 60 additions and 9 deletions

View File

@ -25,13 +25,16 @@ commands:
`usage: optutil.js user <action> [<args>]
actions:
pw USERNAME PASSWORD set password to PASSWORD for USERNAME
rm USERNAME permanently removes USERNAME user from system
activate USERNAME sets USERNAME's status to active
deactivate USERNAME sets USERNAME's status to inactive
disable USERNAME sets USERNAME's status to disabled
lock USERNAME sets USERNAME's status to locked
group USERNAME [+|-]GROUP adds (+) or removes (-) user from GROUP
info USERNAME display information about a user
pw USERNAME PASSWORD set a user's password
aliases: password, passwd
rm USERNAME permanently removes user from system
aliases: remove, delete, del
activate USERNAME set status to active
deactivate USERNAME set status to inactive
disable USERNAME set status to disabled
lock USERNAME set status to locked
group USERNAME [+|-]GROUP adds (+) or removes (-) user from a group
`,
Config :

View File

@ -15,6 +15,7 @@ const UserProps = require('../user_property.js');
const async = require('async');
const _ = require('lodash');
const moment = require('moment');
exports.handleUserCommand = handleUserCommand;
@ -129,6 +130,13 @@ function removeUserRecordsFromDbAndTable(dbName, tableName, userId, col, cb) {
function removeUser(user) {
async.series(
[
(callback) => {
if(user.isRoot()) {
return callback(Errors.Invalid('Cannot delete root/SysOp user!'));
}
return callback(null);
},
(callback) => {
if(false === argv.prompt) {
return callback(null);
@ -262,6 +270,44 @@ function modUserGroups(user) {
}
}
function showUserInfo(user) {
const User = require('../../core/user.js');
const statusDesc = () => {
const status = user.properties[UserProps.AccountStatus];
return _.invert(User.AccountStatus)[status] || 'unknown';
};
const created = () => {
const ac = user.properties[UserProps.AccountCreated];
return ac ? moment(ac).format() : 'N/A';
};
const lastLogin = () => {
const ll = user.properties[UserProps.LastLoginTs];
return ll ? moment(ll).format() : 'N/A';
};
const propOrNA = p => {
return user.properties[p] || 'N/A';
};
console.info(`User information:
Username : ${user.username}${user.isRoot() ? ' (root/SysOp)' : ''}
Real name : ${propOrNA(UserProps.RealName)}
ID : ${user.userId}
Status : ${statusDesc()}
Groups : ${user.groups.join(', ')}
Created : ${created()}
Last login : ${lastLogin()}
Login count : ${propOrNA(UserProps.LoginCount)}
Email : ${propOrNA(UserProps.EmailAddress)}
Location : ${propOrNA(UserProps.Location)}
Affiliations : ${propOrNA(UserProps.Affiliations)}
`);
}
function handleUserCommand() {
function errUsage() {
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
@ -272,7 +318,7 @@ function handleUserCommand() {
}
const action = argv._[1];
const usernameIdx = [ 'pass', 'passwd', 'password', 'group' ].includes(action) ? argv._.length - 2 : argv._.length - 1;
const usernameIdx = [ 'pw', 'pass', 'passwd', 'password', 'group' ].includes(action) ? argv._.length - 2 : argv._.length - 1;
const userName = argv._[usernameIdx];
if(!userName) {
@ -286,7 +332,7 @@ function handleUserCommand() {
}
return ({
pass : setUserPassword,
pw : setUserPassword,
passwd : setUserPassword,
password : setUserPassword,
@ -301,6 +347,8 @@ function handleUserCommand() {
lock : setAccountStatus,
group : modUserGroups,
info : showUserInfo,
}[action] || errUsage)(user, action);
});
}