* New @systemMethod

This commit is contained in:
Bryan Ashby 2015-04-20 23:24:15 -06:00
parent ec5f1836c5
commit 5ab89f952f
7 changed files with 99 additions and 41 deletions

View File

@ -13,6 +13,7 @@ var ALL_ASSETS = [
'art', 'art',
'menu', 'menu',
'method', 'method',
'systemMethod',
'prompt', 'prompt',
]; ];

View File

@ -209,25 +209,12 @@ function prepareClient(client, cb) {
if('*' === conf.config.preLoginTheme) { if('*' === conf.config.preLoginTheme) {
var theme = require('./theme.js'); var theme = require('./theme.js');
async.waterfall( client.user.properties.theme_id = theme.getRandomTheme() || '';
[
function getRandTheme(callback) { theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
theme.getRandomTheme(function randTheme(err, themeId) { client.currentThemeInfo = info;
client.user.properties.theme_id = themeId || ''; cb(null);
callback(null); });
});
},
function setCurrentThemeInfo(callback) {
theme.getThemeInfo(client.user.properties.theme_id, function themeInfo(err, info) {
client.currentThemeInfo = info;
callback(null);
});
}
],
function complete(err) {
cb(err);
}
);
} else { } else {
client.user.properties.theme_id = conf.config.preLoginTheme; client.user.properties.theme_id = conf.config.preLoginTheme;
cb(null); cb(null);

View File

@ -167,29 +167,34 @@ function handleAction(client, formData, conf) {
var actionAsset = asset.parseAsset(conf.action); var actionAsset = asset.parseAsset(conf.action);
assert(_.isObject(actionAsset)); assert(_.isObject(actionAsset));
function callModuleMenuMethod(path) {
if('' === paths.extname(path)) {
path += '.js';
}
try {
var methodMod = require(path);
methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs);
} catch(e) {
Log.error( { error : e.toString(), methodName : actionAsset.asset }, 'Failed to execute asset method');
}
}
switch(actionAsset.type) { switch(actionAsset.type) {
case 'method' : case 'method' :
case 'systemMethod' :
if(_.isString(actionAsset.location)) { if(_.isString(actionAsset.location)) {
try { callModuleMenuMethod(paths.join(Config.paths.mods, actionAsset.location));
// allow ".js" omission
if(''=== paths.extname(actionAsset.location)) {
actionAsset.location += '.js';
}
var methodMod = require(paths.join(Config.paths.mods, actionAsset.location));
if(_.isFunction(methodMod[actionAsset.asset])) {
methodMod[actionAsset.asset](client.currentMenuModule, formData, conf.extraArgs);
}
} catch(e) {
Log.error( { error : e, methodName : actionAsset.asset }, 'Failed to execute asset method');
}
} else { } else {
// local to current module if('systemMethod' === actionAsset.type) {
var currentModule = client.currentMenuModule; callModuleMenuMethod(paths.join(__dirname, 'system_menu_method.js'));
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) { } else {
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs); // local to current module
var currentModule = client.currentMenuModule;
if(_.isFunction(currentModule.menuMethods[actionAsset.asset])) {
currentModule.menuMethods[actionAsset.asset](formData, conf.extraArgs);
}
} }
} }
break; break;

View File

@ -0,0 +1,51 @@
/* jslint node: true */
'use strict';
var theme = require('../core/theme.js');
var Log = require('../core/logger.js').log;
var ansi = require('../core/ansi_term.js');
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) {
Log.info( { username : formData.value.username }, 'Failed login attempt %s', err);
client.gotoMenuModule( { name : callingMenu.menuConfig.fallback } );
} else {
// use client.user so we can get correct case
Log.info( { username : callingMenu.client.user.username }, 'Successful login');
async.parallel(
[
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 logoff(callingMenu, formData, extraArgs) {
var client = callingMenu.client;
// :TODO: record this.
setTimeout(function timeout() {
client.term.write(ansi.normal() + '\nATH0\n');
client.end();
}, 500);
}

View File

@ -94,6 +94,14 @@ function initAvailableThemes(cb) {
); );
} }
function getRandomTheme() {
if(Object.getOwnPropertyNames(availableThemes).length > 0) {
var themeIds = Object.keys(availableThemes);
return themeIds[Math.floor(Math.random() * themeIds.length)];
}
}
/*
function getRandomTheme(cb) { function getRandomTheme(cb) {
if(Object.getOwnPropertyNames(availableThemes).length > 0) { if(Object.getOwnPropertyNames(availableThemes).length > 0) {
var themeIds = Object.keys(availableThemes); var themeIds = Object.keys(availableThemes);
@ -102,6 +110,7 @@ function getRandomTheme(cb) {
cb(new Error('No themes available')); cb(new Error('No themes available'));
} }
} }
*/
function getThemeArt(name, themeID, options, cb) { function getThemeArt(name, themeID, options, cb) {
// allow options to be optional // allow options to be optional

View File

@ -87,13 +87,18 @@ function submitApplication(callingMenu, formData, extraArgs) {
email_address : formData.value.email, email_address : formData.value.email,
web_address : formData.value.web, 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, account_status : Config.users.requireActivation ? user.User.AccountStatus.inactive : user.User.AccountStatus.active,
// :TODO: Other defaults // :TODO: Other defaults
// :TODO: should probably have a place to create defaults/etc. // :TODO: should probably have a place to create defaults/etc.
}; };
if('*' === Config.defaults.theme) {
newUser.properties.theme_id = theme.getRandomTheme();
} else {
newUser.properties.theme_id = Config.defaults.theme;
}
newUser.create( { password : formData.value.password }, function created(err) { newUser.create( { password : formData.value.password }, function created(err) {
if(err) { if(err) {
client.gotoMenuModule( { name : extraArgs.error } ); client.gotoMenuModule( { name : extraArgs.error } );

View File

@ -71,7 +71,7 @@
"prompt" : "userCredentials", "prompt" : "userCredentials",
"fallback" : "matrix", "fallback" : "matrix",
"next" : "newUserActive", "next" : "newUserActive",
"action" : "@method:general_menu_methods/login", "action" : "@systemMethod:login",
// :TODO: support alt submit method for prompts // :TODO: support alt submit method for prompts
// if present, standard filters apply. No need for multiple submit ID's // if present, standard filters apply. No need for multiple submit ID's
@ -90,7 +90,7 @@
"logoff" : { "logoff" : {
"art" : "LOGOFF", "art" : "LOGOFF",
//"module" : "logoff", //"module" : "logoff",
"action" : "@method:general_menu_methods/logoff", "action" : "@systemMethod:logoff",
"options" : { "cls" : true } "options" : { "cls" : true }
}, },
"apply" : { "apply" : {