diff --git a/public/src/forum/admin/users.js b/public/src/forum/admin/users.js index 1d82b1ae11..764e8ca2d8 100644 --- a/public/src/forum/admin/users.js +++ b/public/src/forum/admin/users.js @@ -93,7 +93,7 @@ define(function() { var parent = adminBtn.parents('.users-box'); var isBanned = isUserBanned(adminBtn); var uid = getUID(adminBtn); - + if(uid === yourid){ app.alert({ title: 'Error', @@ -107,7 +107,7 @@ define(function() { adminBtn.attr('value', 'UnMake Admin').html('Remove Admin'); parent.attr('data-admin', 1); updateUserBanButtons(); - + } else if(uid !== yourid) { bootbox.confirm('Do you really want to remove this user as admin "' + parent.attr('data-username') + '"?', function(confirm) { if (confirm) { @@ -115,11 +115,11 @@ define(function() { adminBtn.attr('value', 'Make Admin').html('Make Admin'); parent.attr('data-admin', 0); updateUserBanButtons(); - + } }); } - + return false; }); @@ -187,8 +187,35 @@ define(function() { var username = $('#search-user').val(); jQuery('.fa-spinner').removeClass('none'); - socket.emit('api:admin.user.search', username); + socket.emit('api:admin.user.search', username, function(err, data) { + console.log(data); + if(err) { + return app.alertError(err.message); + } + + var html = templates.prepare(templates['admin/users'].blocks['users']).parse({ + users: data + }), + userListEl = document.querySelector('.users'); + + userListEl.innerHTML = html; + jQuery('.fa-spinner').addClass('none'); + + if (data && data.length === 0) { + $('#user-notfound-notify').html('User not found!') + .show() + .addClass('label-danger') + .removeClass('label-success'); + } else { + $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!') + .show() + .addClass('label-success') + .removeClass('label-danger'); + } + + initUsers(); + }); }, 250); }); @@ -196,32 +223,6 @@ define(function() { handleUserCreate(); - socket.removeAllListeners('api:admin.user.search'); - - socket.on('api:admin.user.search', function(data) { - var html = templates.prepare(templates['admin/users'].blocks['users']).parse({ - users: data - }), - userListEl = document.querySelector('.users'); - - userListEl.innerHTML = html; - jQuery('.fa-spinner').addClass('none'); - - if (data && data.length === 0) { - $('#user-notfound-notify').html('User not found!') - .show() - .addClass('label-danger') - .removeClass('label-success'); - } else { - $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!') - .show() - .addClass('label-success') - .removeClass('label-danger'); - } - - initUsers(); - }); - function onUsersLoaded(users) { var html = templates.prepare(templates['admin/users'].blocks['users']).parse({ users: users diff --git a/public/src/forum/users.js b/public/src/forum/users.js index 8df4fb5e89..20b50a0df8 100644 --- a/public/src/forum/users.js +++ b/public/src/forum/users.js @@ -47,39 +47,40 @@ define(function() { jQuery('#user-notfound-notify').html(''); setTimeout(function() { - socket.emit('api:admin.user.search', username); + socket.emit('api:admin.user.search', username, function(err, data) { + console.log(err, data); + if(err) { + return app.alert(err.message); + } + + if (!data) { + $('#user-notfound-notify').html('You need to be logged in to search!'); + $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); + return; + } + + var html = templates.prepare(templates['users'].blocks['users']).parse({ + users: data + }), + userListEl = $('#users-container'); + + userListEl.html(html); + + + if (data && data.length === 0) { + $('#user-notfound-notify').html('User not found!'); + $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); + } else { + $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!'); + $('#user-notfound-notify').parent().addClass('btn-success label-success'); + } + + }); }, 500); //replace this with global throttling function/constant }, 250); }); - socket.removeAllListeners('api:admin.user.search'); - - socket.on('api:admin.user.search', function(data) { - if (data === null) { - $('#user-notfound-notify').html('You need to be logged in to search!'); - $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); - return; - } - - var html = templates.prepare(templates['users'].blocks['users']).parse({ - users: data - }), - userListEl = $('#users-container'); - - userListEl.html(html); - - - if (data && data.length === 0) { - $('#user-notfound-notify').html('User not found!'); - $('#user-notfound-notify').parent().addClass('btn-warning label-warning'); - } else { - $('#user-notfound-notify').html(data.length + ' user' + (data.length > 1 ? 's' : '') + ' found!'); - $('#user-notfound-notify').parent().addClass('btn-success label-success'); - } - - }); - socket.on('api:user.isOnline', function(data) { if(getActiveSection() == 'online' && !loadingMoreUsers) { startLoading('users:online', 0, true); diff --git a/src/user.js b/src/user.js index ffc8c05026..8e604a2440 100644 --- a/src/user.js +++ b/src/user.js @@ -487,9 +487,9 @@ var bcrypt = require('bcrypt'), User.search = function(username, callback) { if (!username) { - callback([]); - return; + return callback([]); } + db.search('user', username, function(err, uids) { if (err) { console.log(err); diff --git a/src/websockets.js b/src/websockets.js index b52807b23b..deb3438f23 100644 --- a/src/websockets.js +++ b/src/websockets.js @@ -1056,15 +1056,30 @@ websockets.init = function(io) { }); socket.on('api:admin.user.search', function(username, callback) { - if (uid && uid > 0) { - user.search(username, function(data) { - if (!callback) socket.emit('api:admin.user.search', data); - else callback(null, data); - }); - } else { - if (!callback) socket.emit('api:admin.user.search', null); - else callback(); + if (!(uid && uid > 0)) { + return callback(); } + + user.search(username, function(data) { + function isAdmin(userData, next) { + user.isAdministrator(userData.uid, function(err, isAdmin) { + if(err) { + return next(err); + } + + userData.administrator = isAdmin?'1':'0'; + next(); + }); + } + + async.each(data, isAdmin, function(err) { + if(err) { + return callback({message: err.message}); + } + + callback(null, data); + }); + }); }); socket.on('api:admin.categories.search', function(username, cid, callback) {