Node.js based forum software built for the modern web
fix: closes #14553, closes #14554
Barış Soner Uşaklı37c1853645ec
diff --git a/src/api/topics.js b/src/api/topics.jsindex 0a9d925449..5eb4c22219 100644--- a/src/api/topics.js+++ b/src/api/topics.js@@ -33,20 +33,11 @@ topicsAPI._checkThumbPrivileges = async function ({ tid, uid }) {};topicsAPI.get = async function (caller, data) {- const [userPrivileges, topic] = await Promise.all([- privileges.topics.get(data.tid, caller.uid),- topics.getTopicData(data.tid),- ]);- if (- !topic ||- !userPrivileges['topics:read'] ||- userPrivileges.disabled ||- !privileges.topics.canViewDeletedScheduled(topic, userPrivileges)- ) {+ const canReadTopic = await privileges.topics.canRead(data.tid, caller.uid);+ if (!canReadTopic) {return null;}-- return topic;+ return await topics.getTopicData(data.tid);};topicsAPI.create = async function (caller, data) {@@ -253,7 +244,8 @@ topicsAPI.reorderThumbs = async (caller, { tid, path, order }) => {};topicsAPI.getEvents = async (caller, { tid }) => {- if (!await privileges.topics.can('topics:read', tid, caller.uid)) {+ const canRead = await privileges.topics.canRead(tid, caller.uid);+ if (!canRead) {throw new Error('[[error:no-privileges]]');}@@ -261,7 +253,8 @@ topicsAPI.getEvents = async (caller, { tid }) => {};topicsAPI.deleteEvent = async (caller, { tid, eventId }) => {- if (!await privileges.topics.isAdminOrMod(tid, caller.uid)) {+ const canRead = await privileges.topics.canRead(tid, caller.uid);+ if (!canRead || !await privileges.topics.isAdminOrMod(tid, caller.uid)) {throw new Error('[[error:no-privileges]]');}diff --git a/src/controllers/topics.js b/src/controllers/topics.jsindex 47f3bd0a67..cc53ef0c72 100644--- a/src/controllers/topics.js+++ b/src/controllers/topics.js@@ -430,15 +430,11 @@ topicsController.pagination = async function (req, res, next) {if (!topic) {return next();}- const [userPrivileges, settings] = await Promise.all([- privileges.topics.get(tid, req.uid),- user.getSettings(req.uid),- ]);-- if (!userPrivileges.read || !privileges.topics.canViewDeletedScheduled(topic, userPrivileges)) {+ if (!await privileges.topics.canRead(tid, req.uid)) {return helpers.notAllowed(req, res);}+ const settings = await user.getSettings(req.uid);const postCount = topic.postcount;const pageCount = Math.max(1, Math.ceil(postCount / settings.postsPerPage));diff --git a/src/privileges/topics.js b/src/privileges/topics.jsindex 640734e89e..78f25fd3b9 100644--- a/src/privileges/topics.js+++ b/src/privileges/topics.js@@ -143,6 +143,20 @@ privsTopics.filterUids = async function (privilege, tid, uids) {((allowedTo[index] && (topicData.scheduled || !topicData.deleted)) || isAdmins[index]));};+privsTopics.canRead = async function (tid, uid) {+ tid = String(tid);+ const [userPrivileges, topic] = await Promise.all([+ privsTopics.get(tid, uid),+ topics.getTopicData(tid),+ ]);+ return (+ topic &&+ userPrivileges['topics:read'] &&+ !userPrivileges.disabled &&+ privsTopics.canViewDeletedScheduled(topic, userPrivileges)+ );+};+privsTopics.canPurge = async function (tid, uid) {const cid = await topics.getTopicField(tid, 'cid');let [purge, owner, isAdmin, isModerator] = await Promise.all([diff --git a/src/socket.io/topics/infinitescroll.js b/src/socket.io/topics/infinitescroll.jsindex cb0814d329..d7ca7aa104 100644--- a/src/socket.io/topics/infinitescroll.js+++ b/src/socket.io/topics/infinitescroll.js@@ -12,15 +12,15 @@ module.exports = function (SocketTopics) {throw new Error('[[error:invalid-data]]');}+ if (!await privileges.topics.canRead(data.tid, socket.uid)) {+ throw new Error('[[error:no-privileges]]');+ }+const [userPrivileges, topicData] = await Promise.all([privileges.topics.get(data.tid, socket.uid),topics.getTopicData(data.tid),]);- if (!userPrivileges['topics:read'] || !privileges.topics.canViewDeletedScheduled(topicData, userPrivileges)) {- throw new Error('[[error:no-privileges]]');- }-const set = data.topicPostSort === 'most_votes' ? `tid:${data.tid}:posts:votes` : `tid:${data.tid}:posts`;const reverse = data.topicPostSort === 'newest_to_oldest' || data.topicPostSort === 'most_votes';let start = Math.max(0, parseInt(data.after, 10));diff --git a/src/socket.io/topics/tools.js b/src/socket.io/topics/tools.jsindex e4044a5272..0b024b7d39 100644--- a/src/socket.io/topics/tools.js+++ b/src/socket.io/topics/tools.js@@ -18,7 +18,7 @@ module.exports = function (SocketTopics) {if (!topicData) {throw new Error('[[error:no-topic]]');}- if (!userPrivileges['topics:read'] || !userPrivileges.view_thread_tools) {+ if (!userPrivileges.view_thread_tools || !await privileges.topics.canRead(data.tid, socket.uid)) {throw new Error('[[error:no-privileges]]');}topicData.privileges = userPrivileges;