temporary ban support

v1.18.x
Julian Lam 9 years ago
parent 585a90db5d
commit 04d4fc2eca

@ -334,10 +334,13 @@ authenticationController.localLogin = function(req, username, password, next) {
function (next) {
async.parallel({
userData: function(next) {
db.getObjectFields('user:' + uid, ['password', 'banned', 'passwordExpiry'], next);
db.getObjectFields('user:' + uid, ['password', 'passwordExpiry'], next);
},
isAdmin: function(next) {
user.isAdministrator(uid, next);
},
banned: function(next) {
user.isBanned(uid, next);
}
}, next);
},
@ -349,13 +352,13 @@ authenticationController.localLogin = function(req, username, password, next) {
if (!result.isAdmin && parseInt(meta.config.allowLocalLogin, 10) === 0) {
return next(new Error('[[error:local-login-disabled]]'));
}
if (!userData || !userData.password) {
return next(new Error('[[error:invalid-user-data]]'));
}
if (userData.banned && parseInt(userData.banned, 10) === 1) {
if (result.banned) {
return next(new Error('[[error:user-banned]]'));
}
Password.compare(password, userData.password, next);
},
function (passwordMatch, next) {

@ -7,17 +7,20 @@ var events = require('../../events');
module.exports = function(SocketUser) {
SocketUser.banUsers = function(socket, uids, until, callback) {
if (!callback && typeof until === 'function') {
callback = until;
until = 0;
SocketUser.banUsers = function(socket, data, callback) {
// Backwards compatibility
if (Array.isArray(data)) {
data = {
uids: data,
until: 0
}
}
toggleBan(socket.uid, uids, SocketUser.banUser, function(err) {
toggleBan(socket.uid, data.uids, banUser.bind(null, data.until || 0), function(err) {
if (err) {
return callback(err);
}
async.each(uids, function(uid, next) {
async.each(data.uids, function(uid, next) {
events.log({
type: 'user-ban',
uid: socket.uid,
@ -50,7 +53,7 @@ module.exports = function(SocketUser) {
], callback);
}
SocketUser.banUser = function(uid, until, callback) {
function banUser(until, uid, callback) {
async.waterfall([
function (next) {
user.isAdministrator(uid, next);

@ -256,6 +256,35 @@ var utils = require('../public/src/utils');
});
};
User.isBanned = function(uid, callback) {
async.waterfall([
async.apply(User.getUserField, uid, 'banned'),
function(banned, next) {
banned = parseInt(banned, 10) === 1;
if (!banned) {
return next(null, banned);
} else {
// If they are banned, see if the ban has expired
db.sortedSetScore('users:banned:expire', uid, function(err, score) {
var stillBanned = Date.now() < score;
if (!stillBanned) {
async.parallel([
async.apply(db.sortedSetRemove.bind(db), 'users:banned:expire', uid),
async.apply(db.sortedSetRemove.bind(db), 'users:banned', uid),
async.apply(User.setUserField, uid, 'banned', 0)
], function(err) {
next(err, false);
});
} else {
next(err, true);
}
});
}
}
], callback);
};
User.addInterstitials = function(callback) {
plugins.registerHook('core', {
hook: 'filter:register.interstitial',

@ -71,6 +71,8 @@ module.exports = function(User) {
if (until > 0 && Date.now() < until) {
tasks.push(async.apply(db.sortedSetAdd, 'users:banned:expire', until, uid));
} else {
until = 0;
}
async.series(tasks, function (err) {
@ -78,7 +80,10 @@ module.exports = function(User) {
return callback(err);
}
plugins.fireHook('action:user.banned', {uid: uid});
plugins.fireHook('action:user.banned', {
uid: uid,
until: until > 0 ? until : undefined
});
callback();
});
};

Loading…
Cancel
Save