mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: refactor groups.delete
This commit is contained in:
@@ -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]]');
|
||||
@@ -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({
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user