Skip to content

Commit

Permalink
feat: refactor groups.delete
Browse files Browse the repository at this point in the history
  • Loading branch information
barisusakli committed Oct 15, 2020
1 parent d69e503 commit 8ae1f81
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 32 deletions.
36 changes: 33 additions & 3 deletions src/api/groups.js
Expand Up @@ -29,6 +29,22 @@ groupsAPI.create = async function (caller, data) {
return groupData;
};

groupsAPI.delete = async function (caller, data) {
const groupName = await groups.getGroupNameByGroupSlug(data.slug);
await isOwner(caller, groupName);
if (
groups.systemGroups.includes(groupName) ||
groups.ephemeralGroups.includes(groupName)
) {
throw new Error('[[error:not-allowed]]');
}

await groups.destroy(groupName);
logGroupEvent(caller, 'group-delete', {
groupName: groupName,
});
};

groupsAPI.join = async function (caller, data) {
if (caller.uid <= 0 || !data.uid) {
throw new Error('[[error:invalid-uid]]');
Expand Down Expand Up @@ -88,9 +104,23 @@ groupsAPI.join = async function (caller, data) {
// // TODO:
// };

// groupsAPI.delete = async function (caller, data) {
// // TODO:
// };

async function isOwner(caller, groupName) {
if (typeof groupName !== 'string') {
throw new Error('[[error:invalid-group-name]]');
}
const [isAdmin, isGlobalModerator, isOwner, group] = await Promise.all([
user.isAdministrator(caller.uid),
user.isGlobalModerator(caller.uid),
groups.ownership.isOwner(caller.uid, groupName),
groups.getGroupData(groupName),
]);

const check = isOwner || isAdmin || (isGlobalModerator && !group.system);
if (!check) {
throw new Error('[[error:no-privileges]]');
}
}

function logGroupEvent(caller, event, additional) {
events.log({
Expand Down
17 changes: 1 addition & 16 deletions src/controllers/write/groups.js
Expand Up @@ -19,23 +19,8 @@ Groups.create = async (req, res) => {
};

Groups.delete = async (req, res) => {
const group = await groups.getByGroupslug(req.params.slug, {
uid: req.user.uid,
});

if (groups.ephemeralGroups.includes(group.slug)) {
throw new Error('[[error:not-allowed]]');
}

if (group.system || (!group.isOwner && !res.locals.privileges.isAdmin && !res.locals.privileges.isGmod)) {
throw new Error('[[error:no-privileges]]');
}

await groups.destroy(group.name);
await api.groups.delete(req, req.params);
helpers.formatApiResponse(200, res);
logGroupEvent(req, 'group-delete', {
groupName: group.name,
});
};

Groups.join = async (req, res) => {
Expand Down
15 changes: 3 additions & 12 deletions src/socket.io/groups.js
Expand Up @@ -244,18 +244,9 @@ SocketGroups.create = async (socket, data) => {
};

SocketGroups.delete = async (socket, data) => {
await isOwner(socket, data);
if (
data.groupName === 'administrators' || data.groupName === 'registered-users' ||
data.groupName === 'guests' || data.groupName === 'Global Moderators'
) {
throw new Error('[[error:not-allowed]]');
}

await groups.destroy(data.groupName);
logGroupEvent(socket, 'group-delete', {
groupName: data.groupName,
});
sockets.warnDeprecated(socket, 'DEL /api/v3/groups');
const slug = await groups.getGroupField(data.groupName, 'slug');
await api.groups.delete(socket, { slug: slug });
};

SocketGroups.search = async (socket, data) => {
Expand Down
2 changes: 1 addition & 1 deletion test/groups.js
Expand Up @@ -1237,7 +1237,7 @@ describe('Groups', function () {

it('should fail to delete group if name is special', function (done) {
socketGroups.delete({ uid: adminUid }, { groupName: 'guests' }, function (err) {
assert.equal(err.message, '[[error:not-allowed]]');
assert.equal(err.message, '[[error:invalid-group-name]]');
done();
});
});
Expand Down

0 comments on commit 8ae1f81

Please sign in to comment.