Skip to content

Node.js based forum software built for the modern web

refactor: flag assignment checks the same rule as flag access (#14889)

palmoni508f6f05425f8
diff --git a/src/flags.js b/src/flags.js
index be876cb767..158f9761f7 100644
--- a/src/flags.js
+++ b/src/flags.js
@@ -803,18 +803,6 @@ Flags.update = async function (flagId, uid, changeset) {
});
await notifications.push(notifObj, [assigneeId]);
};
- const isAssignable = async function (assigneeId) {
- let allowed = await user.isAdminOrGlobalMod(assigneeId);
-
- // Mods are also allowed to be assigned, if flag target is post in uid's moderated cid
- if (!allowed && current.type === 'post') {
- const cid = await posts.getCidByPid(current.targetId);
- allowed = await user.isModerator(assigneeId, cid);
- }
-
- return allowed;
- };
-
async function rescindNotifications(match) {
const nids = await db.getSortedSetScan({ key: 'notifications', match: `${match}*` });
return notifications.rescind(nids);
@@ -844,7 +832,7 @@ Flags.update = async function (flagId, uid, changeset) {
tasks.push(db.sortedSetRemove(`flags:byAssignee:${current[prop]}`, flagId));
}
/* eslint-disable-next-line */
- } else if (!await isAssignable(parseInt(changeset[prop], 10))) {
+ } else if (!await Flags.canView(flagId, parseInt(changeset[prop], 10))) {
delete changeset[prop];
} else {
if (current[prop]) {
diff --git a/test/flags.js b/test/flags.js
index 3e13663bca..5508f6f2be 100644
--- a/test/flags.js
+++ b/test/flags.js
@@ -594,6 +594,26 @@ describe('Flags', () => {
assert.ok(!changes.includes('[[global:guest]]'));
});
+ it('should not assign a chat message flag to a global moderator', async () => {
+ const globalModUid = await User.create({ username: 'message-flag-assignee' });
+ await Groups.join('Global Moderators', globalModUid);
+
+ const roomId = await messaging.newRoom(uid1, { uids: [uid3] });
+ const { mid } = await messaging.sendMessage({ uid: uid3, roomId, content: 'private chat content' });
+ const { flagId } = await Flags.create('message', mid, uid1, 'spam');
+
+ await Flags.update(flagId, adminUid, {
+ assignee: globalModUid,
+ });
+ assert.strictEqual(false, await Flags.canView(flagId, globalModUid));
+ assert.notStrictEqual(String(globalModUid), String(await db.getObjectField(`flag:${flagId}`, 'assignee')));
+
+ await Flags.update(flagId, adminUid, {
+ assignee: adminUid,
+ });
+ assert.strictEqual(String(adminUid), String(await db.getObjectField(`flag:${flagId}`, 'assignee')));
+ });
+
it('should do nothing when you attempt to set a bogus state', async () => {
await Flags.update(1, adminUid, {
state: 'hocus pocus',