Skip to content

Node.js based forum software built for the modern web

WIP temporary bans

Julian Lam7da71976cfb5
diff --git a/public/language/en_GB/error.json b/public/language/en_GB/error.json
index 288ac0f4f2..113337098f 100644
--- a/public/language/en_GB/error.json
+++ b/public/language/en_GB/error.json
@@ -37,6 +37,7 @@
"user-banned": "User banned",
"user-too-new": "Sorry, you are required to wait %1 second(s) before making your first post",
"blacklisted-ip": "Sorry, your IP address has been banned from this community. If you feel this is in error, please contact an administrator.",
+ "ban-expiry-missing": "Please provide an end date for this ban",
"no-category": "Category does not exist",
"no-topic": "Topic does not exist",
diff --git a/src/socket.io/user/ban.js b/src/socket.io/user/ban.js
index 574490b74d..a8a67b17b0 100644
--- a/src/socket.io/user/ban.js
+++ b/src/socket.io/user/ban.js
@@ -7,7 +7,12 @@ var events = require('../../events');
module.exports = function(SocketUser) {
- SocketUser.banUsers = function(socket, uids, callback) {
+ SocketUser.banUsers = function(socket, uids, until, callback) {
+ if (!callback && typeof until === 'function') {
+ callback = until;
+ until = 0;
+ }
+
toggleBan(socket.uid, uids, SocketUser.banUser, function(err) {
if (err) {
return callback(err);
@@ -45,7 +50,7 @@ module.exports = function(SocketUser) {
], callback);
}
- SocketUser.banUser = function(uid, callback) {
+ SocketUser.banUser = function(uid, until, callback) {
async.waterfall([
function (next) {
user.isAdministrator(uid, next);
@@ -54,7 +59,7 @@ module.exports = function(SocketUser) {
if (isAdmin) {
return next(new Error('[[error:cant-ban-other-admins]]'));
}
- user.ban(uid, next);
+ user.ban(uid, until, next);
},
function (next) {
websockets.in('uid_' + uid).emit('event:banned');
diff --git a/src/user/admin.js b/src/user/admin.js
index 004e048c93..402c8c57a9 100644
--- a/src/user/admin.js
+++ b/src/user/admin.js
@@ -52,19 +52,35 @@ module.exports = function(User) {
], callback);
};
- User.ban = function(uid, callback) {
- async.waterfall([
- function (next) {
- User.setUserField(uid, 'banned', 1, next);
- },
- function (next) {
- db.sortedSetAdd('users:banned', Date.now(), uid, next);
- },
- function (next) {
- plugins.fireHook('action:user.banned', {uid: uid});
- next();
+ User.ban = function(uid, until, callback) {
+ // "until" (optional) is unix timestamp in milliseconds
+ if (!callback && typeof until === 'function') {
+ callback = until;
+ until = 0;
+ }
+
+ until = parseInt(until, 10);
+ if (isNaN(until)) {
+ return callback(new Error('[[error:ban-expiry-missing]]'));
+ }
+
+ var tasks = [
+ async.apply(User.setUserField, uid, 'banned', 1),
+ async.apply(db.sortedSetAdd, 'users:banned', Date.now(), uid),
+ ];
+
+ if (until > 0 && Date.now() < until) {
+ tasks.push(async.apply(db.sortedSetAdd, 'users:banned:expire', until, uid));
+ }
+
+ async.series(tasks, function (err) {
+ if (err) {
+ return callback(err);
}
- ], callback);
+
+ plugins.fireHook('action:user.banned', {uid: uid});
+ callback();
+ });
};
User.unban = function(uid, callback) {