* clearScreen -> cls for nostalgia

* module cleanup: some simple modules moved to general_menu_method.js @methods
* More work on menu configuration & options
- Removed formatting of args for now. Too tied to MCI, not really needed with argName stuff
This commit is contained in:
Bryan Ashby
2015-04-20 22:50:58 -06:00
parent 8db72430d3
commit ec5f1836c5
16 changed files with 350 additions and 1233 deletions

View File

@@ -12,132 +12,101 @@ var Config = require('../core/config.js').config;
var util = require('util');
//var async = require('async');
exports.submitApplication = submitApplication;
// :TODO: clean up requires
function validateApplicationData(formData, cb) {
if(formData.value.username.length < Config.users.usernameMin) {
cb('Handle too short!', [ 1 ]);
return;
}
exports.moduleInfo = {
name : 'Apply',
desc : 'Application Module',
author : 'NuSkooler',
};
if(formData.value.username.length > Config.users.usernameMax) {
cb('Handle too long!', [ 1 ]);
return;
}
exports.getModule = ApplyModule;
var re = new RegExp(Config.users.usernamePattern);
if(!re.test(formData.value.username)) {
cb('Handle contains invalid characters!', [ 1 ] );
return;
}
if(formData.value.password.length < Config.users.passwordMin) {
cb('Password too short!', [ 9, 10 ]);
return;
}
function ApplyModule(menuConfig) {
MenuModule.call(this, menuConfig);
if(formData.value.password !== formData.value.pwConfirm) {
cb('Passwords do not match!', [ 9, 10 ]);
return;
}
var self = this;
this.menuMethods.submitApplication = function(formData, extraArgs) {
var usernameView = self.viewController.getView(1);
var passwordView = self.viewController.getView(9);
var pwConfirmView = self.viewController.getView(10);
var statusView = self.viewController.getView(11);
self.validateApplication(formData, function validated(errString, clearFields) {
if(errString) {
statusView.setText(errString);
clearFields.forEach(function formId(id) {
self.viewController.getView(id).setText('');
});
self.viewController.switchFocus(clearFields[0]);
} else {
var newUser = new user.User();
newUser.username = formData.value.username;
newUser.properties = {
real_name : formData.value.realName,
age : formData.value.age,
sex : formData.value.sex,
location : formData.value.location,
affiliation : formData.value.affils,
email_address : formData.value.email,
web_address : formData.value.web,
art_theme_id : Config.defaults.theme, // :TODO: allow '*' = random
account_status : user.User.AccountStatus.inactive,
// :TODO: Other defaults
// :TODO: should probably have a place to create defaults/etc.
// :TODO: set account_status to default based on Config.user...
};
newUser.create({ password : formData.value.pw }, function created(err) {
if(err) {
self.client.gotoMenuModule( { name : extraArgs.error } );
} else {
Log.info( { username : formData.value.username, userId : newUser.userId }, 'New user created');
if(user.User.AccountStatus.inactive === self.client.user.properties.account_status) {
self.client.gotoMenuModule( { name : extraArgs.inactive } );
} else {
self.client.gotoMenuModule( { name : this.menuConfig.next } );
}
}
});
}
});
};
this.validateApplication = function(formData, cb) {
if(formData.value.username.length < Config.users.usernameMin) {
cb('Handle too short!', [ 1 ]);
return;
user.getUserIdAndName(formData.value.username, function userIdAndName(err) {
var alreadyExists = !err;
if(alreadyExists) {
cb('Username unavailable!', [ 1 ] );
} else {
cb(null);
}
if(formData.value.username.length > Config.users.usernameMax) {
cb('Handle too long!', [ 1 ]);
return;
}
var re = new RegExp(Config.users.usernamePattern);
if(!re.test(formData.value.username)) {
cb('Handle contains invalid characters!', [ 1 ] );
return;
}
if(formData.value.pw.length < Config.users.passwordMin) {
cb('Password too short!', [ 9, 10 ]);
return;
}
if(formData.value.pw !== formData.value.pwConfirm) {
cb('Passwords do not match!', [ 9, 10 ]);
return;
}
user.getUserIdAndName(formData.value.username, function userIdAndName(err) {
var alreadyExists = !err;
if(alreadyExists) {
cb('Username unavailable!', [ 1 ] );
} else {
cb(null);
}
});
};
});
}
util.inherits(ApplyModule, MenuModule);
function submitApplication(callingMenu, formData, extraArgs) {
var client = callingMenu.client;
var menuConfig = callingMenu.menuConfig;
var menuViewController = callingMenu.viewControllers.menu;
ApplyModule.prototype.enter = function(client) {
ApplyModule.super_.prototype.enter.call(this, client);
};
var views = {
username : menuViewController.getView(1),
password : menuViewController.getView(9),
confirm : menuViewController.getView(10),
errorMsg : menuViewController.getView(11)
};
ApplyModule.prototype.beforeArt = function() {
ApplyModule.super_.prototype.beforeArt.call(this);
};
validateApplicationData(formData, function validationResult(errorMsg, viewIds) {
if(errorMsg) {
views.errorMsg.setText(errorMsg);
ApplyModule.prototype.mciReady = function(mciData) {
ApplyModule.super_.prototype.mciReady.call(this, mciData);
viewIds.forEach(function formId(id) {
menuViewController.getView(id).clearText('');
});
var self = this;
menuViewController.switchFocus(viewIds[0]);
} else {
// Seems legit!
var newUser = new user.User();
self.viewController = self.addViewController(new ViewController({ client : self.client } ));
self.viewController.loadFromMCIMapAndConfig( { mciMap : mciData.menu, menuConfig : self.menuConfig }, function onViewReady(err) {
newUser.username = formData.value.username;
newUser.properties = {
real_name : formData.value.realName,
age : formData.value.age,
sex : formData.value.sex,
location : formData.value.location,
affiliation : formData.value.affils,
email_address : formData.value.email,
web_address : formData.value.web,
theme_id : Config.defaults.theme, // :TODO: allow '*' = random
account_status : Config.users.requireActivation ? user.User.AccountStatus.inactive : user.User.AccountStatus.active,
// :TODO: Other defaults
// :TODO: should probably have a place to create defaults/etc.
};
newUser.create( { password : formData.value.password }, function created(err) {
if(err) {
client.gotoMenuModule( { name : extraArgs.error } );
} else {
Log.info( { username : formData.value.username, userId : newUser.userId }, 'New user created');
if(user.User.AccountStatus.inactive === client.user.properties.account_status) {
client.gotoMenuModule( { name : extraArgs.inactive } );
} else {
client.gotoMenuModule( { name : menuConfig.next } );
}
}
});
}
});
};
}

View File

@@ -1,29 +1,14 @@
/* jslint node: true */
'use strict';
var ansi = require('../core/ansi_term.js');
var art = require('../core/art.js');
var user = require('../core/user.js');
var theme = require('../core/theme.js');
var Log = require('../core/logger.js').log;
var MenuModule = require('../core/menu_module.js').MenuModule;
var ViewController = require('../core/view_controller.js').ViewController;
var async = require('async');
// :TODO: clean up requires
exports.login = login;
/*exports.moduleInfo = {
name : 'Login',
desc : 'Login Module',
author : 'NuSkooler',
};*/
//exports.getModule = LoginModule;
exports.attemptLogin = attemptLogin;
function attemptLogin(callingMenu, formData, extraArgs) {
function login(callingMenu, formData, extraArgs) {
var client = callingMenu.client;
client.user.authenticate(formData.value.username, formData.value.password, function authenticated(err) {
@@ -36,99 +21,18 @@ function attemptLogin(callingMenu, formData, extraArgs) {
Log.info( { username : callingMenu.client.user.username }, 'Successful login');
async.parallel(
[
function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.art_theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
callback(null);
});
}
],
function complete(err, results) {
client.gotoMenuModule( { name : callingMenu.menuConfig.next } );
[
function loadThemeConfig(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
callback(null);
});
}
);
],
function complete(err, results) {
client.gotoMenuModule( { name : callingMenu.menuConfig.next } );
}
);
}
});
}
/*
function LoginModule(menuConfig) {
MenuModule.call(this, menuConfig);
var self = this;
// :TODO: Handle max login attempts before hangup
// :TODO: probably should persist failed login attempts to user DB
this.menuMethods.attemptLogin = function(args) {
self.client.user.authenticate(args.username, args.password, function onAuth(err) {
if(err) {
// :TODO: change to simple login/username prompts - no buttons.
Log.info( { username : args.username }, 'Failed login attempt %s', err);
// :TODO: localize:
// :TODO: create a blink label of sorts - simulate blink with ICE
self.viewController.getView(5).setText('Invalid username or password!');
self.clearForm();
self.viewController.switchFocus(1);
setTimeout(function onTimeout() {
// :TODO: should there be a block input type of pattern here? self.client.ignoreInput() ... self.client.acceptInput()
self.viewController.getView(5).clearText(); // :TODO: for some reason this doesn't clear the last character
self.viewController.switchFocus(1);
}, 2000);
} else {
Log.info( { username : self.client.user.username }, 'Successful login');
// :TODO: persist information about login to user
async.parallel(
[
function loadThemeConfig(callback) {
theme.getThemeInfo(self.client.user.properties.art_theme_id, function themeInfo(err, info) {
self.client.currentThemeInfo = info;
callback(null);
});
}
],
function complete(err, results) {
self.client.gotoMenuModule( { name : args.next.success } );
}
);
}
});
};
this.clearForm = function() {
[ 1, 2, ].forEach(function onId(id) {
self.viewController.getView(id).clearText();
});
};
}
require('util').inherits(LoginModule, MenuModule);
LoginModule.prototype.enter = function(client) {
LoginModule.super_.prototype.enter.call(this, client);
};
LoginModule.prototype.beforeArt = function() {
LoginModule.super_.prototype.beforeArt.call(this);
//this.client.term.write(ansi.resetScreen());
};
LoginModule.prototype.mciReady = function(mciData) {
LoginModule.super_.prototype.mciReady.call(this, mciData);
var self = this;
self.viewController = self.addViewController(new ViewController( { client : self.client } ));
self.viewController.loadFromMCIMapAndConfig( { mciMap : mciData.menu, menuConfig : self.menuConfig }, function onViewReady(err) {
});
};
*/

View File

@@ -16,16 +16,17 @@
"focus" : ...
}
..note that script/methods should be part of a *theme* - or at least checked first with fallback
.....why?
NOte that @draw & @art should check theme first.
@draw:myMethod -> theme/draw.js::myMethod(opts)
}
}
*/
"connected" : {
"art" : "connect",
"art" : "CONNECT",
"next" : "matrix",
"options" : {
"clearScreen" : true,
"cls" : true,
"nextTimeout" : 1500
}
},
@@ -62,7 +63,7 @@
}
},
"options" : {
"clearScreen" : true
"cls" : true
}
},
"login" : {
@@ -70,22 +71,35 @@
"prompt" : "userCredentials",
"fallback" : "matrix",
"next" : "newUserActive",
"action" : "@method:login.js/attemptLogin",
"action" : "@method:general_menu_methods/login",
// :TODO: support alt submit method for prompts
// if present, standard filters apply. No need for multiple submit ID's
// since a prompt can only utilize one:
"submit" : [
{
"value" : { "1" : "thing" },
"action" : "@method:doThings"
}
],
"options" : {
"clearScreen" : true
"cls" : true
}
},
"logoff" : {
"art" : "logoff",
"module" : "logoff"
"art" : "LOGOFF",
//"module" : "logoff",
"action" : "@method:general_menu_methods/logoff",
"options" : { "cls" : true }
},
"apply" : {
"art" : "apply",
"module" : "apply",
"art" : "APPLY",
"next" : "newUserActive",
"form" : {
"0" : {
"BT12BT13ET1ET10ET2ET3ET4ET5ET6ET7ET8ET9TL11" : {
"cancelKeys" : [ "esc" ],
"mci" : {
"ET1" : {
"focus" : true,
@@ -98,7 +112,7 @@
"ET6" : { "argName" : "affils" },
"ET7" : { "argName" : "email" },
"ET8" : { "argName" : "web" },
"ET9" : { "argName" : "pw" },
"ET9" : { "argName" : "password" },
"ET10" : { "argName" : "pwConfirm" },
"BT12" : {
"submit" : true,
@@ -112,8 +126,8 @@
"submit" : {
"12" : [ // Apply
{
"value" : { "12" : null },
"action" : "@method:submitApplication",
"value" : 12,
"action" : "@method:apply/submitApplication",
"extraArgs" : {
"inactive" : "userNeedsActivated",
"error" : "newUserCreateError"
@@ -122,7 +136,7 @@
],
"13" : [ // Cancel
{
"value" : { "13" : null }, // :TODO: allow just "13" (number)
"value" : 13,
"action" : "@menu:matrix"
}
]
@@ -131,14 +145,14 @@
}
},
"options" : {
"clearScreen" : true
"cls" : true
}
},
"newUserActive" : {
"art" : "STATS",
"options" : {
// :TODO: implement MCI codes for this
"clearScreen" : true
"cls" : true
}
}
}

View File

@@ -1,92 +0,0 @@
/* jslint node: true */
'use strict';
var ansi = require('../core/ansi_term.js');
var MenuModule = require('../core/menu_module.js').MenuModule;
var ViewController = require('../core/view_controller.js').ViewController;
var menuUtil = require('../core/menu_util.js');
var _ = require('lodash');
exports.getModule = StandardMenuModule;
exports.moduleInfo = {
name : 'Standard Menu Module',
desc : 'Menu module handling most standard stuff',
author : 'NuSkooler',
};
function StandardMenuModule(menuConfig) {
MenuModule.call(this, menuConfig);
}
require('util').inherits(StandardMenuModule, MenuModule);
StandardMenuModule.prototype.enter = function(client) {
StandardMenuModule.super_.prototype.enter.call(this, client);
};
StandardMenuModule.prototype.beforeArt = function() {
StandardMenuModule.super_.prototype.beforeArt.call(this);
};
StandardMenuModule.prototype.mciReady = function(mciData) {
StandardMenuModule.super_.prototype.mciReady.call(this, mciData);
var self = this;
//
// A quick rundown:
// * We may have mciData.menu, mciData.prompt, or both.
// * Prompt form is favored over menu form if both are present.
// * Standard/prefdefined MCI entries must load both (e.g. %BN is expected to resolve)
//
// :TODO: Create MenuModule.standardMciReady() method that others can call that does this -- even custom modules will generally want most of this
self.viewControllers = {};
var vcOpts = { client : self.client };
if(mciData.menu) {
self.viewControllers.menu = new ViewController(vcOpts);
}
if(mciData.prompt) {
self.viewControllers.prompt = new ViewController(vcOpts);
}
var viewsReady = function(err) {
// :TODO: Hrm.....
};
if(self.viewControllers.menu) {
var menuLoadOpts = {
mciMap : mciData.menu,
callingMenu : self,
//menuConfig : self.menuConfig,
withoutForm : _.isObject(mciData.prompt),
};
self.viewControllers.menu.loadFromMenuConfig(menuLoadOpts, viewsReady);
}
if(self.viewControllers.prompt) {
var promptLoadOpts = {
callingMenu : self,
mciMap : mciData.prompt,
};
self.viewControllers.prompt.loadFromPromptConfig(promptLoadOpts, viewsReady);
}
/*
var vc = self.addViewController(new ViewController({ client : self.client } ));
vc.loadFromMCIMapAndConfig( { mciMap : mciData.menu, menuConfig : self.menuConfig }, function onViewReady(err) {
if(err) {
console.log(err);
} else {
}
});
*/
};

View File

@@ -23,7 +23,7 @@ function entryPoint(client) {
async.waterfall(
[
function getArt(callback) {
theme.getThemeArt('MCI_VM1.ANS', client.user.properties.art_theme_id, function onArt(err, theArt) {
theme.getThemeArt('MCI_VM1.ANS', client.user.properties.theme_id, function onArt(err, theArt) {
callback(err, theArt);
});
},