2015-04-07 04:29:45 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
2015-09-26 06:20:17 +00:00
|
|
|
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 Config = require('../core/config.js').config;
|
|
|
|
var sysMenuMethod = require('../core/system_menu_method.js');
|
|
|
|
var getDefaultMessageArea = require('../core/message_area.js').getDefaultMessageArea;
|
|
|
|
|
|
|
|
var util = require('util');
|
2015-04-07 04:29:45 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
exports.submitApplication = submitApplication;
|
|
|
|
|
|
|
|
function validateApplicationData(formData, cb) {
|
|
|
|
if(formData.value.username.length < Config.users.usernameMin) {
|
|
|
|
cb('Handle too short!', [ 1 ]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-10-22 19:01:16 +00:00
|
|
|
var invalidNames = Config.users.newUserNames + Config.users.badUserNames;
|
|
|
|
if(invalidNames.indexOf(formData.value.username.toLowerCase()) > -1) {
|
|
|
|
cb('Handle is blacklisted!', [ 1 ] );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-22 05:52:20 +00:00
|
|
|
if(isNaN(Date.parse(formData.value.birthdate))) {
|
2015-09-20 07:29:07 +00:00
|
|
|
cb('Invalid birthdate!', [ 3 ] );
|
2015-07-22 05:52:20 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
if(formData.value.password.length < Config.users.passwordMin) {
|
|
|
|
cb('Password too short!', [ 9, 10 ]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-24 05:00:48 +00:00
|
|
|
if(formData.value.password !== formData.value.passwordConfirm) {
|
2015-04-21 04:50:58 +00:00
|
|
|
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);
|
2015-04-15 04:27:07 +00:00
|
|
|
}
|
2015-04-21 04:50:58 +00:00
|
|
|
});
|
2015-04-07 04:29:45 +00:00
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
function submitApplication(callingMenu, formData, extraArgs) {
|
|
|
|
var client = callingMenu.client;
|
|
|
|
var menuConfig = callingMenu.menuConfig;
|
|
|
|
var menuViewController = callingMenu.viewControllers.menu;
|
2015-04-07 04:29:45 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
var views = {
|
|
|
|
username : menuViewController.getView(1),
|
|
|
|
password : menuViewController.getView(9),
|
|
|
|
confirm : menuViewController.getView(10),
|
|
|
|
errorMsg : menuViewController.getView(11)
|
|
|
|
};
|
2015-04-07 04:29:45 +00:00
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
validateApplicationData(formData, function validationResult(errorMsg, viewIds) {
|
|
|
|
if(errorMsg) {
|
|
|
|
views.errorMsg.setText(errorMsg);
|
|
|
|
|
|
|
|
viewIds.forEach(function formId(id) {
|
|
|
|
menuViewController.getView(id).clearText('');
|
|
|
|
});
|
|
|
|
|
|
|
|
menuViewController.switchFocus(viewIds[0]);
|
|
|
|
} else {
|
|
|
|
// Seems legit!
|
2015-07-21 04:56:48 +00:00
|
|
|
// :TODO: All of this should be a system API, not a mod, e.g. createNewUser(...)
|
2015-04-21 04:50:58 +00:00
|
|
|
var newUser = new user.User();
|
|
|
|
|
|
|
|
newUser.username = formData.value.username;
|
|
|
|
|
|
|
|
newUser.properties = {
|
2015-09-26 06:20:17 +00:00
|
|
|
real_name : formData.value.realName,
|
|
|
|
birthdate : new Date(Date.parse(formData.value.birthdate)).toISOString(),
|
|
|
|
sex : formData.value.sex,
|
|
|
|
location : formData.value.location,
|
|
|
|
affiliation : formData.value.affils,
|
|
|
|
email_address : formData.value.email,
|
|
|
|
web_address : formData.value.web,
|
|
|
|
account_created : new Date().toISOString(),
|
|
|
|
|
|
|
|
message_area_name : getDefaultMessageArea().name,
|
2015-10-19 03:24:13 +00:00
|
|
|
|
|
|
|
term_height : client.term.termHeight,
|
|
|
|
term_width : client.term.termWidth,
|
2015-04-21 04:50:58 +00:00
|
|
|
|
2015-05-11 22:39:28 +00:00
|
|
|
// :TODO: This is set in User.create() -- proabbly don't need it here:
|
2015-05-12 22:34:11 +00:00
|
|
|
//account_status : Config.users.requireActivation ? user.User.AccountStatus.inactive : user.User.AccountStatus.active,
|
2015-04-21 04:50:58 +00:00
|
|
|
|
|
|
|
// :TODO: Other defaults
|
2015-05-11 22:39:28 +00:00
|
|
|
// :TODO: should probably have a place to create defaults/etc.
|
2015-04-21 04:50:58 +00:00
|
|
|
};
|
|
|
|
|
2015-04-21 05:24:15 +00:00
|
|
|
if('*' === Config.defaults.theme) {
|
|
|
|
newUser.properties.theme_id = theme.getRandomTheme();
|
|
|
|
} else {
|
|
|
|
newUser.properties.theme_id = Config.defaults.theme;
|
|
|
|
}
|
|
|
|
|
2015-04-21 04:50:58 +00:00
|
|
|
newUser.create( { password : formData.value.password }, function created(err) {
|
|
|
|
if(err) {
|
2015-09-20 07:29:07 +00:00
|
|
|
Log.info( { error : err, username : formData.value.username }, 'New user creation failed');
|
|
|
|
|
2015-11-04 06:25:35 +00:00
|
|
|
callingMenu.gotoMenu(extraArgs.error, function result(err) {
|
|
|
|
if(err) {
|
|
|
|
callingMenu.prevMenu();
|
|
|
|
}
|
|
|
|
});
|
2015-04-21 04:50:58 +00:00
|
|
|
} else {
|
|
|
|
Log.info( { username : formData.value.username, userId : newUser.userId }, 'New user created');
|
|
|
|
|
|
|
|
if(user.User.AccountStatus.inactive === client.user.properties.account_status) {
|
2015-11-04 06:25:35 +00:00
|
|
|
callingMenu.gotoMenu(extraArgs.inactive);
|
2015-04-21 04:50:58 +00:00
|
|
|
} else {
|
2015-09-26 06:20:17 +00:00
|
|
|
//
|
|
|
|
// If active now, we need to call login() to authenticate
|
|
|
|
//
|
|
|
|
sysMenuMethod.login(callingMenu, formData, extraArgs);
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-04-07 04:29:45 +00:00
|
|
|
});
|
2015-04-21 04:50:58 +00:00
|
|
|
}
|