feat: #8023, allow wildcard search for uid/email

v1.18.x
Barış Soner Uşaklı 5 years ago
parent 4e9743abb3
commit 3dcf538773

@ -108,5 +108,5 @@
"alerts.prompt-email": "Emails: ",
"alerts.email-sent-to": "An invitation email has been sent to %1",
"alerts.x-users-found": "%1 user(s) found! Search took %2 ms."
"alerts.x-users-found": "%1 user(s) found, (%2 seconds)"
}

@ -954,11 +954,30 @@ paths:
properties:
search_display:
type: string
matchCount:
type: number
query:
type: string
uidQuery:
type: string
usernameQuery:
type: string
emailQuery:
type: string
ipQuery:
type: string
pageCount:
type: number
resultsPerPage:
type: number
timing:
type: number
users:
type: array
items:
$ref: components/schemas/UserObject.yaml#/UserObjectACP
- $ref: components/schemas/CommonProps.yaml#/CommonProps
- $ref: components/schemas/Pagination.yaml#/Pagination
/api/admin/manage/users/latest:
get:
tags:

@ -376,33 +376,10 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete'], funct
timeoutId = setTimeout(function () {
$('.fa-spinner').removeClass('hidden');
socket.emit('admin.user.search', { searchBy: type, query: $this.val() }, function (err, data) {
if (err) {
return app.alertError(err.message);
}
Benchpress.parse('admin/manage/users', 'users', data, function (html) {
translator.translate(html, function (html) {
html = $(html);
$('.users-table tbody tr').remove();
$('.users-table tbody').append(html);
html.find('.timeago').timeago();
$('.fa-spinner').addClass('hidden');
if (data && data.users.length === 0) {
$('#user-notfound-notify').translateHtml('[[admin/manage/users:search.not-found]]')
.removeClass('hide')
.addClass('label-danger')
.removeClass('label-success');
} else {
$('#user-notfound-notify').translateHtml(translator.compile('admin/manage/users:alerts.x-users-found', data.users.length, data.timing))
.removeClass('hide')
.addClass('label-success')
.removeClass('label-danger');
}
});
});
loadSearchPage({
searchBy: type,
query: $this.val(),
page: 1,
});
}, 250);
});
@ -412,6 +389,38 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete'], funct
handleInvite();
};
function loadSearchPage(query) {
var qs = decodeURIComponent($.param(query));
$.get(config.relative_path + '/api/admin/manage/users/search?' + qs, renderSearchResults).fail(function (xhrErr) {
if (xhrErr && xhrErr.responseJSON && xhrErr.responseJSON.error) {
app.alertError(xhrErr.responseJSON.error);
}
});
}
function renderSearchResults(data) {
Benchpress.parse('partials/paginator', { pagination: data.pagination }, function (html) {
$('.pagination-container').replaceWith(html);
});
app.parseAndTranslate('admin/manage/users', 'users', data, function (html) {
$('.users-table tbody tr').remove();
$('.users-table tbody').append(html);
html.find('.timeago').timeago();
$('.fa-spinner').addClass('hidden');
if (data && data.users.length === 0) {
$('#user-notfound-notify').translateHtml('[[admin/manage/users:search.not-found]]')
.removeClass('hidden');
$('#user-found-notify').addClass('hidden');
} else {
$('#user-found-notify').translateHtml(translator.compile('admin/manage/users:alerts.x-users-found', data.matchCount, data.timing))
.removeClass('hidden');
$('#user-notfound-notify').addClass('hidden');
}
});
}
function handleInvite() {
$('[component="user/invite"]').on('click', function () {
bootbox.prompt('[[admin/manage/users:alerts.prompt-email]]', function (email) {

@ -1,6 +1,7 @@
'use strict';
const nconf = require('nconf');
const validator = require('validator');
const user = require('../../user');
const meta = require('../../meta');
@ -15,11 +16,56 @@ const usersController = module.exports;
const userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',
'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed'];
usersController.search = function (req, res) {
res.render('admin/manage/users', {
search_display: '',
users: [],
usersController.search = async function (req, res) {
const page = parseInt(req.query.page, 10) || 1;
let resultsPerPage = parseInt(req.query.resultsPerPage, 10) || 50;
if (![50, 100, 250, 500].includes(resultsPerPage)) {
resultsPerPage = 50;
}
const searchData = await user.search({
uid: req.uid,
query: req.query.query,
searchBy: req.query.searchBy,
page: page,
resultsPerPage: resultsPerPage,
findUids: async function (query, searchBy, hardCap) {
if (!query || query.length < 2) {
return [];
}
hardCap = hardCap || resultsPerPage * 10;
if (!query.endsWith('*')) {
query += '*';
}
const data = await db.getSortedSetScan({
key: searchBy + ':sorted',
match: query,
limit: hardCap,
});
return data.map(data => data.split(':')[1]);
},
});
const uids = searchData.users.map(user => user && user.uid);
const userInfo = await user.getUsersFields(uids, ['email', 'flags', 'lastonline', 'joindate']);
searchData.users.forEach(function (user, index) {
if (user && userInfo[index]) {
user.email = userInfo[index].email;
user.flags = userInfo[index].flags || 0;
user.lastonlineISO = userInfo[index].lastonlineISO;
user.joindateISO = userInfo[index].joindateISO;
}
});
searchData.query = validator.escape(String(req.query.query || ''));
searchData.uidQuery = req.query.searchBy === 'uid' ? searchData.query : '';
searchData.usernameQuery = req.query.searchBy === 'username' ? searchData.query : '';
searchData.emailQuery = req.query.searchBy === 'email' ? searchData.query : '';
searchData.ipQuery = req.query.searchBy === 'uid' ? searchData.query : '';
searchData.resultsPerPage = resultsPerPage;
searchData.pagination = pagination.create(page, searchData.pageCount, req.query);
searchData.search_display = '';
res.render('admin/manage/users', searchData);
};
usersController.sortByJoinDate = async function (req, res) {

@ -178,6 +178,7 @@ async function deleteUsers(socket, uids, method) {
}
User.search = async function (socket, data) {
// TODO: deprecate
const searchData = await user.search({
query: data.query,
searchBy: data.searchBy,

@ -37,9 +37,9 @@ module.exports = function (User) {
};
if (paginate) {
var resultsPerPage = meta.config.userSearchResultsPerPage;
var start = Math.max(0, page - 1) * resultsPerPage;
var stop = start + resultsPerPage;
const resultsPerPage = data.resultsPerPage || meta.config.userSearchResultsPerPage;
const start = Math.max(0, page - 1) * resultsPerPage;
const stop = start + resultsPerPage;
searchResult.pageCount = Math.ceil(uids.length / resultsPerPage);
uids = uids.slice(start, stop);
}

@ -57,23 +57,26 @@
<form class="form-inline">
<div class="form-group">
<label>[[admin/manage/users:search.uid]]</label>
<input class="form-control" id="search-user-uid" data-search-type="uid" type="number" placeholder="[[admin/manage/users:search.uid-placeholder]]"/><br />
<input class="form-control" id="search-user-uid" data-search-type="uid" type="number" placeholder="[[admin/manage/users:search.uid-placeholder]]" value="{uidQuery}"/>
</div>
<div class="form-group">
<label>[[admin/manage/users:search.username]]</label>
<input class="form-control" id="search-user-name" data-search-type="username" type="text" placeholder="[[admin/manage/users:search.username-placeholder]]"/><br />
<input class="form-control" id="search-user-name" data-search-type="username" type="text" placeholder="[[admin/manage/users:search.username-placeholder]]" value="{usernameQuery}"/>
</div>
<div class="form-group">
<label>[[admin/manage/users:search.email]]</label>
<input class="form-control" id="search-user-email" data-search-type="email" type="text" placeholder="[[admin/manage/users:search.email-placeholder]]"/><br />
<input class="form-control" id="search-user-email" data-search-type="email" type="text" placeholder="[[admin/manage/users:search.email-placeholder]]" value="{emailQuery}"/>
</div>
<div class="form-group">
<label>[[admin/manage/users:search.ip]]</label>
<input class="form-control" id="search-user-ip" data-search-type="ip" type="text" placeholder="[[admin/manage/users:search.ip-placeholder]]"/><br />
<input class="form-control" id="search-user-ip" data-search-type="ip" type="text" placeholder="[[admin/manage/users:search.ip-placeholder]]" value="{ipQuery}"/>
</div>
</form>
<i class="fa fa-spinner fa-spin hidden"></i>
<span id="user-notfound-notify" class="label label-danger hide">[[admin/manage/users:search.not-found]]</span><br/>
<div id="user-found-notify" class="label label-info {{{if !matchCount}}}hidden{{{end}}}">[[admin/manage/users:alerts.x-users-found, {matchCount}, {timing}]]</div>
<div id="user-notfound-notify" class="label label-danger {{{if !query}}}hidden{{{end}}} {{{if matchCount}}}hidden{{{end}}}">[[admin/manage/users:search.not-found]]</div>
</div>
<!-- IF inactive -->

Loading…
Cancel
Save