From afa84023a2881891d0f6f0e3f0ef52338594c902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 11 Nov 2018 18:42:15 -0500 Subject: [PATCH] closes #6942 --- src/groups/delete.js | 21 ++++++--------------- test/groups.js | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/groups/delete.js b/src/groups/delete.js index ed55a1f383..f605a044c3 100644 --- a/src/groups/delete.js +++ b/src/groups/delete.js @@ -12,7 +12,6 @@ module.exports = function (Groups) { groupNames = [groupNames]; } - var groupObj; var groupsData; async.waterfall([ function (next) { @@ -23,8 +22,6 @@ module.exports = function (Groups) { if (!groupsData.length) { return callback(); } - // backwards compatibility - groupObj = groupsData[0]; async.parallel([ function (next) { @@ -49,19 +46,15 @@ module.exports = function (Groups) { ], groupNames, next); }, function (next) { - var keys = groupNames.map(function (groupName) { - return groupName.toLowerCase() + ':' + groupName; - }); + const keys = groupNames.map(groupName => groupName.toLowerCase() + ':' + groupName); db.sortedSetRemove('groups:visible:name', keys, next); }, function (next) { - var fields = groupNames.map(function (groupName) { - return utils.slugify(groupName); - }); + const fields = groupNames.map(groupName => utils.slugify(groupName)); db.deleteObjectFields('groupslug:groupname', fields, next); }, function (next) { - removeGroupsFromOtherGroups(groupNames, next); + removeGroupsFromPrivilegeGroups(groupNames, next); }, ], function (err) { next(err); @@ -69,18 +62,16 @@ module.exports = function (Groups) { }, function (next) { Groups.resetCache(); - plugins.fireHook('action:group.destroy', { group: groupObj }); plugins.fireHook('action:groups.destroy', { groups: groupsData }); next(); }, ], callback); }; - function removeGroupsFromOtherGroups(groupNames, callback) { + function removeGroupsFromPrivilegeGroups(groupNames, callback) { batch.processSortedSet('groups:createtime', function (otherGroups, next) { - var keys = otherGroups.map(function (group) { - return 'group:' + group + ':members'; - }); + const privilegeGroups = otherGroups.filter(group => Groups.isPrivilegeGroup(group)); + const keys = privilegeGroups.map(group => 'group:' + group + ':members'); db.sortedSetRemove(keys, groupNames, next); }, { batch: 500, diff --git a/test/groups.js b/test/groups.js index bbd92e8f66..078f871d7f 100644 --- a/test/groups.js +++ b/test/groups.js @@ -456,7 +456,6 @@ describe('Groups', function () { Groups.get('foobar?', {}, function (err, groupObj) { assert.ifError(err); assert.strictEqual(groupObj, null); - done(); }); }); @@ -465,12 +464,43 @@ describe('Groups', function () { it('should also remove the members set', function (done) { db.exists('group:foo:members', function (err, exists) { assert.ifError(err); - assert.strictEqual(false, exists); - done(); }); }); + + it('should remove group from privilege groups', function (done) { + const privileges = require('../src/privileges'); + const cid = 1; + const groupName = '1'; + const uid = 1; + async.waterfall([ + function (next) { + Groups.create({ name: groupName }, next); + }, + function (groupData, next) { + privileges.categories.give(['topics:create'], cid, groupName, next); + }, + function (next) { + Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create', next); + }, + function (isMember, next) { + assert(isMember); + Groups.destroy(groupName, next); + }, + function (next) { + Groups.isMember(groupName, 'cid:1:privileges:groups:topics:create', next); + }, + function (isMember, next) { + assert(!isMember); + Groups.isMember(uid, 'registered-users', next); + }, + function (isMember, next) { + assert(isMember); + next(); + }, + ], done); + }); }); describe('.join()', function () {