2015-04-21 05:24:15 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
var theme = require('./theme.js');
|
|
|
|
var clientConnections = require('./client_connections.js').clientConnections;
|
|
|
|
var ansi = require('./ansi_term.js');
|
|
|
|
var userDb = require('./database.js').dbs.user;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
var async = require('async');
|
2015-10-16 05:41:24 +00:00
|
|
|
var _ = require('lodash');
|
2015-04-21 05:24:15 +00:00
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
exports.login = login;
|
|
|
|
exports.logoff = logoff;
|
2015-09-24 05:41:06 +00:00
|
|
|
exports.fallbackMenu = fallbackMenu;
|
2015-04-21 05:24:15 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
// :TODO: if username exists, record failed login attempt to properties
|
|
|
|
// :TODO: check Config max failed logon attempts/etc.
|
|
|
|
|
2015-09-27 21:35:24 +00:00
|
|
|
client.fallbackMenuModule();
|
|
|
|
//client.gotoMenuModule( { name : callingMenu.menuConfig.fallback } );
|
2015-04-21 05:24:15 +00:00
|
|
|
} else {
|
2015-07-27 04:51:06 +00:00
|
|
|
var now = new Date();
|
|
|
|
var user = callingMenu.client.user;
|
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
//
|
|
|
|
// Ensure this user is not already logged in.
|
|
|
|
// Loop through active connections -- which includes the current --
|
|
|
|
// and check for matching user ID. If the count is > 1, disallow.
|
|
|
|
//
|
|
|
|
var existingClientConnection;
|
|
|
|
clientConnections.forEach(function connEntry(cc) {
|
|
|
|
if(cc.user !== user && cc.user.userId === user.userId) {
|
|
|
|
existingClientConnection = cc;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(existingClientConnection) {
|
|
|
|
client.log.info( {
|
2015-09-26 05:10:18 +00:00
|
|
|
existingClientId : existingClientConnection.session.id,
|
2015-08-05 04:35:59 +00:00
|
|
|
username : user.username,
|
|
|
|
userId : user.userId },
|
|
|
|
'Already logged in'
|
|
|
|
);
|
|
|
|
|
2015-10-16 05:41:24 +00:00
|
|
|
client.term.rawWrite(ansi.resetScreen());
|
2015-08-05 04:38:28 +00:00
|
|
|
|
2015-10-16 05:41:24 +00:00
|
|
|
var tooNodeArt;
|
|
|
|
if(_.has(callingMenu, 'menuConfig.config.tooNodeArt')) {
|
|
|
|
tooNodeArt = callingMenu.menuConfig.config.tooNodeArt;
|
|
|
|
} else {
|
|
|
|
tooNodeArt = 'TOONODE';
|
|
|
|
}
|
|
|
|
|
|
|
|
var artOpts = {
|
|
|
|
client : client,
|
|
|
|
font : callingMenu.menuConfig.font,
|
|
|
|
name : tooNodeArt,
|
|
|
|
};
|
|
|
|
|
|
|
|
theme.displayThemeArt(artOpts, function artDisplayed(err) {
|
|
|
|
if(err) {
|
|
|
|
client.term.write('\nA user by that name is already logged in.\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function timeout() {
|
|
|
|
client.fallbackMenuModule();
|
|
|
|
}, 2000);
|
|
|
|
});
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
},
|
2015-07-30 04:14:26 +00:00
|
|
|
function updateUserLoginCount(callback) {
|
|
|
|
if(!user.properties.login_count) {
|
|
|
|
user.properties.login_count = 1;
|
|
|
|
} else {
|
|
|
|
user.properties.login_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
user.persistProperty('login_count', user.properties.login_count, function persisted(err) {
|
|
|
|
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) {
|
2015-08-05 04:35:59 +00:00
|
|
|
//
|
|
|
|
// Simple logoff. Note that recording of @ logoff properties/stats
|
|
|
|
// occurs elsewhere!
|
|
|
|
//
|
2015-04-21 05:24:15 +00:00
|
|
|
var client = callingMenu.client;
|
|
|
|
|
|
|
|
setTimeout(function timeout() {
|
2015-08-21 19:47:01 +00:00
|
|
|
//
|
|
|
|
// For giggles...
|
|
|
|
//
|
|
|
|
client.term.write(
|
|
|
|
ansi.normal() + '\n' +
|
2015-08-27 05:04:04 +00:00
|
|
|
require('crypto').randomBytes(Math.floor(Math.random() * 65) + 20).toString(client.term.outputEncoding) +
|
2015-08-21 19:47:01 +00:00
|
|
|
'NO CARRIER');
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
client.end();
|
|
|
|
}, 500);
|
2015-09-24 04:24:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fallbackMenu(callingMenu, formData, extraArgs) {
|
|
|
|
callingMenu.client.fallbackMenuModule( { extraArgs : extraArgs }, function result(err) {
|
|
|
|
if(err) {
|
2015-09-24 05:41:06 +00:00
|
|
|
callingMenu.client.log.error( { error : err }, 'Error attempting to fallback!');
|
2015-09-24 04:24:37 +00:00
|
|
|
}
|
|
|
|
});
|
2015-09-24 05:41:06 +00:00
|
|
|
}
|