Files
NodeBB/src/controllers/admin/users.js

190 lines
5.3 KiB
JavaScript
Raw Normal View History

"use strict";
2016-01-20 16:12:57 +02:00
var async = require('async');
2016-10-07 17:35:24 +03:00
var validator = require('validator');
2016-01-20 16:12:57 +02:00
var user = require('../../user');
var meta = require('../../meta');
var db = require('../../database');
var pagination = require('../../pagination');
var events = require('../../events');
var plugins = require('../../plugins');
var usersController = {};
2016-10-07 17:35:24 +03:00
var userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',
'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed'];
usersController.search = function (req, res, next) {
2014-09-28 21:09:40 -04:00
res.render('admin/manage/users', {
2014-09-25 15:13:54 -04:00
search_display: '',
users: []
});
};
usersController.sortByJoinDate = function (req, res, next) {
2016-10-07 17:35:24 +03:00
getUsers('users:joindate', 'latest', undefined, undefined, req, res, next);
};
usersController.notValidated = function (req, res, next) {
2016-10-07 17:35:24 +03:00
getUsers('users:notvalidated', 'notvalidated', undefined, undefined, req, res, next);
};
usersController.noPosts = function (req, res, next) {
2016-10-07 17:35:24 +03:00
getUsers('users:postcount', 'noposts', '-inf', 0, req, res, next);
2016-01-20 16:12:57 +02:00
};
usersController.flagged = function (req, res, next) {
2016-10-07 17:35:24 +03:00
getUsers('users:flags', 'mostflags', 1, '+inf', req, res, next);
2016-07-04 17:49:02 +03:00
};
usersController.inactive = function (req, res, next) {
2016-01-20 16:12:57 +02:00
var timeRange = 1000 * 60 * 60 * 24 * 30 * (parseInt(req.query.months, 10) || 3);
var cutoff = Date.now() - timeRange;
2016-10-07 17:35:24 +03:00
getUsers('users:online', 'inactive', '-inf', cutoff, req, res, next);
2014-03-15 16:28:57 -04:00
};
usersController.banned = function (req, res, next) {
2016-10-07 17:35:24 +03:00
getUsers('users:banned', 'banned', undefined, undefined, req, res, next);
2014-10-03 14:14:41 -04:00
};
usersController.registrationQueue = function (req, res, next) {
2016-03-29 11:26:28 +03:00
var page = parseInt(req.query.page, 10) || 1;
var itemsPerPage = 20;
var start = (page - 1) * 20;
var stop = start + itemsPerPage - 1;
var invitations;
async.parallel({
registrationQueueCount: function (next) {
2016-03-29 11:26:28 +03:00
db.sortedSetCard('registration:queue', next);
},
users: function (next) {
2016-03-29 11:26:28 +03:00
user.getRegistrationQueue(start, stop, next);
},
customHeaders: function (next) {
plugins.fireHook('filter:admin.registrationQueue.customHeaders', {headers: []}, next);
},
invites: function (next) {
async.waterfall([
function (next) {
user.getAllInvites(next);
},
function (_invitations, next) {
invitations = _invitations;
async.map(invitations, function (invites, next) {
user.getUserField(invites.uid, 'username', next);
}, next);
},
function (usernames, next) {
invitations.forEach(function (invites, index) {
invites.username = usernames[index];
});
async.map(invitations, function (invites, next) {
async.map(invites.invitations, user.getUsernameByEmail, next);
}, next);
},
function (usernames, next) {
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]
};
});
});
next(null, invitations);
}
], next);
}
}, function (err, data) {
2015-06-27 21:26:19 -04:00
if (err) {
return next(err);
}
2016-03-29 11:26:28 +03:00
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
};
2016-10-07 17:35:24 +03:00
function getUsers(set, section, min, max, req, res, next) {
2016-01-20 16:12:57 +02:00
var page = parseInt(req.query.page, 10) || 1;
2016-10-07 17:35:24 +03:00
var resultsPerPage = 50;
2016-01-20 16:12:57 +02:00
var start = Math.max(0, page - 1) * resultsPerPage;
var stop = start + resultsPerPage - 1;
2016-10-07 17:35:24 +03:00
var byScore = min !== undefined && max !== undefined;
2016-01-20 16:12:57 +02:00
async.parallel({
count: function (next) {
2016-10-07 17:35:24 +03:00
if (byScore) {
db.sortedSetCount(set, min, max, next);
} else {
db.sortedSetCard(set, next);
}
2016-01-20 16:12:57 +02:00
},
users: function (next) {
2016-10-07 17:35:24 +03:00
async.waterfall([
function (next) {
2016-10-07 17:35:24 +03:00
if (byScore) {
db.getSortedSetRevRangeByScore(set, start, resultsPerPage, max, min, next);
} else {
user.getUidsFromSet(set, start, stop, next);
}
},
function (uids, next) {
2016-10-07 17:35:24 +03:00
user.getUsersWithFields(uids, userFields, req.uid, next);
}
], next);
2016-01-20 16:12:57 +02:00
}
}, function (err, results) {
2014-03-15 16:28:57 -04:00
if (err) {
return next(err);
}
2014-12-29 15:11:52 -05:00
results.users = results.users.filter(function (user) {
2016-10-07 17:35:24 +03:00
user.email = validator.escape(String(user.email || ''));
2014-12-29 15:11:52 -05:00
return user && parseInt(user.uid, 10);
});
2016-01-20 16:12:57 +02:00
var data = {
users: results.users,
page: page,
2016-03-29 11:26:28 +03:00
pageCount: Math.max(1, Math.ceil(results.count / resultsPerPage))
2016-01-20 16:12:57 +02:00
};
data[section] = true;
render(req, res, data);
});
2014-03-15 16:28:57 -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);
data.requireEmailConfirmation = parseInt(meta.config.requireEmailConfirmation, 10) === 1;
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);
}
usersController.getCSV = function (req, res, next) {
events.log({
type: 'getUsersCSV',
uid: req.user.uid,
ip: req.ip
});
user.getUsersCSV(function (err, data) {
if (err) {
return next(err);
}
res.attachment('users.csv');
res.setHeader('Content-Type', 'text/csv');
res.end(data);
});
};
2014-04-10 20:31:57 +01:00
module.exports = usersController;