resolved #1372 -- username clashes will now cause a random # to be appended to the username

This commit is contained in:
Julian Lam
2014-04-15 00:17:35 -04:00
parent f892a6793a
commit 4c9ea62c0d
3 changed files with 56 additions and 8 deletions

View File

@@ -1,11 +1,13 @@
'use strict';
var async = require('async'),
db = require('./../database'),
utils = require('./../../public/src/utils'),
db = require('../database'),
utils = require('../../public/src/utils'),
validator = require('validator'),
plugins = require('./../plugins'),
groups = require('./../groups');
plugins = require('../plugins'),
groups = require('../groups'),
notifications = require('../notifications'),
translator = require('../../public/src/translator');
module.exports = function(User) {
@@ -45,7 +47,23 @@ module.exports = function(User) {
if (err) {
return next(err);
}
next(exists ? new Error('[[error:username-taken]]') : null);
if (exists) {
async.forever(function(next) {
// Append a random number to the username
var newUsername = userData.username + (Math.floor(Math.random() * 255) + 1);
User.exists(newUsername, function(err, exists) {
if (!exists) {
next(newUsername);
} else {
next();
}
});
}, function(username) {
next(null, username);
});
} else {
next();
}
});
},
function(next) {
@@ -71,6 +89,12 @@ module.exports = function(User) {
}
userData = results[results.length - 1];
// If a new username was picked...
if (results[3]) {
userData.username = results[3];
userData.userslug = utils.slugify(results[3]);
}
db.incrObjectField('global', 'nextUid', function(err, uid) {
if(err) {
return callback(err);
@@ -125,6 +149,19 @@ module.exports = function(User) {
groups.join('registered-users', uid);
// If their username was automatically changed...
if (results[3]) {
translator.translate('[[user:username_taken_workaround, ' + userData.username + ']]', function(notifText) {
notifications.create({
text: notifText,
picture: 'brand:logo',
datetime: Date.now()
}, function(nid) {
notifications.push(nid, uid);
});
});
}
if (password) {
User.hashPassword(password, function(err, hash) {
if(err) {