Node.js based forum software built for the modern web
feat: expose topic lock state to filter:privileges.posts.edit (#14710)
palmoni561126cc9360c
diff --git a/src/privileges/posts.js b/src/privileges/posts.jsindex d1d3034d83..50b8640efa 100644--- a/src/privileges/posts.js+++ b/src/privileges/posts.js@@ -175,19 +175,22 @@ privsPosts.canEdit = async function (pid, uid) {return { flag: false, message: `[[error:post-edit-duration-expired, ${meta.config.newbiePostEditDuration}]]` };}- const isLocked = await topics.isLocked(results.postData.tid);- if (!results.isMod && isLocked) {+ results.isLocked = await topics.isLocked(results.postData.tid);+ results.pid = utils.isNumber(pid) ? parseInt(pid, 10) : pid;+ results.uid = uid;++ // Fired ahead of the lock and deleted-post gates, so that plugins can grant+ // edit access inside a locked topic by unsetting `isLocked`.+ const result = await plugins.hooks.fire('filter:privileges.posts.edit', results);++ if (!result.isMod && result.isLocked) {return { flag: false, message: '[[error:topic-locked]]' };}- if (!results.isMod && results.postData.deleted && parseInt(uid, 10) !== parseInt(results.postData.deleterUid, 10)) {+ if (!result.isMod && result.postData.deleted && parseInt(uid, 10) !== parseInt(result.postData.deleterUid, 10)) {return { flag: false, message: '[[error:post-deleted]]' };}- results.pid = utils.isNumber(pid) ? parseInt(pid, 10) : pid;- results.uid = uid;-- const result = await plugins.hooks.fire('filter:privileges.posts.edit', results);return {flag: result.edit && (result.isOwner || result.isEditor || result.isGroupEditor || result.isMod),message: '[[error:no-privileges]]',diff --git a/test/posts.js b/test/posts.jsindex b947c34e49..2582d1b53d 100644--- a/test/posts.js+++ b/test/posts.js@@ -16,6 +16,7 @@ const categories = require('../src/categories');const privileges = require('../src/privileges');const user = require('../src/user');const groups = require('../src/groups');+const plugins = require('../src/plugins');const socketPosts = require('../src/socket.io/posts');const apiPosts = require('../src/api/posts');const apiTopics = require('../src/api/topics');@@ -1393,6 +1394,47 @@ describe('Post\'s', () => {});});+ describe('editing posts in locked topics', () => {+ let ownerUid;+ let pid;++ before(async () => {+ ownerUid = await user.create({ username: 'locked topic owner' });+ const topic = await topics.post({+ uid: ownerUid,+ cid,+ title: 'a topic that gets locked',+ content: 'Some text here for the OP',+ });+ pid = topic.postData.pid;+ await topics.setTopicField(topic.topicData.tid, 'locked', 1);+ });++ it('should not allow the owner to edit a post in a locked topic', async () => {+ const { flag, message } = await privileges.posts.canEdit(pid, ownerUid);+ assert.strictEqual(flag, false);+ assert.strictEqual(message, '[[error:topic-locked]]');+ });++ it('should let a plugin grant edit access by unsetting isLocked', async () => {+ const method = (data) => {+ assert.strictEqual(data.isLocked, true);+ data.isLocked = false;+ return data;+ };+ plugins.hooks.register('test-edit-locked', {+ hook: 'filter:privileges.posts.edit',+ method: method,+ });+ try {+ const { flag } = await privileges.posts.canEdit(pid, ownerUid);+ assert.strictEqual(flag, true);+ } finally {+ plugins.hooks.unregister('test-edit-locked', 'filter:privileges.posts.edit', method);+ }+ });+ });+describe('Topic Backlinks', () => {let tid1;before(async () => {