diff options
| author | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-26 01:49:05 +0000 |
|---|---|---|
| committer | nanalelfe <nargiza.nosirova@mail.utoronto.ca> | 2016-07-26 01:49:05 +0000 |
| commit | 203071f005057ebb186ddd3e0ada8629420ed409 (patch) | |
| tree | 7ac20e7fda8f9f3ca5d00ceee140e9701d44dcf7 /node_simple.js | |
| parent | 0d8073039faa903ee0f12e68064c89b5f27cdf14 (diff) | |
Added add_user: extra check if username is taken by an admin (and nice versa). Fixed Answered, Inbox and comments fields in user_profile
Diffstat (limited to 'node_simple.js')
| -rw-r--r-- | node_simple.js | 49 |
1 files changed, 33 insertions, 16 deletions
diff --git a/node_simple.js b/node_simple.js index 596cef6..228e861 100644 --- a/node_simple.js +++ b/node_simple.js @@ -552,10 +552,20 @@ exports.find_user_name = function (user_name, callback) { .then(function (db) { var logins = db.collection('logins'); + var admins = db.collection('admins'); logins.find( { user_name: user_name } ).toArray(function (err, result) { if (err) throw err; - else if (result.length == 0) { // nothing was found so this user is new - callback(false); + else if (result.length == 0) { // nothing was found in users + // check if user_name is taken by admin + admins.find({username : user_name}).toArray(function(err, data) { + if (err) throw err; + else if (data.length == 0) { + callback(false); + } + else { + callback(true); + } + }); } else { callback(true); @@ -1240,25 +1250,32 @@ exports.addAdmin = function (admin_data, callback) { console.log('addAdmin, admin_data: ' + admin_data.fname); console.log("inside addAdmin"); - + var logins = db.collection('logins'); // Check if the admin username already exists. Also check for user username. If it does, then we don't add the admin_data and return a message. exports.adminExists( admin_data.username , function (error, exists, data, message) { console.log("adminExists: " + message); if (!exists && !error) { - mongoFactory.getConnection(uri).then(function (db) { - - var admins = db.collection('admins'); - - admins.insertOne( admin_data, function (err) { - if (err) { - callback(false, true, message); - } - else { - callback(true, false, "Admin added."); - } - - }); + logins.find({username: username}).toArray(function (err, result) { + if (err){ + callback(true, false, null, "Error: could not retreive logins collection in addAdmin()."); + } else if (result.length) { + callback(false, false, 'User with given username already exists.'); + } else { + mongoFactory.getConnection(uri).then(function (db) { + + var admins = db.collection('admins'); + + admins.insertOne(admin_data, function (err) { + if (err) { + callback(false, true, message); + } + else { + callback(true, false, "Admin added."); + } + }); + }); + } }).catch(function (err) { callbackUser(false, true, "Unable to connect."); }) |
