2015-04-21 05:24:15 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var theme = require('../core/theme.js');
|
2015-07-26 06:20:07 +00:00
|
|
|
//var Log = require('../core/logger.js').log;
|
2015-04-21 05:24:15 +00:00
|
|
|
var ansi = require('../core/ansi_term.js');
|
2015-07-27 04:51:06 +00:00
|
|
|
var userDb = require('./database.js').dbs.user;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
exports.login = login;
|
|
|
|
exports.logoff = logoff;
|
|
|
|
|
|
|
|
function login(callingMenu, formData, extraArgs) {
|
|
|
|
var client = callingMenu.client;
|
|
|
|
|
|
|
|
client.user.authenticate(formData.value.username, formData.value.password, function authenticated(err) {
|
|
|
|
if(err) {
|
2015-07-26 06:20:07 +00:00
|
|
|
client.log.info( { username : formData.value.username }, 'Failed login attempt %s', err);
|
2015-04-21 05:24:15 +00:00
|
|
|
|
|
|
|
client.gotoMenuModule( { name : callingMenu.menuConfig.fallback } );
|
|
|
|
} else {
|
2015-07-27 04:51:06 +00:00
|
|
|
var now = new Date();
|
|
|
|
var user = callingMenu.client.user;
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
// use client.user so we can get correct case
|
2015-07-27 04:51:06 +00:00
|
|
|
client.log.info( { username : user.username }, 'Successful login');
|
2015-04-21 05:24:15 +00:00
|
|
|
|
|
|
|
async.parallel(
|
|
|
|
[
|
|
|
|
function loadThemeConfig(callback) {
|
2015-07-27 04:51:06 +00:00
|
|
|
theme.loadTheme(user.properties.theme_id, function themeLoaded(err, theme) {
|
2015-05-13 05:04:22 +00:00
|
|
|
client.currentTheme = theme;
|
|
|
|
callback(null); // always non-fatal
|
2015-04-21 05:24:15 +00:00
|
|
|
});
|
2015-07-26 06:20:07 +00:00
|
|
|
},
|
2015-07-27 04:51:06 +00:00
|
|
|
function recordLastLogin(callback) {
|
|
|
|
user.persistProperty('last_login_timestamp', now.toISOString(), function persisted(err) {
|
2015-07-26 06:20:07 +00:00
|
|
|
callback(err);
|
|
|
|
});
|
2015-07-27 04:51:06 +00:00
|
|
|
},
|
|
|
|
function recordLoginHistory(callback) {
|
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
userDb.serialize(function serialized() {
|
|
|
|
userDb.run(
|
|
|
|
'INSERT INTO user_login_history (user_id, user_name, timestamp) ' +
|
|
|
|
'VALUES(?, ?, ?);', [ user.userId, user.username, now.toISOString() ]
|
|
|
|
);
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
// keep 30 days of records
|
|
|
|
userDb.run(
|
|
|
|
'DELETE FROM user_login_history ' +
|
|
|
|
'WHERE timestamp <= DATETIME("now", "-30 day");'
|
|
|
|
);
|
|
|
|
});
|
2015-07-27 04:51:06 +00:00
|
|
|
|
2015-07-28 04:10:20 +00:00
|
|
|
callback(null);
|
2015-04-21 05:24:15 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
function complete(err, results) {
|
2015-07-26 06:20:07 +00:00
|
|
|
if(err) {
|
|
|
|
client.log.error(err);
|
|
|
|
// :TODO: drop the connection?
|
|
|
|
}
|
2015-04-21 05:24:15 +00:00
|
|
|
client.gotoMenuModule( { name : callingMenu.menuConfig.next } );
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function logoff(callingMenu, formData, extraArgs) {
|
|
|
|
var client = callingMenu.client;
|
|
|
|
|
|
|
|
// :TODO: record this.
|
|
|
|
|
|
|
|
setTimeout(function timeout() {
|
|
|
|
client.term.write(ansi.normal() + '\nATH0\n');
|
|
|
|
client.end();
|
|
|
|
}, 500);
|
|
|
|
}
|