fix: #9394, fix guest handles

v1.18.x
Barış Soner Uşaklı 4 years ago
parent d1685600d3
commit eb360351e5

@ -68,6 +68,9 @@ topicsAPI.reply = async function (caller, data) {
};
if (data.toPid) { payload.toPid = data.toPid; }
if (data.handle && !parseInt(caller.uid, 10)) {
payload.handle = data.handle;
}
// Blacklist & Post Queue
await meta.blacklist.test(caller.ip);

@ -20,7 +20,7 @@ module.exports = function (Posts) {
options.parse = options.hasOwnProperty('parse') ? options.parse : true;
options.extraFields = options.hasOwnProperty('extraFields') ? options.extraFields : [];
const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies'].concat(options.extraFields);
const fields = ['pid', 'tid', 'content', 'uid', 'timestamp', 'deleted', 'upvotes', 'downvotes', 'replies', 'handle'].concat(options.extraFields);
let posts = await Posts.getPostsFields(pids, fields);
posts = posts.filter(Boolean);
@ -45,6 +45,8 @@ module.exports = function (Posts) {
post.uid = 0;
}
post.user = uidToUser[post.uid];
Posts.overrideGuestHandle(post, post.handle);
post.handle = undefined;
post.topic = tidToTopic[post.tid];
post.category = post.topic && cidToCategory[post.topic.cid];
post.isMainPost = post.topic && post.pid === post.topic.mainPid;
@ -60,7 +62,7 @@ module.exports = function (Posts) {
};
async function parsePosts(posts, options) {
async function parse(post) {
return await Promise.all(posts.map(async (post) => {
if (!post.content || !options.parse) {
post.content = post.content ? validator.escape(String(post.content)) : post.content;
return post;
@ -70,8 +72,7 @@ module.exports = function (Posts) {
post.content = stripTags(post.content);
}
return post;
}
return await Promise.all(posts.map(p => parse(p)));
}));
}
async function getTopicAndCategories(tids) {

@ -53,6 +53,16 @@ module.exports = function (Posts) {
}));
};
Posts.overrideGuestHandle = function (postData, handle) {
if (meta.config.allowGuestHandles && postData && postData.user && parseInt(postData.uid, 10) === 0 && handle) {
postData.user.username = validator.escape(String(handle));
if (postData.user.hasOwnProperty('fullname')) {
postData.user.fullname = postData.user.username;
}
postData.user.displayname = postData.user.username;
}
};
async function checkGroupMembership(uid, groupTitleArray) {
if (!Array.isArray(groupTitleArray) || !groupTitleArray.length) {
return null;

@ -217,10 +217,7 @@ module.exports = function (Topics) {
postData.topic = topicInfo;
postData.index = topicInfo.postcount - 1;
// Username override for guests, if enabled
if (meta.config.allowGuestHandles && postData.uid === 0 && data.handle) {
postData.user.username = validator.escape(String(data.handle));
}
posts.overrideGuestHandle(postData, data.handle);
postData.votes = 0;
postData.bookmarked = false;

@ -167,6 +167,44 @@ describe('Topic\'s', () => {
json: true,
});
assert.strictEqual(replyResult.body.response.content, 'a reply by guest');
assert.strictEqual(replyResult.body.response.user.username, '[[global:guest]]');
});
it('should post a topic/reply as guest with handle if guest group has privileges', async () => {
const categoryObj = await categories.create({
name: 'Test Category',
description: 'Test category created by testing script',
});
await privileges.categories.give(['groups:topics:create'], categoryObj.cid, 'guests');
await privileges.categories.give(['groups:topics:reply'], categoryObj.cid, 'guests');
const oldValue = meta.config.allowGuestHandles;
meta.config.allowGuestHandles = 1;
const result = await requestType('post', `${nconf.get('url')}/api/v3/topics`, {
form: {
title: 'just a title',
cid: categoryObj.cid,
content: 'content for the main post',
handle: 'guest123',
},
json: true,
});
assert.strictEqual(result.body.status.code, 'ok');
assert.strictEqual(result.body.response.title, 'just a title');
assert.strictEqual(result.body.response.user.username, 'guest123');
assert.strictEqual(result.body.response.user.displayname, 'guest123');
const replyResult = await requestType('post', `${nconf.get('url')}/api/v3/topics/${result.body.response.tid}`, {
form: {
content: 'a reply by guest',
handle: 'guest124',
},
json: true,
});
assert.strictEqual(replyResult.body.response.content, 'a reply by guest');
assert.strictEqual(replyResult.body.response.user.username, 'guest124');
assert.strictEqual(replyResult.body.response.user.displayname, 'guest124');
meta.config.allowGuestHandles = oldValue;
});
});

Loading…
Cancel
Save