mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 10:35:55 +01:00
feat: #7743, privileges
This commit is contained in:
@@ -1,300 +1,213 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var _ = require('lodash');
|
||||
const _ = require('lodash');
|
||||
|
||||
var meta = require('../meta');
|
||||
var posts = require('../posts');
|
||||
var topics = require('../topics');
|
||||
var user = require('../user');
|
||||
var helpers = require('./helpers');
|
||||
var plugins = require('../plugins');
|
||||
const meta = require('../meta');
|
||||
const posts = require('../posts');
|
||||
const topics = require('../topics');
|
||||
const user = require('../user');
|
||||
const helpers = require('./helpers');
|
||||
const plugins = require('../plugins');
|
||||
const utils = require('../utils');
|
||||
|
||||
module.exports = function (privileges) {
|
||||
privileges.posts = {};
|
||||
|
||||
privileges.posts.get = function (pids, uid, callback) {
|
||||
privileges.posts.get = async function (pids, uid) {
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
return setImmediate(callback, null, []);
|
||||
return [];
|
||||
}
|
||||
let uniqueCids;
|
||||
let cids;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getCidsByPids(pids, next);
|
||||
},
|
||||
function (_cids, next) {
|
||||
cids = _cids;
|
||||
uniqueCids = _.uniq(cids);
|
||||
async.parallel({
|
||||
isAdmin: async.apply(user.isAdministrator, uid),
|
||||
isModerator: async.apply(user.isModerator, uid, uniqueCids),
|
||||
isOwner: async.apply(posts.isOwner, pids, uid),
|
||||
'topics:read': async.apply(helpers.isUserAllowedTo, 'topics:read', uid, uniqueCids),
|
||||
read: async.apply(helpers.isUserAllowedTo, 'read', uid, uniqueCids),
|
||||
'posts:edit': async.apply(helpers.isUserAllowedTo, 'posts:edit', uid, uniqueCids),
|
||||
'posts:history': async.apply(helpers.isUserAllowedTo, 'posts:history', uid, uniqueCids),
|
||||
'posts:view_deleted': async.apply(helpers.isUserAllowedTo, 'posts:view_deleted', uid, uniqueCids),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
const isModerator = _.zipObject(uniqueCids, results.isModerator);
|
||||
const privData = {};
|
||||
privData['topics:read'] = _.zipObject(uniqueCids, results['topics:read']);
|
||||
privData.read = _.zipObject(uniqueCids, results.read);
|
||||
privData['posts:edit'] = _.zipObject(uniqueCids, results['posts:edit']);
|
||||
privData['posts:history'] = _.zipObject(uniqueCids, results['posts:history']);
|
||||
privData['posts:view_deleted'] = _.zipObject(uniqueCids, results['posts:view_deleted']);
|
||||
const cids = await posts.getCidsByPids(pids);
|
||||
const uniqueCids = _.uniq(cids);
|
||||
|
||||
var privileges = cids.map(function (cid, i) {
|
||||
var isAdminOrMod = results.isAdmin || isModerator[cid];
|
||||
var editable = (privData['posts:edit'][cid] && (results.isOwner[i] || results.isModerator)) || results.isAdmin;
|
||||
var viewDeletedPosts = results.isOwner[i] || privData['posts:view_deleted'][cid] || results.isAdmin;
|
||||
var viewHistory = results.isOwner[i] || privData['posts:history'][cid] || results.isAdmin;
|
||||
const results = await utils.promiseParallel({
|
||||
isAdmin: user.isAdministrator(uid),
|
||||
isModerator: user.isModerator(uid, uniqueCids),
|
||||
isOwner: posts.isOwner(pids, uid),
|
||||
'topics:read': helpers.isUserAllowedTo('topics:read', uid, uniqueCids),
|
||||
read: helpers.isUserAllowedTo('read', uid, uniqueCids),
|
||||
'posts:edit': helpers.isUserAllowedTo('posts:edit', uid, uniqueCids),
|
||||
'posts:history': helpers.isUserAllowedTo('posts:history', uid, uniqueCids),
|
||||
'posts:view_deleted': helpers.isUserAllowedTo('posts:view_deleted', uid, uniqueCids),
|
||||
});
|
||||
|
||||
return {
|
||||
editable: editable,
|
||||
move: isAdminOrMod,
|
||||
isAdminOrMod: isAdminOrMod,
|
||||
'topics:read': privData['topics:read'][cid] || results.isAdmin,
|
||||
read: privData.read[cid] || results.isAdmin,
|
||||
'posts:history': viewHistory,
|
||||
'posts:view_deleted': viewDeletedPosts,
|
||||
};
|
||||
});
|
||||
const isModerator = _.zipObject(uniqueCids, results.isModerator);
|
||||
const privData = {};
|
||||
privData['topics:read'] = _.zipObject(uniqueCids, results['topics:read']);
|
||||
privData.read = _.zipObject(uniqueCids, results.read);
|
||||
privData['posts:edit'] = _.zipObject(uniqueCids, results['posts:edit']);
|
||||
privData['posts:history'] = _.zipObject(uniqueCids, results['posts:history']);
|
||||
privData['posts:view_deleted'] = _.zipObject(uniqueCids, results['posts:view_deleted']);
|
||||
|
||||
next(null, privileges);
|
||||
},
|
||||
], callback);
|
||||
const privileges = cids.map(function (cid, i) {
|
||||
const isAdminOrMod = results.isAdmin || isModerator[cid];
|
||||
const editable = (privData['posts:edit'][cid] && (results.isOwner[i] || results.isModerator)) || results.isAdmin;
|
||||
const viewDeletedPosts = results.isOwner[i] || privData['posts:view_deleted'][cid] || results.isAdmin;
|
||||
const viewHistory = results.isOwner[i] || privData['posts:history'][cid] || results.isAdmin;
|
||||
|
||||
return {
|
||||
editable: editable,
|
||||
move: isAdminOrMod,
|
||||
isAdminOrMod: isAdminOrMod,
|
||||
'topics:read': privData['topics:read'][cid] || results.isAdmin,
|
||||
read: privData.read[cid] || results.isAdmin,
|
||||
'posts:history': viewHistory,
|
||||
'posts:view_deleted': viewDeletedPosts,
|
||||
};
|
||||
});
|
||||
|
||||
return privileges;
|
||||
};
|
||||
|
||||
privileges.posts.can = function (privilege, pid, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getCidByPid(pid, next);
|
||||
},
|
||||
function (cid, next) {
|
||||
privileges.categories.can(privilege, cid, uid, next);
|
||||
},
|
||||
], callback);
|
||||
privileges.posts.can = async function (privilege, pid, uid) {
|
||||
const cid = await posts.getCidByPid(pid);
|
||||
return await privileges.categories.can(privilege, cid, uid);
|
||||
};
|
||||
|
||||
privileges.posts.filter = function (privilege, pids, uid, callback) {
|
||||
privileges.posts.filter = async function (privilege, pids, uid) {
|
||||
if (!Array.isArray(pids) || !pids.length) {
|
||||
return setImmediate(callback, null, []);
|
||||
return [];
|
||||
}
|
||||
var cids;
|
||||
var postData;
|
||||
var tids;
|
||||
var tidToTopic = {};
|
||||
|
||||
pids = _.uniq(pids);
|
||||
const postData = await posts.getPostsFields(pids, ['uid', 'tid', 'deleted']);
|
||||
const tids = _.uniq(postData.map(post => post && post.tid).filter(Boolean));
|
||||
const topicData = await topics.getTopicsFields(tids, ['deleted', 'cid']);
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getPostsFields(pids, ['uid', 'tid', 'deleted'], next);
|
||||
},
|
||||
function (_posts, next) {
|
||||
postData = _posts;
|
||||
const tidToTopic = _.zipObject(tids, topicData);
|
||||
|
||||
tids = _.uniq(_posts.map(post => post && post.tid).filter(Boolean));
|
||||
let cids = postData.map(function (post, index) {
|
||||
if (post) {
|
||||
post.pid = pids[index];
|
||||
post.topic = tidToTopic[post.tid];
|
||||
}
|
||||
return tidToTopic[post.tid] && tidToTopic[post.tid].cid;
|
||||
}).filter(cid => parseInt(cid, 10));
|
||||
|
||||
topics.getTopicsFields(tids, ['deleted', 'cid'], next);
|
||||
},
|
||||
function (topicData, next) {
|
||||
topicData.forEach(function (topic, index) {
|
||||
if (topic) {
|
||||
tidToTopic[tids[index]] = topic;
|
||||
}
|
||||
});
|
||||
cids = _.uniq(cids);
|
||||
|
||||
cids = postData.map(function (post, index) {
|
||||
if (post) {
|
||||
post.pid = pids[index];
|
||||
post.topic = tidToTopic[post.tid];
|
||||
}
|
||||
return tidToTopic[post.tid] && tidToTopic[post.tid].cid;
|
||||
}).filter(cid => parseInt(cid, 10));
|
||||
const results = await privileges.categories.getBase(privilege, cids, uid);
|
||||
cids = cids.filter(function (cid, index) {
|
||||
return !results.categories[index].disabled &&
|
||||
(results.allowedTo[index] || results.isAdmin);
|
||||
});
|
||||
|
||||
cids = _.uniq(cids);
|
||||
const cidsSet = new Set(cids);
|
||||
|
||||
privileges.categories.getBase(privilege, cids, uid, next);
|
||||
},
|
||||
function (results, next) {
|
||||
cids = cids.filter(function (cid, index) {
|
||||
return !results.categories[index].disabled &&
|
||||
(results.allowedTo[index] || results.isAdmin);
|
||||
});
|
||||
pids = postData.filter(function (post) {
|
||||
return post.topic && cidsSet.has(post.topic.cid) &&
|
||||
((!post.topic.deleted && !post.deleted) || results.isAdmin);
|
||||
}).map(post => post.pid);
|
||||
|
||||
const cidsSet = new Set(cids);
|
||||
const data = await plugins.fireHook('filter:privileges.posts.filter', {
|
||||
privilege: privilege,
|
||||
uid: uid,
|
||||
pids: pids,
|
||||
});
|
||||
|
||||
pids = postData.filter(function (post) {
|
||||
return post.topic && cidsSet.has(post.topic.cid) &&
|
||||
((!post.topic.deleted && !post.deleted) || results.isAdmin);
|
||||
}).map(post => post.pid);
|
||||
|
||||
plugins.fireHook('filter:privileges.posts.filter', {
|
||||
privilege: privilege,
|
||||
uid: uid,
|
||||
pids: pids,
|
||||
}, function (err, data) {
|
||||
next(err, data ? data.pids : null);
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
return data ? data.pids : null;
|
||||
};
|
||||
|
||||
privileges.posts.canEdit = function (pid, uid, callback) {
|
||||
let results;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
isAdmin: async.apply(privileges.users.isAdministrator, uid),
|
||||
isMod: async.apply(posts.isModerator, [pid], uid),
|
||||
owner: async.apply(posts.isOwner, pid, uid),
|
||||
edit: async.apply(privileges.posts.can, 'posts:edit', pid, uid),
|
||||
postData: async.apply(posts.getPostFields, pid, ['tid', 'timestamp', 'deleted', 'deleterUid']),
|
||||
userData: async.apply(user.getUserFields, uid, ['reputation']),
|
||||
}, next);
|
||||
},
|
||||
function (_results, next) {
|
||||
results = _results;
|
||||
results.isMod = results.isMod[0];
|
||||
if (results.isAdmin) {
|
||||
return callback(null, { flag: true });
|
||||
}
|
||||
privileges.posts.canEdit = async function (pid, uid) {
|
||||
const results = await utils.promiseParallel({
|
||||
isAdmin: privileges.users.isAdministrator(uid),
|
||||
isMod: posts.isModerator([pid], uid),
|
||||
owner: posts.isOwner(pid, uid),
|
||||
edit: privileges.posts.can('posts:edit', pid, uid),
|
||||
postData: posts.getPostFields(pid, ['tid', 'timestamp', 'deleted', 'deleterUid']),
|
||||
userData: user.getUserFields(uid, ['reputation']),
|
||||
});
|
||||
|
||||
if (!results.isMod && meta.config.postEditDuration && (Date.now() - results.postData.timestamp > meta.config.postEditDuration * 1000)) {
|
||||
return callback(null, { flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.postEditDuration + ']]' });
|
||||
}
|
||||
if (!results.isMod && meta.config.newbiePostEditDuration > 0 && meta.config.newbiePostDelayThreshold > _results.userData.reputation && Date.now() - _results.postData.timestamp > meta.config.newbiePostEditDuration * 1000) {
|
||||
return callback(null, { flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.newbiePostEditDuration + ']]' });
|
||||
}
|
||||
results.isMod = results.isMod[0];
|
||||
if (results.isAdmin) {
|
||||
return { flag: true };
|
||||
}
|
||||
|
||||
topics.isLocked(results.postData.tid, next);
|
||||
},
|
||||
function (isLocked, next) {
|
||||
if (!results.isMod && isLocked) {
|
||||
return callback(null, { flag: false, message: '[[error:topic-locked]]' });
|
||||
}
|
||||
if (!results.isMod && meta.config.postEditDuration && (Date.now() - results.postData.timestamp > meta.config.postEditDuration * 1000)) {
|
||||
return { flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.postEditDuration + ']]' };
|
||||
}
|
||||
if (!results.isMod && meta.config.newbiePostEditDuration > 0 && meta.config.newbiePostDelayThreshold > results.userData.reputation && Date.now() - results.postData.timestamp > meta.config.newbiePostEditDuration * 1000) {
|
||||
return { flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.newbiePostEditDuration + ']]' };
|
||||
}
|
||||
|
||||
if (!results.isMod && results.postData.deleted && parseInt(uid, 10) !== parseInt(results.postData.deleterUid, 10)) {
|
||||
return callback(null, { flag: false, message: '[[error:post-deleted]]' });
|
||||
}
|
||||
const isLocked = await topics.isLocked(results.postData.tid);
|
||||
if (!results.isMod && isLocked) {
|
||||
return { flag: false, message: '[[error:topic-locked]]' };
|
||||
}
|
||||
|
||||
results.pid = parseInt(pid, 10);
|
||||
results.uid = uid;
|
||||
if (!results.isMod && results.postData.deleted && parseInt(uid, 10) !== parseInt(results.postData.deleterUid, 10)) {
|
||||
return { flag: false, message: '[[error:post-deleted]]' };
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:privileges.posts.edit', results, next);
|
||||
},
|
||||
function (result, next) {
|
||||
next(null, { flag: result.edit && (result.owner || result.isMod), message: '[[error:no-privileges]]' });
|
||||
},
|
||||
], callback);
|
||||
results.pid = parseInt(pid, 10);
|
||||
results.uid = uid;
|
||||
|
||||
const result = await plugins.fireHook('filter:privileges.posts.edit', results);
|
||||
return { flag: result.edit && (result.owner || result.isMod), message: '[[error:no-privileges]]' };
|
||||
};
|
||||
|
||||
privileges.posts.canDelete = function (pid, uid, callback) {
|
||||
var postData;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getPostFields(pid, ['uid', 'tid', 'timestamp', 'deleterUid'], next);
|
||||
},
|
||||
function (_postData, next) {
|
||||
postData = _postData;
|
||||
async.parallel({
|
||||
isAdmin: async.apply(privileges.users.isAdministrator, uid),
|
||||
isMod: async.apply(posts.isModerator, [pid], uid),
|
||||
isLocked: async.apply(topics.isLocked, postData.tid),
|
||||
isOwner: async.apply(posts.isOwner, pid, uid),
|
||||
'posts:delete': async.apply(privileges.posts.can, 'posts:delete', pid, uid),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
results.isMod = results.isMod[0];
|
||||
if (results.isAdmin) {
|
||||
return next(null, { flag: true });
|
||||
}
|
||||
privileges.posts.canDelete = async function (pid, uid) {
|
||||
const postData = await posts.getPostFields(pid, ['uid', 'tid', 'timestamp', 'deleterUid']);
|
||||
const results = await utils.promiseParallel({
|
||||
isAdmin: privileges.users.isAdministrator(uid),
|
||||
isMod: posts.isModerator([pid], uid),
|
||||
isLocked: topics.isLocked(postData.tid),
|
||||
isOwner: posts.isOwner(pid, uid),
|
||||
'posts:delete': privileges.posts.can('posts:delete', pid, uid),
|
||||
});
|
||||
results.isMod = results.isMod[0];
|
||||
if (results.isAdmin) {
|
||||
return { flag: true };
|
||||
}
|
||||
|
||||
if (!results.isMod && results.isLocked) {
|
||||
return next(null, { flag: false, message: '[[error:topic-locked]]' });
|
||||
}
|
||||
if (!results.isMod && results.isLocked) {
|
||||
return { flag: false, message: '[[error:topic-locked]]' };
|
||||
}
|
||||
|
||||
var postDeleteDuration = meta.config.postDeleteDuration;
|
||||
if (!results.isMod && postDeleteDuration && (Date.now() - postData.timestamp > postDeleteDuration * 1000)) {
|
||||
return next(null, { flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]' });
|
||||
}
|
||||
var deleterUid = postData.deleterUid;
|
||||
var flag = results['posts:delete'] && ((results.isOwner && (deleterUid === 0 || deleterUid === postData.uid)) || results.isMod);
|
||||
next(null, { flag: flag, message: '[[error:no-privileges]]' });
|
||||
},
|
||||
], callback);
|
||||
var postDeleteDuration = meta.config.postDeleteDuration;
|
||||
if (!results.isMod && postDeleteDuration && (Date.now() - postData.timestamp > postDeleteDuration * 1000)) {
|
||||
return { flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]' };
|
||||
}
|
||||
var deleterUid = postData.deleterUid;
|
||||
var flag = results['posts:delete'] && ((results.isOwner && (deleterUid === 0 || deleterUid === postData.uid)) || results.isMod);
|
||||
return { flag: flag, message: '[[error:no-privileges]]' };
|
||||
};
|
||||
|
||||
privileges.posts.canFlag = function (pid, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
userReputation: async.apply(user.getUserField, uid, 'reputation'),
|
||||
isAdminOrMod: async.apply(isAdminOrMod, pid, uid),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
var minimumReputation = meta.config['min:rep:flag'];
|
||||
var canFlag = results.isAdminOrMod || (results.userReputation >= minimumReputation);
|
||||
next(null, { flag: canFlag });
|
||||
},
|
||||
], callback);
|
||||
privileges.posts.canFlag = async function (pid, uid) {
|
||||
const [userReputation, isAdminOrModerator] = await Promise.all([
|
||||
user.getUserField(uid, 'reputation'),
|
||||
isAdminOrMod(pid, uid),
|
||||
]);
|
||||
const minimumReputation = meta.config['min:rep:flag'];
|
||||
const canFlag = isAdminOrModerator || (userReputation >= minimumReputation);
|
||||
return { flag: canFlag };
|
||||
};
|
||||
|
||||
privileges.posts.canMove = function (pid, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.isMain(pid, next);
|
||||
},
|
||||
function (isMain, next) {
|
||||
if (isMain) {
|
||||
return next(new Error('[[error:cant-move-mainpost]]'));
|
||||
}
|
||||
isAdminOrMod(pid, uid, next);
|
||||
},
|
||||
], callback);
|
||||
privileges.posts.canMove = async function (pid, uid) {
|
||||
const isMain = await posts.isMain(pid);
|
||||
if (isMain) {
|
||||
throw new Error('[[error:cant-move-mainpost]]');
|
||||
}
|
||||
return await isAdminOrMod(pid, uid);
|
||||
};
|
||||
|
||||
privileges.posts.canPurge = function (pid, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getCidByPid(pid, next);
|
||||
},
|
||||
function (cid, next) {
|
||||
async.parallel({
|
||||
purge: async.apply(privileges.categories.isUserAllowedTo, 'purge', cid, uid),
|
||||
owner: async.apply(posts.isOwner, pid, uid),
|
||||
isAdmin: async.apply(privileges.users.isAdministrator, uid),
|
||||
isModerator: async.apply(privileges.users.isModerator, uid, cid),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
next(null, (results.purge && (results.owner || results.isModerator)) || results.isAdmin);
|
||||
},
|
||||
], callback);
|
||||
privileges.posts.canPurge = async function (pid, uid) {
|
||||
const cid = await posts.getCidByPid(pid);
|
||||
const results = await utils.promiseParallel({
|
||||
purge: privileges.categories.isUserAllowedTo('purge', cid, uid),
|
||||
owner: posts.isOwner(pid, uid),
|
||||
isAdmin: privileges.users.isAdministrator(uid),
|
||||
isModerator: privileges.users.isModerator(uid, cid),
|
||||
});
|
||||
return (results.purge && (results.owner || results.isModerator)) || results.isAdmin;
|
||||
};
|
||||
|
||||
function isAdminOrMod(pid, uid, callback) {
|
||||
helpers.some([
|
||||
function (next) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
posts.getCidByPid(pid, next);
|
||||
},
|
||||
function (cid, next) {
|
||||
user.isModerator(uid, cid, next);
|
||||
},
|
||||
], next);
|
||||
},
|
||||
function (next) {
|
||||
user.isAdministrator(uid, next);
|
||||
},
|
||||
], callback);
|
||||
async function isAdminOrMod(pid, uid) {
|
||||
if (parseInt(uid, 10) <= 0) {
|
||||
return false;
|
||||
}
|
||||
const cid = await posts.getCidByPid(pid);
|
||||
return await privileges.categories.isAdminOrMod(cid, uid);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user