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

View File

@ -15,6 +15,7 @@ const UserProps = require('../user_property.js');
const async = require('async'); const async = require('async');
const _ = require('lodash'); const _ = require('lodash');
const moment = require('moment');
exports.handleUserCommand = handleUserCommand; exports.handleUserCommand = handleUserCommand;
@ -129,6 +130,13 @@ function removeUserRecordsFromDbAndTable(dbName, tableName, userId, col, cb) {
function removeUser(user) { function removeUser(user) {
async.series( async.series(
[ [
(callback) => {
if(user.isRoot()) {
return callback(Errors.Invalid('Cannot delete root/SysOp user!'));
}
return callback(null);
},
(callback) => { (callback) => {
if(false === argv.prompt) { if(false === argv.prompt) {
return callback(null); 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 handleUserCommand() {
function errUsage() { function errUsage() {
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR); return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
@ -272,7 +318,7 @@ function handleUserCommand() {
} }
const action = argv._[1]; 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]; const userName = argv._[usernameIdx];
if(!userName) { if(!userName) {
@ -286,7 +332,7 @@ function handleUserCommand() {
} }
return ({ return ({
pass : setUserPassword, pw : setUserPassword,
passwd : setUserPassword, passwd : setUserPassword,
password : setUserPassword, password : setUserPassword,
@ -301,6 +347,8 @@ function handleUserCommand() {
lock : setAccountStatus, lock : setAccountStatus,
group : modUserGroups, group : modUserGroups,
info : showUserInfo,
}[action] || errUsage)(user, action); }[action] || errUsage)(user, action);
}); });
} }