2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2014-03-12 17:59:29 -04:00
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
const nconf = require('nconf');
|
2016-10-07 17:35:24 +03:00
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
const user = require('../../user');
|
|
|
|
|
const meta = require('../../meta');
|
|
|
|
|
const db = require('../../database');
|
|
|
|
|
const pagination = require('../../pagination');
|
|
|
|
|
const events = require('../../events');
|
|
|
|
|
const plugins = require('../../plugins');
|
|
|
|
|
const utils = require('../../utils');
|
2014-03-12 17:59:29 -04:00
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
const usersController = module.exports;
|
2014-03-12 17:59:29 -04:00
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
const userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',
|
2016-10-07 17:35:24 +03:00
|
|
|
'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed'];
|
|
|
|
|
|
2017-02-18 15:05:36 -07:00
|
|
|
usersController.search = function (req, res) {
|
2014-09-28 21:09:40 -04:00
|
|
|
res.render('admin/manage/users', {
|
2014-09-25 15:13:54 -04:00
|
|
|
search_display: '',
|
2017-02-17 19:31:21 -07:00
|
|
|
users: [],
|
2014-03-12 17:59:29 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.sortByJoinDate = async function (req, res) {
|
|
|
|
|
await getUsers('users:joindate', 'latest', undefined, undefined, req, res);
|
2014-03-12 17:59:29 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.notValidated = async function (req, res) {
|
|
|
|
|
await getUsers('users:notvalidated', 'notvalidated', undefined, undefined, req, res);
|
2014-03-12 17:59:29 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.noPosts = async function (req, res) {
|
|
|
|
|
await getUsers('users:postcount', 'noposts', '-inf', 0, req, res);
|
2016-01-20 16:12:57 +02:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.topPosters = async function (req, res) {
|
|
|
|
|
await getUsers('users:postcount', 'topposts', 0, '+inf', req, res);
|
2016-10-20 00:08:41 +03:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.mostReputaion = async function (req, res) {
|
|
|
|
|
await getUsers('users:reputation', 'mostreputation', 0, '+inf', req, res);
|
2016-10-20 00:08:41 +03:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.flagged = async function (req, res) {
|
|
|
|
|
await getUsers('users:flags', 'mostflags', 1, '+inf', req, res);
|
2016-07-04 17:49:02 +03:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.inactive = async function (req, res) {
|
|
|
|
|
const timeRange = 1000 * 60 * 60 * 24 * 30 * (parseInt(req.query.months, 10) || 3);
|
|
|
|
|
const cutoff = Date.now() - timeRange;
|
|
|
|
|
await getUsers('users:online', 'inactive', '-inf', cutoff, req, res);
|
2014-03-15 16:28:57 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.banned = async function (req, res) {
|
|
|
|
|
await getUsers('users:banned', 'banned', undefined, undefined, req, res);
|
2014-10-03 14:14:41 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.registrationQueue = async function (req, res) {
|
|
|
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
const itemsPerPage = 20;
|
|
|
|
|
const start = (page - 1) * 20;
|
|
|
|
|
const stop = start + itemsPerPage - 1;
|
|
|
|
|
|
|
|
|
|
const data = await utils.promiseParallel({
|
|
|
|
|
registrationQueueCount: db.sortedSetCard('registration:queue'),
|
|
|
|
|
users: user.getRegistrationQueue(start, stop),
|
|
|
|
|
customHeaders: plugins.fireHook('filter:admin.registrationQueue.customHeaders', { headers: [] }),
|
|
|
|
|
invites: getInvites(),
|
|
|
|
|
});
|
|
|
|
|
var pageCount = Math.max(1, Math.ceil(data.registrationQueueCount / itemsPerPage));
|
|
|
|
|
data.pagination = pagination.create(page, pageCount);
|
|
|
|
|
data.customHeaders = data.customHeaders.headers;
|
|
|
|
|
res.render('admin/manage/registration', data);
|
2015-06-27 21:26:19 -04:00
|
|
|
};
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
async function getInvites() {
|
|
|
|
|
const invitations = await user.getAllInvites();
|
|
|
|
|
const uids = invitations.map(invite => invite.uid);
|
|
|
|
|
let usernames = await user.getUsersFields(uids, ['username']);
|
|
|
|
|
usernames = usernames.map(user => user.username);
|
|
|
|
|
|
|
|
|
|
invitations.forEach(function (invites, index) {
|
|
|
|
|
invites.username = usernames[index];
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function getUsernamesByEmails(emails) {
|
|
|
|
|
const uids = await db.sortedSetScore('email:uid', emails.map(email => String(email).toLowerCase()));
|
|
|
|
|
const usernames = await user.getUsersFields(uids, ['username']);
|
|
|
|
|
return usernames.map(user => user.username);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usernames = await Promise.all(invitations.map(invites => getUsernamesByEmails(invites.invitations)));
|
|
|
|
|
|
|
|
|
|
invitations.forEach(function (invites, index) {
|
|
|
|
|
invites.invitations = invites.invitations.map(function (email, i) {
|
|
|
|
|
return {
|
|
|
|
|
email: email,
|
|
|
|
|
username: usernames[index][i] === '[[global:guest]]' ? '' : usernames[index][i],
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return invitations;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getUsers(set, section, min, max, req, res) {
|
|
|
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
|
|
|
let resultsPerPage = parseInt(req.query.resultsPerPage, 10) || 50;
|
2019-04-10 14:00:33 -04:00
|
|
|
if (![50, 100, 250, 500].includes(resultsPerPage)) {
|
|
|
|
|
resultsPerPage = 50;
|
|
|
|
|
}
|
2019-08-14 22:48:57 -04:00
|
|
|
const start = Math.max(0, page - 1) * resultsPerPage;
|
|
|
|
|
const stop = start + resultsPerPage - 1;
|
|
|
|
|
const byScore = min !== undefined && max !== undefined;
|
|
|
|
|
|
|
|
|
|
async function getCount() {
|
|
|
|
|
if (byScore) {
|
|
|
|
|
return await db.sortedSetCount(set, min, max);
|
|
|
|
|
} else if (set === 'users:banned' || set === 'users:notvalidated') {
|
|
|
|
|
return await db.sortedSetCard(set);
|
|
|
|
|
}
|
|
|
|
|
return await db.getObjectField('global', 'userCount');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getUsersWithFields() {
|
|
|
|
|
let uids;
|
|
|
|
|
if (byScore) {
|
|
|
|
|
uids = await db.getSortedSetRevRangeByScore(set, start, resultsPerPage, max, min);
|
|
|
|
|
} else {
|
|
|
|
|
uids = await user.getUidsFromSet(set, start, stop);
|
|
|
|
|
}
|
|
|
|
|
return await user.getUsersWithFields(uids, userFields, req.uid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const [count, users] = await Promise.all([
|
|
|
|
|
getCount(),
|
|
|
|
|
getUsersWithFields(),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
users: users.filter(user => user && parseInt(user.uid, 10)),
|
|
|
|
|
page: page,
|
|
|
|
|
pageCount: Math.max(1, Math.ceil(count / resultsPerPage)),
|
|
|
|
|
resultsPerPage: resultsPerPage,
|
|
|
|
|
};
|
|
|
|
|
data[section] = true;
|
|
|
|
|
render(req, res, data);
|
2014-03-15 16:28:57 -04:00
|
|
|
}
|
2014-03-12 17:59:29 -04:00
|
|
|
|
2016-01-20 16:12:57 +02:00
|
|
|
function render(req, res, data) {
|
|
|
|
|
data.search_display = 'hidden';
|
|
|
|
|
data.pagination = pagination.create(data.page, data.pageCount, req.query);
|
2018-10-21 16:47:51 -04:00
|
|
|
data.requireEmailConfirmation = meta.config.requireEmailConfirmation;
|
2016-04-27 11:01:27 +03:00
|
|
|
|
|
|
|
|
var registrationType = meta.config.registrationType;
|
|
|
|
|
|
|
|
|
|
data.inviteOnly = registrationType === 'invite-only' || registrationType === 'admin-invite-only';
|
|
|
|
|
data.adminInviteOnly = registrationType === 'admin-invite-only';
|
|
|
|
|
|
2016-01-20 16:12:57 +02:00
|
|
|
res.render('admin/manage/users', data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 22:48:57 -04:00
|
|
|
usersController.getCSV = async function (req, res) {
|
2017-12-01 18:16:59 -05:00
|
|
|
var referer = req.headers.referer;
|
|
|
|
|
|
|
|
|
|
if (!referer || !referer.replace(nconf.get('url'), '').startsWith('/admin/manage/users')) {
|
|
|
|
|
return res.status(403).send('[[error:invalid-origin]]');
|
|
|
|
|
}
|
2016-08-19 12:40:52 -04:00
|
|
|
events.log({
|
|
|
|
|
type: 'getUsersCSV',
|
2018-01-31 15:20:17 -05:00
|
|
|
uid: req.uid,
|
2017-02-17 19:31:21 -07:00
|
|
|
ip: req.ip,
|
2016-08-19 12:40:52 -04:00
|
|
|
});
|
2019-08-14 22:48:57 -04:00
|
|
|
const data = await user.getUsersCSV();
|
|
|
|
|
res.attachment('users.csv');
|
|
|
|
|
res.setHeader('Content-Type', 'text/csv');
|
|
|
|
|
res.end(data);
|
2014-03-12 17:59:29 -04:00
|
|
|
};
|