mirror of
https://github.com/NuSkooler/enigma-bbs.git
synced 2025-06-06 12:47:13 +02:00
* Separate login logic vs display
* Work on SSH a bit -- major WIP, not working!
This commit is contained in:
parent
a6f15c2dfc
commit
d86d3e0119
4 changed files with 204 additions and 136 deletions
|
@ -6,6 +6,7 @@ var clientConnections = require('./client_connections.js').clientConnections;
|
|||
var ansi = require('./ansi_term.js');
|
||||
var userDb = require('./database.js').dbs.user;
|
||||
var sysProp = require('./system_property.js');
|
||||
var userLogin = require('./user_login.js').userLogin;
|
||||
|
||||
var async = require('async');
|
||||
var _ = require('lodash');
|
||||
|
@ -17,52 +18,16 @@ exports.fallbackMenu = fallbackMenu;
|
|||
function login(callingMenu, formData, extraArgs) {
|
||||
var client = callingMenu.client;
|
||||
|
||||
client.user.authenticate(formData.value.username, formData.value.password, function authenticated(err) {
|
||||
userLogin(callingMenu.client, formData.value.username, formData.value.password, function authResult(err) {
|
||||
if(err) {
|
||||
client.log.info( { username : formData.value.username }, 'Failed login attempt %s', err);
|
||||
|
||||
// :TODO: if username exists, record failed login attempt to properties
|
||||
// :TODO: check Config max failed logon attempts/etc.
|
||||
|
||||
client.fallbackMenuModule();
|
||||
//client.gotoMenuModule( { name : callingMenu.menuConfig.fallback } );
|
||||
} else {
|
||||
var now = new Date();
|
||||
var user = callingMenu.client.user;
|
||||
|
||||
//
|
||||
// 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( {
|
||||
existingClientId : existingClientConnection.session.id,
|
||||
username : user.username,
|
||||
userId : user.userId },
|
||||
'Already logged in'
|
||||
);
|
||||
|
||||
// login failure
|
||||
if(err.existingConn) {
|
||||
client.term.rawWrite(ansi.resetScreen());
|
||||
|
||||
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,
|
||||
font : _.has(callingMenu, 'menuConfig.config.tooNode.font') ? callingMenu.menuConfig.config.tooNode.font : null,
|
||||
name : _.has(callingMenu, 'menuConfig.config.tooNode.art') ? callingMenu.menuConfig.config.tooNode.art : 'TOONODE',
|
||||
};
|
||||
|
||||
theme.displayThemeArt(artOpts, function artDisplayed(err) {
|
||||
|
@ -76,66 +41,14 @@ function login(callingMenu, formData, extraArgs) {
|
|||
});
|
||||
|
||||
return;
|
||||
} else {
|
||||
// Other error
|
||||
client.fallbackMenuModule();
|
||||
}
|
||||
|
||||
|
||||
// use client.user so we can get correct case
|
||||
client.log.info( { username : user.username }, 'Successful login');
|
||||
|
||||
async.parallel(
|
||||
[
|
||||
function loadThemeConfig(callback) {
|
||||
theme.loadTheme(user.properties.theme_id, function themeLoaded(err, theme) {
|
||||
client.currentTheme = theme;
|
||||
callback(null); // always non-fatal
|
||||
});
|
||||
},
|
||||
function updateSystemLoginCount(callback) {
|
||||
var sysLoginCount = sysProp.getSystemProperty('login_count') || 0;
|
||||
sysLoginCount = parseInt(sysLoginCount, 10) + 1;
|
||||
sysProp.persistSystemProperty('login_count', sysLoginCount, callback);
|
||||
},
|
||||
function recordLastLogin(callback) {
|
||||
user.persistProperty('last_login_timestamp', now.toISOString(), function persisted(err) {
|
||||
callback(err);
|
||||
});
|
||||
},
|
||||
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);
|
||||
});
|
||||
},
|
||||
function recordLoginHistory(callback) {
|
||||
userDb.serialize(function serialized() {
|
||||
userDb.run(
|
||||
'INSERT INTO user_login_history (user_id, user_name, timestamp) ' +
|
||||
'VALUES(?, ?, ?);', [ user.userId, user.username, now.toISOString() ]
|
||||
);
|
||||
|
||||
// keep 30 days of records
|
||||
userDb.run(
|
||||
'DELETE FROM user_login_history ' +
|
||||
'WHERE timestamp <= DATETIME("now", "-30 day");'
|
||||
);
|
||||
});
|
||||
|
||||
callback(null);
|
||||
}
|
||||
],
|
||||
function complete(err, results) {
|
||||
if(err) {
|
||||
client.log.error(err);
|
||||
// :TODO: drop the connection?
|
||||
}
|
||||
client.gotoMenuModule( { name : callingMenu.menuConfig.next } );
|
||||
}
|
||||
);
|
||||
} else {
|
||||
// success!
|
||||
client.gotoMenuModule( { name : callingMenu.menuConfig.next } );
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue