mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-24 05:10:38 +01:00
feat: #8023, allow wildcard search for uid/email
This commit is contained in:
@@ -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 -->
|
||||
|
||||
Reference in New Issue
Block a user