2015-10-18 02:58:07 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2016-07-28 03:44:27 +00:00
|
|
|
// ENiGMA½
|
2017-11-24 23:23:15 +00:00
|
|
|
const MenuModule = require('./menu_module.js').MenuModule;
|
|
|
|
const ViewController = require('./view_controller.js').ViewController;
|
|
|
|
const StatLog = require('./stat_log.js');
|
|
|
|
const User = require('./user.js');
|
|
|
|
const stringFormat = require('./string_format.js');
|
2016-07-28 03:44:27 +00:00
|
|
|
|
|
|
|
// deps
|
|
|
|
const moment = require('moment');
|
|
|
|
const async = require('async');
|
|
|
|
const _ = require('lodash');
|
2015-10-18 02:58:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Available listFormat object members:
|
|
|
|
userId
|
|
|
|
userName
|
|
|
|
location
|
|
|
|
affiliation
|
2015-12-14 00:32:05 +00:00
|
|
|
ts
|
2015-10-18 02:58:07 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
exports.moduleInfo = {
|
|
|
|
name : 'Last Callers',
|
|
|
|
desc : 'Last callers to the system',
|
|
|
|
author : 'NuSkooler',
|
2017-02-27 04:28:05 +00:00
|
|
|
packageName : 'codes.l33t.enigma.lastcallers'
|
2015-10-18 02:58:07 +00:00
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
const MciCodeIds = {
|
2015-10-18 02:58:07 +00:00
|
|
|
CallerList : 1,
|
|
|
|
};
|
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
exports.getModule = class LastCallersModule extends MenuModule {
|
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
}
|
2016-08-30 03:04:15 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
mciReady(mciData, cb) {
|
|
|
|
super.mciReady(mciData, err => {
|
|
|
|
if(err) {
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const self = this;
|
|
|
|
const vc = self.viewControllers.allViews = new ViewController( { client : self.client } );
|
|
|
|
|
|
|
|
let loginHistory;
|
|
|
|
let callersView;
|
|
|
|
|
|
|
|
async.series(
|
|
|
|
[
|
|
|
|
function loadFromConfig(callback) {
|
|
|
|
const loadOpts = {
|
|
|
|
callingMenu : self,
|
|
|
|
mciMap : mciData.menu,
|
|
|
|
noInput : true,
|
|
|
|
};
|
|
|
|
|
|
|
|
vc.loadFromMenuConfig(loadOpts, callback);
|
|
|
|
},
|
|
|
|
function fetchHistory(callback) {
|
|
|
|
callersView = vc.getView(MciCodeIds.CallerList);
|
|
|
|
|
|
|
|
// fetch up
|
|
|
|
StatLog.getSystemLogEntries('user_login_history', StatLog.Order.TimestampDesc, 200, (err, lh) => {
|
|
|
|
loginHistory = lh;
|
|
|
|
|
|
|
|
if(self.menuConfig.config.hideSysOpLogin) {
|
|
|
|
const noOpLoginHistory = loginHistory.filter(lh => {
|
2017-02-18 20:21:18 +00:00
|
|
|
return false === User.isRootUserId(parseInt(lh.log_value)); // log_value=userId
|
2017-01-26 05:18:05 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
//
|
|
|
|
// If we have enough items to display, or hideSysOpLogin is set to 'always',
|
|
|
|
// then set loginHistory to our filtered list. Else, we'll leave it be.
|
|
|
|
//
|
|
|
|
if(noOpLoginHistory.length >= callersView.dimens.height || 'always' === self.menuConfig.config.hideSysOpLogin) {
|
|
|
|
loginHistory = noOpLoginHistory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Finally, we need to trim up the list to the needed size
|
|
|
|
//
|
|
|
|
loginHistory = loginHistory.slice(0, callersView.dimens.height);
|
|
|
|
|
|
|
|
return callback(err);
|
2016-07-28 03:44:27 +00:00
|
|
|
});
|
|
|
|
},
|
2017-01-26 05:18:05 +00:00
|
|
|
function getUserNamesAndProperties(callback) {
|
|
|
|
const getPropOpts = {
|
|
|
|
names : [ 'location', 'affiliation' ]
|
|
|
|
};
|
|
|
|
|
|
|
|
const dateTimeFormat = self.menuConfig.config.dateTimeFormat || 'ddd MMM DD';
|
|
|
|
|
|
|
|
async.each(
|
|
|
|
loginHistory,
|
|
|
|
(item, next) => {
|
|
|
|
item.userId = parseInt(item.log_value);
|
|
|
|
item.ts = moment(item.timestamp).format(dateTimeFormat);
|
|
|
|
|
2017-02-18 20:21:18 +00:00
|
|
|
User.getUserName(item.userId, (err, userName) => {
|
2017-03-03 01:50:58 +00:00
|
|
|
if(err) {
|
|
|
|
item.deleted = true;
|
|
|
|
return next(null);
|
|
|
|
} else {
|
|
|
|
item.userName = userName || 'N/A';
|
|
|
|
|
|
|
|
User.loadProperties(item.userId, getPropOpts, (err, props) => {
|
|
|
|
if(!err && props) {
|
|
|
|
item.location = props.location || 'N/A';
|
|
|
|
item.affiliation = item.affils = (props.affiliation || 'N/A');
|
|
|
|
} else {
|
|
|
|
item.location = 'N/A';
|
|
|
|
item.affiliation = item.affils = 'N/A';
|
|
|
|
}
|
|
|
|
return next(null);
|
|
|
|
});
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
});
|
|
|
|
},
|
2017-03-03 01:50:58 +00:00
|
|
|
err => {
|
|
|
|
loginHistory = loginHistory.filter(lh => true !== lh.deleted);
|
|
|
|
return callback(err);
|
|
|
|
}
|
2017-01-26 05:18:05 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
function populateList(callback) {
|
|
|
|
const listFormat = self.menuConfig.config.listFormat || '{userName} - {location} - {affiliation} - {ts}';
|
2016-07-28 03:44:27 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
callersView.setItems(_.map(loginHistory, ce => stringFormat(listFormat, ce) ) );
|
2015-10-18 02:58:07 +00:00
|
|
|
|
2017-01-26 05:18:05 +00:00
|
|
|
callersView.redraw();
|
|
|
|
return callback(null);
|
|
|
|
}
|
|
|
|
],
|
|
|
|
(err) => {
|
|
|
|
if(err) {
|
|
|
|
self.client.log.error( { error : err.toString() }, 'Error loading last callers');
|
|
|
|
}
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2015-11-26 01:03:47 +00:00
|
|
|
};
|