From 8a510c340bb52b63f8e98ca866a589208ff044d2 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 22 May 2014 10:21:01 -0400 Subject: [PATCH] limiting user and group creation so that their slugs can't intersect. --- src/groups.js | 3 ++- src/meta.js | 12 +++++++++++- src/socket.io/user.js | 3 ++- src/user/create.js | 4 +++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/groups.js b/src/groups.js index d7ca7592ca..102d818c3b 100644 --- a/src/groups.js +++ b/src/groups.js @@ -4,6 +4,7 @@ var async = require('async'), winston = require('winston'), user = require('./user'), + meta = require('./meta'), db = require('./database'), utils = require('../public/src/utils'), @@ -182,7 +183,7 @@ var system = true; } - Groups.exists(name, function (err, exists) { + meta.userOrGroupExists(name, function (err, exists) { if (err) { return callback(err); } diff --git a/src/meta.js b/src/meta.js index 90199e29fa..a655249b1f 100644 --- a/src/meta.js +++ b/src/meta.js @@ -15,7 +15,8 @@ var fs = require('fs'), translator = require('./../public/src/translator'), db = require('./database'), plugins = require('./plugins'), - user = require('./user'); + user = require('./user'), + groups = require('./groups'); (function (Meta) { Meta.restartRequired = false; @@ -521,6 +522,15 @@ var fs = require('fs'), }; /* Assorted */ + Meta.userOrGroupExists = function(slug, callback) { + async.parallel([ + async.apply(user.exists, slug), + async.apply(groups.exists, slug) + ], function(err, results) { + callback(err, results.some(function(result) { return result })); + }); + }; + Meta.restart = function() { if (process.send) { process.send({ diff --git a/src/socket.io/user.js b/src/socket.io/user.js index c03b4cef13..a0ae6c65f9 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -2,6 +2,7 @@ var async = require('async'), user = require('../user'), + groups = require('../groups'), topics = require('../topics'), utils = require('./../../public/src/utils'), meta = require('../meta'), @@ -9,7 +10,7 @@ var async = require('async'), SocketUser.exists = function(socket, data, callback) { if (data && data.username) { - user.exists(utils.slugify(data.username), callback); + meta.userOrGroupExists(utils.slugify(data.username), callback); } }; diff --git a/src/user/create.js b/src/user/create.js index dda4f1743d..95bd7c154a 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -6,6 +6,7 @@ var async = require('async'), validator = require('validator'), plugins = require('../plugins'), groups = require('../groups'), + meta = require('../meta'), notifications = require('../notifications'), translator = require('../../public/src/translator'); @@ -43,10 +44,11 @@ module.exports = function(User) { } }, function(next) { - User.exists(userData.userslug, function(err, exists) { + meta.userOrGroupExists(userData.userslug, function(err, exists) { if (err) { return next(err); } + if (exists) { async.forever(function(next) { var newUsername = userData.username + (Math.floor(Math.random() * 255) + 1);