mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-02-26 16:41:21 +01:00
feat: #8023, allow wildcard search for uid/email
This commit is contained in:
@@ -108,5 +108,5 @@
|
|||||||
|
|
||||||
"alerts.prompt-email": "Emails: ",
|
"alerts.prompt-email": "Emails: ",
|
||||||
"alerts.email-sent-to": "An invitation email has been sent to %1",
|
"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:
|
properties:
|
||||||
search_display:
|
search_display:
|
||||||
type: string
|
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:
|
users:
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
$ref: components/schemas/UserObject.yaml#/UserObjectACP
|
$ref: components/schemas/UserObject.yaml#/UserObjectACP
|
||||||
- $ref: components/schemas/CommonProps.yaml#/CommonProps
|
- $ref: components/schemas/CommonProps.yaml#/CommonProps
|
||||||
|
- $ref: components/schemas/Pagination.yaml#/Pagination
|
||||||
/api/admin/manage/users/latest:
|
/api/admin/manage/users/latest:
|
||||||
get:
|
get:
|
||||||
tags:
|
tags:
|
||||||
|
|||||||
@@ -376,33 +376,10 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete'], funct
|
|||||||
|
|
||||||
timeoutId = setTimeout(function () {
|
timeoutId = setTimeout(function () {
|
||||||
$('.fa-spinner').removeClass('hidden');
|
$('.fa-spinner').removeClass('hidden');
|
||||||
|
loadSearchPage({
|
||||||
socket.emit('admin.user.search', { searchBy: type, query: $this.val() }, function (err, data) {
|
searchBy: type,
|
||||||
if (err) {
|
query: $this.val(),
|
||||||
return app.alertError(err.message);
|
page: 1,
|
||||||
}
|
|
||||||
|
|
||||||
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');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}, 250);
|
}, 250);
|
||||||
});
|
});
|
||||||
@@ -412,6 +389,38 @@ define('admin/manage/users', ['translator', 'benchpress', 'autocomplete'], funct
|
|||||||
handleInvite();
|
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() {
|
function handleInvite() {
|
||||||
$('[component="user/invite"]').on('click', function () {
|
$('[component="user/invite"]').on('click', function () {
|
||||||
bootbox.prompt('[[admin/manage/users:alerts.prompt-email]]', function (email) {
|
bootbox.prompt('[[admin/manage/users:alerts.prompt-email]]', function (email) {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const nconf = require('nconf');
|
const nconf = require('nconf');
|
||||||
|
const validator = require('validator');
|
||||||
|
|
||||||
const user = require('../../user');
|
const user = require('../../user');
|
||||||
const meta = require('../../meta');
|
const meta = require('../../meta');
|
||||||
@@ -15,11 +16,56 @@ const usersController = module.exports;
|
|||||||
const userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',
|
const userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',
|
||||||
'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed'];
|
'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed'];
|
||||||
|
|
||||||
usersController.search = function (req, res) {
|
usersController.search = async function (req, res) {
|
||||||
res.render('admin/manage/users', {
|
const page = parseInt(req.query.page, 10) || 1;
|
||||||
search_display: '',
|
let resultsPerPage = parseInt(req.query.resultsPerPage, 10) || 50;
|
||||||
users: [],
|
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) {
|
usersController.sortByJoinDate = async function (req, res) {
|
||||||
|
|||||||
@@ -178,6 +178,7 @@ async function deleteUsers(socket, uids, method) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
User.search = async function (socket, data) {
|
User.search = async function (socket, data) {
|
||||||
|
// TODO: deprecate
|
||||||
const searchData = await user.search({
|
const searchData = await user.search({
|
||||||
query: data.query,
|
query: data.query,
|
||||||
searchBy: data.searchBy,
|
searchBy: data.searchBy,
|
||||||
|
|||||||
@@ -37,9 +37,9 @@ module.exports = function (User) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (paginate) {
|
if (paginate) {
|
||||||
var resultsPerPage = meta.config.userSearchResultsPerPage;
|
const resultsPerPage = data.resultsPerPage || meta.config.userSearchResultsPerPage;
|
||||||
var start = Math.max(0, page - 1) * resultsPerPage;
|
const start = Math.max(0, page - 1) * resultsPerPage;
|
||||||
var stop = start + resultsPerPage;
|
const stop = start + resultsPerPage;
|
||||||
searchResult.pageCount = Math.ceil(uids.length / resultsPerPage);
|
searchResult.pageCount = Math.ceil(uids.length / resultsPerPage);
|
||||||
uids = uids.slice(start, stop);
|
uids = uids.slice(start, stop);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,23 +57,26 @@
|
|||||||
<form class="form-inline">
|
<form class="form-inline">
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>[[admin/manage/users:search.uid]]</label>
|
<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>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>[[admin/manage/users:search.username]]</label>
|
<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>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>[[admin/manage/users:search.email]]</label>
|
<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>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label>[[admin/manage/users:search.ip]]</label>
|
<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>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
<i class="fa fa-spinner fa-spin hidden"></i>
|
<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>
|
</div>
|
||||||
|
|
||||||
<!-- IF inactive -->
|
<!-- IF inactive -->
|
||||||
|
|||||||
Reference in New Issue
Block a user