[ACP] Allow searching users by email.

This commit is contained in:
Xaekai 2017-05-24 04:10:44 -07:00
parent d23b5278b1
commit 93ef067b8c
4 changed files with 32 additions and 29 deletions

View File

@ -91,14 +91,26 @@ function handleGlobalBanDelete(user, data) {
} }
function handleListUsers(user, data) { function handleListUsers(user, data) {
var name = data.name; var query = data.query;
if (typeof name !== "string") { if (typeof query !== "string") {
name = ""; query = "";
}
var field = data.field;
if (typeof field !== "string") {
field = "name";
} }
var fields = ["id", "name", "global_rank", "email", "ip", "time"]; var fields = ["id", "name", "global_rank", "email", "ip", "time"];
db.users.search(name, fields, function (err, users) { if(!fields.includes(field)){
user.socket.emit("errMessage", {
msg: `The field "${field}" doesn't exist or isn't searchable.`
});
return;
}
db.users.search(field, query, fields, function (err, users) {
if (err) { if (err) {
user.socket.emit("errMessage", { user.socket.emit("errMessage", {
msg: err msg: err

View File

@ -51,29 +51,16 @@ module.exports = {
}, },
/** /**
* Search for a user by name * Search for a user by any field
*/ */
search: function (name, fields, callback) { search: function (where, like, fields, callback) {
/* This bit allows it to accept varargs
Function can be called as (name, callback) or
(name, fields, callback)
*/
if (typeof callback !== "function") {
if (typeof fields === "function") {
callback = fields;
fields = ["name"];
} else {
return;
}
}
// Don't allow search to return password hashes // Don't allow search to return password hashes
if (fields.indexOf("password") !== -1) { if (fields.indexOf("password") !== -1) {
fields.splice(fields.indexOf("password")); fields.splice(fields.indexOf("password"));
} }
db.query("SELECT " + fields.join(",") + " FROM `users` WHERE name LIKE ?", db.query(`SELECT ${fields.join(",")} FROM \`users\` WHERE ${where} LIKE ?`,
["%"+name+"%"], ["%"+like+"%"],
function (err, rows) { function (err, rows) {
if (err) { if (err) {
callback(err, true); callback(err, true);

View File

@ -69,10 +69,12 @@ html(lang="en")
button#acp-gban-submit.btn.btn-danger Add ban button#acp-gban-submit.btn.btn-danger Add ban
#acp-user-lookup.acp-panel.col-md-12(style="display: none") #acp-user-lookup.acp-panel.col-md-12(style="display: none")
h3 Users h3 Users
.input-group(style="max-width: 25%") .input-group(style="max-width: 50%")
input#acp-ulookup-name.form-control(type="text") input#acp-ulookup-query.form-control(type="text")
span.input-group-btn span.input-group-btn
button#acp-ulookup-btn.btn.btn-default Search button#acp-ulookup-btn-name.btn.btn-default(data-field="name") Search Name
span.input-group-btn
button#acp-ulookup-btn-email.btn.btn-default(data-field="email") Search Email
table.table.table-bordered.table-striped(style="margin-top: 20px") table.table.table-bordered.table-striped(style="margin-top: 20px")
thead thead
tr tr

View File

@ -135,7 +135,7 @@ socket.on("acp-gbanlist", function (bans) {
/* User listing */ /* User listing */
(function () { (function () {
var doSearch = function () { var doSearch = function () {
if ($("#acp-ulookup-name").val().trim() === "") { if ($("#acp-ulookup-query").val().trim() === "") {
if (!confirm("You are about to list the entire users table. " + if (!confirm("You are about to list the entire users table. " +
"This table might be very large and take a long " + "This table might be very large and take a long " +
"time to query. Continue?")) { "time to query. Continue?")) {
@ -143,14 +143,16 @@ socket.on("acp-gbanlist", function (bans) {
} }
} }
socket.emit("acp-list-users", { socket.emit("acp-list-users", {
name: $("#acp-ulookup-name").val() query: $("#acp-ulookup-query").val(),
field: $(this).data()["field"]
}); });
}; };
$("#acp-ulookup-btn").click(doSearch); $("#acp-ulookup-btn-name").click(doSearch);
$("#acp-ulookup-name").keyup(function (ev) { $("#acp-ulookup-btn-email").click(doSearch);
$("#acp-ulookup-query").keyup(function (ev) {
if (ev.keyCode === 13) { if (ev.keyCode === 13) {
doSearch(); $("#acp-ulookup-btn-name").click();
} }
}); });
})(); })();