From 422a925daaadd62d178b305bd8d6c9552564c872 Mon Sep 17 00:00:00 2001 From: Bryan Ashby Date: Sun, 16 Dec 2018 00:26:28 -0700 Subject: [PATCH] + oputil.js user info * Fix up some help messaging * Don't allow del of +op --- core/oputil/oputil_help.js | 17 ++++++++----- core/oputil/oputil_user.js | 52 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/core/oputil/oputil_help.js b/core/oputil/oputil_help.js index 8b946edd..36002245 100644 --- a/core/oputil/oputil_help.js +++ b/core/oputil/oputil_help.js @@ -25,13 +25,16 @@ commands: `usage: optutil.js user [] 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 : diff --git a/core/oputil/oputil_user.js b/core/oputil/oputil_user.js index 3f169607..a52facfb 100644 --- a/core/oputil/oputil_user.js +++ b/core/oputil/oputil_user.js @@ -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); }); } \ No newline at end of file