Skip to content

Node.js based forum software built for the modern web

check for last owner on user kick from group

pichalitec1c5db4b771c
diff --git a/public/language/en_GB/error.json b/public/language/en_GB/error.json
index 2921a55a35..b60c8a2cde 100644
--- a/public/language/en_GB/error.json
+++ b/public/language/en_GB/error.json
@@ -125,5 +125,6 @@
"no-session-found": "No login session found!",
"not-in-room": "User not in room",
- "no-users-in-room": "No users in this room"
+ "no-users-in-room": "No users in this room",
+ "cant-kick-self": "You can't kick yourself from the group"
}
diff --git a/src/groups/membership.js b/src/groups/membership.js
index cb37121a40..1b13022300 100644
--- a/src/groups/membership.js
+++ b/src/groups/membership.js
@@ -413,4 +413,24 @@ module.exports = function(Groups) {
}
db.getSetMembers('group:' + groupName + ':pending', callback);
};
+
+ Groups.kick = function(uid, groupName, isOwner, callback) {
+ if (isOwner) {
+ // If the owners set only contains one member, error out!
+ async.waterfall([
+ function (next) {
+ db.setCount('group:' + groupName + ':owners', next);
+ },
+ function (numOwners, next) {
+ if (numOwners <= 1) {
+ return next(new Error('[[error:group-needs-owner]]'));
+ }
+ Groups.leave(groupName, uid, callback);
+ next();
+ }
+ ], callback);
+ } else {
+ Groups.leave(groupName, uid, callback);
+ }
+ };
};
diff --git a/src/socket.io/groups.js b/src/socket.io/groups.js
index dc8ec8c51a..0135852f00 100644
--- a/src/socket.io/groups.js
+++ b/src/socket.io/groups.js
@@ -155,9 +155,15 @@ SocketGroups.kick = isOwner(function(socket, data, callback) {
if (socket.uid === parseInt(data.uid, 10)) {
return callback(new Error('[[error:cant-kick-self]]'));
}
- groups.leave(data.groupName, data.uid, callback);
-});
+
+ groups.ownership.isOwner(data.uid, data.groupName, function(err, isOwner) {
+ if (err) {
+ callback(err);
+ }
+ groups.kick(data.uid, data.groupName, isOwner, callback);
+ });
+});
SocketGroups.create = function(socket, data, callback) {
if (!socket.uid) {