2017-02-16 03:27:16 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
'use strict';
|
|
|
|
|
2018-12-16 06:42:19 +00:00
|
|
|
const {
|
|
|
|
printUsageAndSetExitCode,
|
|
|
|
getAnswers,
|
|
|
|
ExitCodes,
|
|
|
|
argv,
|
2022-06-05 20:04:25 +00:00
|
|
|
initConfigAndDatabases,
|
|
|
|
} = require('./oputil_common.js');
|
|
|
|
const getHelpFor = require('./oputil_help.js').getHelpFor;
|
|
|
|
const Errors = require('../enig_error.js').Errors;
|
|
|
|
const UserProps = require('../user_property.js');
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2023-03-15 04:22:11 +00:00
|
|
|
// deps
|
2022-06-05 20:04:25 +00:00
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
|
|
|
const moment = require('moment');
|
|
|
|
const fs = require('fs-extra');
|
2022-08-16 18:19:49 +00:00
|
|
|
const Table = require('easy-table');
|
2017-02-16 03:27:16 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
exports.handleUserCommand = handleUserCommand;
|
2017-02-16 03:27:16 +00:00
|
|
|
|
|
|
|
function initAndGetUser(userName, cb) {
|
2018-06-22 05:15:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function init(callback) {
|
|
|
|
initConfigAndDatabases(callback);
|
|
|
|
},
|
|
|
|
function getUserObject(callback) {
|
2018-11-23 19:02:41 +00:00
|
|
|
const User = require('../../core/user.js');
|
|
|
|
User.getUserIdAndName(userName, (err, userId) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2020-11-18 20:22:22 +00:00
|
|
|
// try user ID if number was supplied
|
|
|
|
if (_.isNumber(userName)) {
|
|
|
|
return User.getUser(parseInt(userName), callback);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
2018-11-23 19:02:41 +00:00
|
|
|
return User.getUser(userId, callback);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
(err, user) => {
|
|
|
|
return cb(err, user);
|
|
|
|
}
|
|
|
|
);
|
2017-02-16 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:36:34 +00:00
|
|
|
function setAccountStatus(user, status) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv._.length < 3) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const AccountStatus = require('../../core/user.js').AccountStatus;
|
2018-11-23 06:07:37 +00:00
|
|
|
|
|
|
|
status = {
|
2022-06-05 20:04:25 +00:00
|
|
|
activate: AccountStatus.active,
|
|
|
|
deactivate: AccountStatus.inactive,
|
|
|
|
disable: AccountStatus.disabled,
|
|
|
|
lock: AccountStatus.locked,
|
2018-11-23 06:07:37 +00:00
|
|
|
}[status];
|
|
|
|
|
2018-06-22 05:15:04 +00:00
|
|
|
const statusDesc = _.invert(AccountStatus)[status];
|
2018-11-23 19:02:41 +00:00
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-11-23 19:02:41 +00:00
|
|
|
return user.persistProperty(UserProps.AccountStatus, status, callback);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
if (AccountStatus.active !== status) {
|
2018-11-23 19:02:41 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
return user.unlockAccount(callback);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-11-23 19:02:41 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-11-23 19:02:41 +00:00
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info(`User status set to ${statusDesc}`);
|
|
|
|
}
|
2018-06-22 05:15:04 +00:00
|
|
|
}
|
2018-11-23 19:02:41 +00:00
|
|
|
);
|
2018-05-22 02:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setUserPassword(user) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv._.length < 4) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function validate(callback) {
|
|
|
|
// :TODO: prompt if no password provided (more secure, no history, etc.)
|
|
|
|
const password = argv._[argv._.length - 1];
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === password.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return callback(Errors.Invalid('Invalid password'));
|
|
|
|
}
|
|
|
|
return callback(null, password);
|
|
|
|
},
|
|
|
|
function set(password, callback) {
|
|
|
|
user.setNewAuthCredentials(password, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
}
|
|
|
|
return callback(err);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-06-22 05:15:04 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info('New password set');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2018-05-22 02:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 06:42:19 +00:00
|
|
|
function removeUserRecordsFromDbAndTable(dbName, tableName, userId, col, cb) {
|
|
|
|
const db = require('../../core/database.js').dbs[dbName];
|
|
|
|
db.run(
|
|
|
|
`DELETE FROM ${tableName}
|
|
|
|
WHERE ${col} = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[userId],
|
2018-12-16 06:42:19 +00:00
|
|
|
err => {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeUser(user) {
|
|
|
|
async.series(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
if (user.isRoot()) {
|
2018-12-16 07:26:28 +00:00
|
|
|
return callback(Errors.Invalid('Cannot delete root/SysOp user!'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return callback(null);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
if (false === argv.prompt) {
|
2018-12-16 06:42:19 +00:00
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info('About to permanently delete the following user:');
|
|
|
|
console.info(`Username : ${user.username}`);
|
2022-06-05 20:04:25 +00:00
|
|
|
console.info(
|
|
|
|
`Real name: ${user.properties[UserProps.RealName] || 'N/A'}`
|
|
|
|
);
|
2018-12-16 06:42:19 +00:00
|
|
|
console.info(`User ID : ${user.userId}`);
|
|
|
|
console.info('WARNING: This cannot be undone!');
|
2022-06-05 20:04:25 +00:00
|
|
|
getAnswers(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
name: 'proceed',
|
|
|
|
message: `Proceed in deleting ${user.username}?`,
|
|
|
|
type: 'confirm',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
answers => {
|
|
|
|
if (answers.proceed) {
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
return callback(Errors.General('User canceled'));
|
2018-12-16 06:42:19 +00:00
|
|
|
}
|
2022-06-05 20:04:25 +00:00
|
|
|
);
|
2018-12-16 06:42:19 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-12-16 06:42:19 +00:00
|
|
|
// op has confirmed they are wanting ready to proceed (or passed --no-prompt)
|
|
|
|
const DeleteFrom = {
|
2022-06-05 20:04:25 +00:00
|
|
|
message: ['user_message_area_last_read'],
|
|
|
|
system: ['user_event_log'],
|
|
|
|
user: ['user_group_member', 'user'],
|
|
|
|
file: ['file_user_rating'],
|
2018-12-16 06:42:19 +00:00
|
|
|
};
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
Object.keys(DeleteFrom),
|
|
|
|
(dbName, nextDbName) => {
|
|
|
|
const tables = DeleteFrom[dbName];
|
|
|
|
async.eachSeries(
|
|
|
|
tables,
|
|
|
|
(tableName, nextTableName) => {
|
|
|
|
const col =
|
|
|
|
'user' === dbName && 'user' === tableName
|
|
|
|
? 'id'
|
|
|
|
: 'user_id';
|
|
|
|
removeUserRecordsFromDbAndTable(
|
|
|
|
dbName,
|
|
|
|
tableName,
|
|
|
|
user.userId,
|
|
|
|
col,
|
|
|
|
err => {
|
|
|
|
return nextTableName(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return nextDbName(err);
|
|
|
|
}
|
|
|
|
);
|
2018-12-16 06:42:19 +00:00
|
|
|
},
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
2018-12-16 06:42:19 +00:00
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2018-12-16 06:42:19 +00:00
|
|
|
//
|
|
|
|
// Clean up *private* messages *to* this user
|
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
const Message = require('../../core/message.js');
|
|
|
|
const MsgDb = require('../../core/database.js').dbs.message;
|
2018-12-16 06:42:19 +00:00
|
|
|
|
|
|
|
const filter = {
|
2022-06-05 20:04:25 +00:00
|
|
|
resultType: 'id',
|
|
|
|
privateTagUserId: user.userId,
|
2018-12-16 06:42:19 +00:00
|
|
|
};
|
|
|
|
Message.findMessages(filter, (err, ids) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-12-16 06:42:19 +00:00
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
async.eachSeries(
|
|
|
|
ids,
|
|
|
|
(messageId, nextMessageId) => {
|
|
|
|
MsgDb.run(
|
|
|
|
`DELETE FROM message
|
2018-12-16 06:42:19 +00:00
|
|
|
WHERE message_id = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[messageId],
|
|
|
|
err => {
|
|
|
|
return nextMessageId(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
2018-12-16 06:42:19 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2018-12-16 06:42:19 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-12-16 06:42:19 +00:00
|
|
|
return console.error(err.reason ? err.reason : err.message);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.info('User has been deleted.');
|
|
|
|
}
|
|
|
|
);
|
2018-05-22 02:36:34 +00:00
|
|
|
}
|
|
|
|
|
2019-02-16 18:03:46 +00:00
|
|
|
function renameUser(user) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv._.length < 3) {
|
2019-02-16 18:03:46 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const newUserName = argv._[argv._.length - 1];
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
|
|
|
const {
|
|
|
|
validateUserNameAvail,
|
|
|
|
} = require('../../core/system_view_validate.js');
|
2019-02-16 18:03:46 +00:00
|
|
|
return validateUserNameAvail(newUserName, callback);
|
|
|
|
},
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2019-02-16 18:03:46 +00:00
|
|
|
const userDb = require('../../core/database.js').dbs.user;
|
|
|
|
userDb.run(
|
|
|
|
`UPDATE user
|
|
|
|
SET user_name = ?
|
|
|
|
WHERE id = ?;`,
|
2022-06-05 20:04:25 +00:00
|
|
|
[newUserName, user.userId],
|
2019-02-16 18:03:46 +00:00
|
|
|
err => {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
);
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2019-02-16 18:03:46 +00:00
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-02-16 18:03:46 +00:00
|
|
|
return console.error(err.reason ? err.reason : err.message);
|
|
|
|
}
|
|
|
|
return console.info(`User "${user.username}" renamed to "${newUserName}"`);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:36:34 +00:00
|
|
|
function modUserGroups(user) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv._.length < 3) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
let groupName = argv._[argv._.length - 1].toString().replace(/["']/g, ''); // remove any quotes - necessary to allow "-foo"
|
|
|
|
let action = groupName[0]; // + or -
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('-' === action || '+' === action || '~' === action) {
|
2018-06-22 05:15:04 +00:00
|
|
|
groupName = groupName.substr(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
action = action || '+';
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (0 === groupName.length) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2020-11-29 21:43:26 +00:00
|
|
|
// Groups are currently arbitrary, so do a slight validation
|
2018-06-22 05:15:04 +00:00
|
|
|
//
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!/[A-Za-z0-9]+/.test(groupName)) {
|
2018-06-22 05:15:04 +00:00
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
return console.error('Bad group name');
|
|
|
|
}
|
|
|
|
|
|
|
|
function done(err) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
process.exitCode = ExitCodes.BAD_ARGS;
|
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info('User groups modified');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const UserGroup = require('../../core/user_group.js');
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('-' === action || '~' === action) {
|
2018-06-22 05:15:04 +00:00
|
|
|
UserGroup.removeUserFromGroup(user.userId, groupName, done);
|
|
|
|
} else {
|
|
|
|
UserGroup.addUserToGroup(user.userId, groupName, done);
|
|
|
|
}
|
2018-05-22 02:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-16 07:26:28 +00:00
|
|
|
function showUserInfo(user) {
|
2023-03-15 04:22:11 +00:00
|
|
|
const User = require('../user');
|
|
|
|
const ActivityPubSettings = require('../activitypub/settings');
|
|
|
|
const { OTPTypes } = require('../user_2fa_otp');
|
2018-12-16 07:26:28 +00:00
|
|
|
|
|
|
|
const statusDesc = () => {
|
|
|
|
const status = user.properties[UserProps.AccountStatus];
|
2022-08-16 18:19:49 +00:00
|
|
|
return _.invert(User.AccountStatus)[status] || 'N/A';
|
2018-12-16 07:26:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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';
|
|
|
|
};
|
|
|
|
|
2020-11-18 20:22:22 +00:00
|
|
|
const currentTheme = () => {
|
|
|
|
return user.properties[UserProps.ThemeId];
|
|
|
|
};
|
|
|
|
|
2023-03-15 04:22:11 +00:00
|
|
|
const apSettings = ActivityPubSettings.fromUser(user);
|
|
|
|
|
|
|
|
let infoDump = `User information:
|
2018-12-16 07:26:28 +00:00
|
|
|
Username : ${user.username}${user.isRoot() ? ' (root/SysOp)' : ''}
|
|
|
|
Real name : ${propOrNA(UserProps.RealName)}
|
|
|
|
ID : ${user.userId}
|
|
|
|
Status : ${statusDesc()}
|
|
|
|
Groups : ${user.groups.join(', ')}
|
2020-11-18 20:22:22 +00:00
|
|
|
Theme ID : ${currentTheme()}
|
2018-12-16 07:26:28 +00:00
|
|
|
Created : ${created()}
|
|
|
|
Last login : ${lastLogin()}
|
|
|
|
Login count : ${propOrNA(UserProps.LoginCount)}
|
|
|
|
Email : ${propOrNA(UserProps.EmailAddress)}
|
|
|
|
Location : ${propOrNA(UserProps.Location)}
|
2023-03-15 04:22:11 +00:00
|
|
|
Affiliations : ${propOrNA(UserProps.Affiliations)}
|
|
|
|
ActivityPub : ${apSettings.enabled ? 'enabled' : 'disabled'}`;
|
|
|
|
|
|
|
|
const otp = user.getProperty(UserProps.AuthFactor2OTP);
|
|
|
|
const oppDesc =
|
|
|
|
{
|
|
|
|
[OTPTypes.RFC6238_TOTP]: 'RFC6238 TOTP',
|
|
|
|
[OTPTypes.RFC4266_HOTP]: 'rfc4266 HOTP',
|
|
|
|
[OTPTypes.GoogleAuthenticator]: 'GoogleAuth',
|
|
|
|
}[otp] || 'disabled';
|
|
|
|
infoDump += `\n2FA OTP : ${oppDesc}`;
|
|
|
|
|
|
|
|
if (argv.security && otp) {
|
|
|
|
const backupCodesOrNa = () => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(
|
|
|
|
user.getProperty(UserProps.AuthFactor2OTPBackupCodes)
|
|
|
|
).join(', ');
|
|
|
|
} catch (e) {
|
|
|
|
return 'N/A';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
infoDump += `\nOTP secret : ${
|
|
|
|
user.getProperty(UserProps.AuthFactor2OTPSecret) || 'N/A'
|
2019-05-11 06:21:42 +00:00
|
|
|
}
|
2023-03-15 04:22:11 +00:00
|
|
|
OTP Backup : ${backupCodesOrNa()}`;
|
2019-05-11 06:21:42 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 04:22:11 +00:00
|
|
|
console.info(infoDump);
|
2018-12-16 07:26:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-11 06:21:42 +00:00
|
|
|
function twoFactorAuthOTP(user) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (argv._.length < 4) {
|
2019-05-10 01:56:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
OTPTypes,
|
|
|
|
prepareOTP,
|
2019-06-14 01:47:04 +00:00
|
|
|
createBackupCodes,
|
2019-05-10 01:56:04 +00:00
|
|
|
} = require('../../core/user_2fa_otp.js');
|
|
|
|
|
2019-05-25 04:27:50 +00:00
|
|
|
let otpType = argv._[argv._.length - 1];
|
|
|
|
|
|
|
|
// shortcut for removal
|
2022-06-05 20:04:25 +00:00
|
|
|
if ('disable' === otpType) {
|
2019-05-25 04:27:50 +00:00
|
|
|
const props = [
|
|
|
|
UserProps.AuthFactor2OTP,
|
|
|
|
UserProps.AuthFactor2OTPSecret,
|
|
|
|
UserProps.AuthFactor2OTPBackupCodes,
|
|
|
|
];
|
|
|
|
return user.removeProperties(props, err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-05-25 04:27:50 +00:00
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
|
|
|
console.info(`2FA OTP disabled for ${user.username}`);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-10 01:56:04 +00:00
|
|
|
async.waterfall(
|
|
|
|
[
|
|
|
|
function validate(callback) {
|
|
|
|
// :TODO: Prompt for if not supplied
|
2019-05-11 06:21:42 +00:00
|
|
|
// allow aliases for OTP types
|
2022-06-05 20:04:25 +00:00
|
|
|
otpType =
|
|
|
|
{
|
|
|
|
google: OTPTypes.GoogleAuthenticator,
|
|
|
|
hotp: OTPTypes.RFC4266_HOTP,
|
|
|
|
totp: OTPTypes.RFC6238_TOTP,
|
|
|
|
}[otpType] || otpType;
|
2019-05-10 01:56:04 +00:00
|
|
|
otpType = _.find(OTPTypes, t => {
|
2019-05-11 06:21:42 +00:00
|
|
|
return t.toLowerCase() === otpType.toLowerCase();
|
2019-05-10 01:56:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!otpType) {
|
2019-05-10 01:56:04 +00:00
|
|
|
return callback(Errors.Invalid('Invalid OTP type'));
|
|
|
|
}
|
|
|
|
return callback(null, otpType);
|
|
|
|
},
|
|
|
|
function prepare(otpType, callback) {
|
|
|
|
const otpOpts = {
|
2022-06-05 20:04:25 +00:00
|
|
|
username: user.username,
|
|
|
|
qrType: argv['qr-type'] || 'ascii',
|
2019-05-10 01:56:04 +00:00
|
|
|
};
|
|
|
|
prepareOTP(otpType, otpOpts, (err, otpInfo) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
err,
|
|
|
|
Object.assign(otpInfo, {
|
|
|
|
otpType,
|
|
|
|
backupCodes: createBackupCodes(),
|
|
|
|
})
|
|
|
|
);
|
2019-05-10 01:56:04 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
function storeOrDisplayQR(otpInfo, callback) {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!argv.out || !otpInfo.qr) {
|
2019-05-10 01:56:04 +00:00
|
|
|
return callback(null, otpInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.writeFile(argv.out, otpInfo.qr, 'utf8', err => {
|
|
|
|
return callback(err, otpInfo);
|
|
|
|
});
|
2019-05-10 02:25:47 +00:00
|
|
|
},
|
|
|
|
function persist(otpInfo, callback) {
|
|
|
|
const props = {
|
2022-06-05 20:04:25 +00:00
|
|
|
[UserProps.AuthFactor2OTP]: otpInfo.otpType,
|
|
|
|
[UserProps.AuthFactor2OTPSecret]: otpInfo.secret,
|
|
|
|
[UserProps.AuthFactor2OTPBackupCodes]: JSON.stringify(
|
|
|
|
otpInfo.backupCodes
|
|
|
|
),
|
2019-05-10 02:25:47 +00:00
|
|
|
};
|
|
|
|
user.persistProperties(props, err => {
|
|
|
|
return callback(err, otpInfo);
|
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
},
|
2019-05-10 01:56:04 +00:00
|
|
|
],
|
2019-05-10 02:25:47 +00:00
|
|
|
(err, otpInfo) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2019-05-10 01:56:04 +00:00
|
|
|
console.error(err.message);
|
|
|
|
} else {
|
2019-05-11 06:21:42 +00:00
|
|
|
console.info(`OTP enabled for : ${user.username}`);
|
|
|
|
console.info(`Secret : ${otpInfo.secret}`);
|
|
|
|
console.info(`Backup codes : ${otpInfo.backupCodes.join(', ')}`);
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (otpInfo.qr) {
|
|
|
|
if (!argv.out) {
|
2019-05-11 06:21:42 +00:00
|
|
|
console.info('--- Begin QR ---');
|
|
|
|
console.info(otpInfo.qr);
|
|
|
|
console.info('--- End QR ---');
|
|
|
|
} else {
|
|
|
|
console.info(`QR code saved to ${argv.out}`);
|
|
|
|
}
|
2019-05-10 02:25:47 +00:00
|
|
|
}
|
2019-05-10 01:56:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-06 03:18:58 +00:00
|
|
|
function listUsers() {
|
|
|
|
// oputil user list [disabled|inactive|active|locked|all]
|
2020-05-31 04:39:27 +00:00
|
|
|
// :TODO: --created-since SPEC and --last-called SPEC
|
|
|
|
// --created-since SPEC
|
|
|
|
// SPEC can be TIMESTAMP or e.g. "-1hour" or "-90days"
|
2020-05-06 03:18:58 +00:00
|
|
|
let listWhat;
|
|
|
|
if (argv._.length > 2) {
|
|
|
|
listWhat = argv._[argv._.length - 1];
|
|
|
|
} else {
|
|
|
|
listWhat = 'all';
|
|
|
|
}
|
|
|
|
|
2022-08-16 18:19:49 +00:00
|
|
|
const sortBy = (argv.sort || 'id').toLowerCase();
|
|
|
|
|
2020-05-06 03:18:58 +00:00
|
|
|
const User = require('../../core/user');
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!['all'].concat(Object.keys(User.AccountStatus)).includes(listWhat)) {
|
2020-05-06 03:18:58 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
async.waterfall(
|
|
|
|
[
|
2022-06-05 20:04:25 +00:00
|
|
|
callback => {
|
2020-05-06 03:18:58 +00:00
|
|
|
const UserProps = require('../../core/user_property');
|
|
|
|
|
|
|
|
const userListOpts = {
|
2022-08-16 18:19:49 +00:00
|
|
|
properties: [
|
|
|
|
UserProps.RealName,
|
|
|
|
UserProps.AccountStatus,
|
|
|
|
UserProps.AccountCreated,
|
|
|
|
UserProps.LastLoginTs,
|
|
|
|
UserProps.LoginCount,
|
|
|
|
],
|
2020-05-06 03:18:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
User.getUserList(userListOpts, (err, userList) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ('all' === listWhat) {
|
|
|
|
return callback(null, userList);
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountStatusFilter = User.AccountStatus[listWhat].toString();
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return callback(
|
|
|
|
null,
|
|
|
|
userList.filter(user => {
|
|
|
|
return user[UserProps.AccountStatus] === accountStatusFilter;
|
|
|
|
})
|
|
|
|
);
|
2020-05-06 03:18:58 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
(userList, callback) => {
|
2022-08-16 18:19:49 +00:00
|
|
|
// default sort: by ID
|
|
|
|
const sortById = (left, right) => {
|
|
|
|
return left.userId - right.userId;
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortByLogin = prop => (left, right) => {
|
|
|
|
return parseInt(right[prop]) - parseInt(left[prop]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortByString = prop => (left, right) => {
|
|
|
|
return left[prop].localeCompare(right[prop], {
|
|
|
|
sensitivity: false,
|
|
|
|
numeric: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const sortByTimestamp = prop => (left, right) => {
|
2022-08-16 19:45:39 +00:00
|
|
|
return moment(right[prop]) - moment(left[prop]);
|
2022-08-16 18:19:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let sorter;
|
|
|
|
switch (sortBy) {
|
|
|
|
case 'username':
|
|
|
|
sorter = sortByString('userName');
|
|
|
|
break;
|
|
|
|
case 'realname':
|
|
|
|
sorter = sortByString(UserProps.RealName);
|
|
|
|
break;
|
|
|
|
case 'status':
|
|
|
|
sorter = sortByString(UserProps.AccountStatus);
|
|
|
|
break;
|
|
|
|
case 'created':
|
|
|
|
sorter = sortByTimestamp(UserProps.AccountCreated);
|
|
|
|
break;
|
|
|
|
case 'lastlogin':
|
|
|
|
sorter = sortByTimestamp(UserProps.LastLoginTs);
|
|
|
|
break;
|
|
|
|
case 'logins':
|
|
|
|
sorter = sortByLogin(UserProps.LoginCount);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'id':
|
|
|
|
default:
|
|
|
|
sorter = sortById;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
userList.sort(sorter);
|
|
|
|
|
|
|
|
const StatusNames = _.invert(User.AccountStatus);
|
|
|
|
|
|
|
|
const propOrNA = (user, prop) => {
|
|
|
|
return user[prop] || 'N/A';
|
|
|
|
};
|
|
|
|
|
|
|
|
const timestampOrNA = (user, prop, format) => {
|
|
|
|
let ts = user[prop];
|
|
|
|
return ts ? moment(ts).format(format) : 'N/A';
|
|
|
|
};
|
|
|
|
|
|
|
|
const makeAccountStatus = status => {
|
|
|
|
return StatusNames[status] || 'N/A';
|
|
|
|
};
|
|
|
|
|
|
|
|
const table = new Table();
|
2020-05-06 03:18:58 +00:00
|
|
|
userList.forEach(user => {
|
2022-08-16 18:19:49 +00:00
|
|
|
table.cell('ID', user.userId);
|
|
|
|
table.cell('Username', user.userName);
|
|
|
|
table.cell('Real Name', user[UserProps.RealName]);
|
|
|
|
table.cell(
|
|
|
|
'Status',
|
|
|
|
makeAccountStatus(user[UserProps.AccountStatus])
|
|
|
|
);
|
|
|
|
table.cell(
|
|
|
|
'Created',
|
|
|
|
timestampOrNA(user, UserProps.AccountCreated, 'YYYY-MM-DD')
|
|
|
|
);
|
|
|
|
table.cell(
|
|
|
|
'Last Login',
|
|
|
|
timestampOrNA(user, UserProps.LastLoginTs, 'YYYY-MM-DD HH::mm')
|
|
|
|
);
|
|
|
|
table.cell('Logins', propOrNA(user, UserProps.LoginCount));
|
|
|
|
|
|
|
|
table.newRow();
|
2020-05-06 03:18:58 +00:00
|
|
|
});
|
2020-06-16 01:08:55 +00:00
|
|
|
|
2022-08-16 18:19:49 +00:00
|
|
|
console.info(table.toString());
|
|
|
|
|
2020-06-16 01:08:55 +00:00
|
|
|
return callback(null);
|
2020-05-06 03:18:58 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
err => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (err) {
|
2020-05-06 03:18:58 +00:00
|
|
|
return console.error(err.reason ? err.reason : err.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-22 02:36:34 +00:00
|
|
|
function handleUserCommand() {
|
2022-06-05 20:04:25 +00:00
|
|
|
function errUsage() {
|
2018-06-22 05:15:04 +00:00
|
|
|
return printUsageAndSetExitCode(getHelpFor('User'), ExitCodes.ERROR);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (true === argv.help) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return errUsage();
|
|
|
|
}
|
|
|
|
|
2019-02-16 18:03:46 +00:00
|
|
|
const action = argv._[1];
|
2022-06-05 20:04:25 +00:00
|
|
|
const userRequired = !['list'].includes(action);
|
2020-05-06 03:18:58 +00:00
|
|
|
|
|
|
|
let userName;
|
|
|
|
if (userRequired) {
|
|
|
|
const usernameIdx = [
|
2022-06-05 20:04:25 +00:00
|
|
|
'pw',
|
|
|
|
'pass',
|
|
|
|
'passwd',
|
|
|
|
'password',
|
2020-05-06 03:18:58 +00:00
|
|
|
'group',
|
2022-06-05 20:04:25 +00:00
|
|
|
'mv',
|
|
|
|
'rename',
|
|
|
|
'2fa-otp',
|
|
|
|
'otp',
|
|
|
|
].includes(action)
|
|
|
|
? argv._.length - 2
|
|
|
|
: argv._.length - 1;
|
2020-05-06 03:18:58 +00:00
|
|
|
userName = argv._[usernameIdx];
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
if (!userName && userRequired) {
|
2018-06-22 05:15:04 +00:00
|
|
|
return errUsage();
|
|
|
|
}
|
|
|
|
|
|
|
|
initAndGetUser(userName, (err, user) => {
|
2022-06-05 20:04:25 +00:00
|
|
|
if (userName && err) {
|
2018-06-22 05:15:04 +00:00
|
|
|
process.exitCode = ExitCodes.ERROR;
|
|
|
|
return console.error(err.message);
|
|
|
|
}
|
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
pw: setUserPassword,
|
|
|
|
passwd: setUserPassword,
|
|
|
|
password: setUserPassword,
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
rm: removeUser,
|
|
|
|
remove: removeUser,
|
|
|
|
del: removeUser,
|
|
|
|
delete: removeUser,
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
mv: renameUser,
|
|
|
|
rename: renameUser,
|
2019-02-16 18:03:46 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
activate: setAccountStatus,
|
|
|
|
deactivate: setAccountStatus,
|
|
|
|
disable: setAccountStatus,
|
|
|
|
lock: setAccountStatus,
|
2018-06-22 05:15:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
group: modUserGroups,
|
2018-12-16 07:26:28 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
info: showUserInfo,
|
2019-05-10 01:56:04 +00:00
|
|
|
|
2022-06-05 20:04:25 +00:00
|
|
|
'2fa-otp': twoFactorAuthOTP,
|
|
|
|
otp: twoFactorAuthOTP,
|
|
|
|
list: listUsers,
|
|
|
|
}[action] || errUsage
|
|
|
|
)(user, action);
|
2018-06-22 05:15:04 +00:00
|
|
|
});
|
2022-06-05 20:04:25 +00:00
|
|
|
}
|