From 642306e69c9d50e87926ffebeb884716b90aae8a Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 23 Mar 2017 16:00:22 -0600 Subject: [PATCH 1/2] Fix #5543 --- src/user/create.js | 34 +++++++++++++++++----------------- test/user.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/src/user/create.js b/src/user/create.js index 2620cb67d3..ab413a545d 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -46,7 +46,7 @@ module.exports = function (User) { async.parallel({ renamedUsername: function (next) { - renameUsername(userData, next); + User.renameUsername(userData, next); }, userData: function (next) { plugins.fireHook('filter:user.create', { user: userData, data: data }, next); @@ -200,28 +200,28 @@ module.exports = function (User) { callback(); }; - function renameUsername(userData, callback) { + User.renameUsername = function (userData, callback) { meta.userOrGroupExists(userData.userslug, function (err, exists) { if (err || !exists) { return callback(err); } - var newUsername = ''; - async.forever(function (next) { - newUsername = userData.username + (Math.floor(Math.random() * 255) + 1); - User.existsBySlug(newUsername, function (err, exists) { - if (err) { - return callback(err); - } - if (!exists) { - next(newUsername); - } else { - next(); + var num = 0; + + function go() { + var username = userData.username + ' ' + num.toString(32); + var userslug = utils.slugify(username); + meta.userOrGroupExists(userslug, function (err, exists) { + if (err || !exists) { + return callback(err, username); } + + num += 1; + go(); }); - }, function (username) { - callback(null, username); - }); + } + + go(); }); - } + }; }; diff --git a/test/user.js b/test/user.js index 776dbf28de..c83e8cd411 100644 --- a/test/user.js +++ b/test/user.js @@ -70,6 +70,38 @@ describe('User', function () { }); }); + describe('.renameUsername()', function () { + it('should deal with collisions', function (done) { + var users = []; + for (var i = 0; i < 10; i += 1) { + users.push({ + username: 'Jane Doe', + password: 'abcdefghi', + email: 'jane.doe' + i + '@example.com', + }); + } + + async.series([ + function (next) { + async.eachSeries(users, function (user, next) { + User.create(user, next); + }, next); + }, + function (next) { + User.renameUsername({ + username: 'Jane Doe', + userslug: 'jane-doe', + }, function (err, username) { + assert.ifError(err); + + assert.strictEqual(username, 'Jane Doe 9'); + done(); + }); + }, + ], done); + }); + }); + describe('.isModerator()', function () { it('should return false', function (done) { User.isModerator(testUid, testCid, function (err, isModerator) { From a7677063de5f5d1dbc0b087934bc024694e8388b Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Fri, 24 Mar 2017 10:33:38 -0600 Subject: [PATCH 2/2] Rename to `uniqueUsername` --- src/user/create.js | 4 ++-- test/user.js | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/user/create.js b/src/user/create.js index ab413a545d..c629f76020 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -46,7 +46,7 @@ module.exports = function (User) { async.parallel({ renamedUsername: function (next) { - User.renameUsername(userData, next); + User.uniqueUsername(userData, next); }, userData: function (next) { plugins.fireHook('filter:user.create', { user: userData, data: data }, next); @@ -200,7 +200,7 @@ module.exports = function (User) { callback(); }; - User.renameUsername = function (userData, callback) { + User.uniqueUsername = function (userData, callback) { meta.userOrGroupExists(userData.userslug, function (err, exists) { if (err || !exists) { return callback(err); diff --git a/test/user.js b/test/user.js index c83e8cd411..08d3ba42d4 100644 --- a/test/user.js +++ b/test/user.js @@ -70,7 +70,7 @@ describe('User', function () { }); }); - describe('.renameUsername()', function () { + describe('.uniqueUsername()', function () { it('should deal with collisions', function (done) { var users = []; for (var i = 0; i < 10; i += 1) { @@ -88,7 +88,7 @@ describe('User', function () { }, next); }, function (next) { - User.renameUsername({ + User.uniqueUsername({ username: 'Jane Doe', userslug: 'jane-doe', }, function (err, username) {