You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

271 lines
7.5 KiB
JavaScript

'use strict';
var async = require('async');
var meta = require('../meta');
var posts = require('../posts');
var topics = require('../topics');
var user = require('../user');
var helpers = require('./helpers');
var plugins = require('../plugins');
module.exports = function (privileges) {
privileges.posts = {};
privileges.posts.get = function (pids, uid, callback) {
11 years ago
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
9 years ago
async.waterfall([
function (next) {
9 years ago
posts.getCidsByPids(pids, next);
10 years ago
},
function (cids, next) {
9 years ago
async.parallel({
isAdmin: async.apply(user.isAdministrator, uid),
isModerator: async.apply(posts.isModerator, pids, uid),
isOwner: async.apply(posts.isOwner, pids, uid),
'topics:read': async.apply(helpers.isUserAllowedTo, 'topics:read', uid, cids),
read: async.apply(helpers.isUserAllowedTo, 'read', uid, cids),
'posts:edit': async.apply(helpers.isUserAllowedTo, 'posts:edit', uid, cids),
9 years ago
}, next);
10 years ago
}
], function (err, results) {
10 years ago
if (err) {
return callback(err);
}
10 years ago
var privileges = [];
for (var i = 0; i < pids.length; ++i) {
9 years ago
var isAdminOrMod = results.isAdmin || results.isModerator[i];
var editable = isAdminOrMod || (results.isOwner[i] && results['posts:edit'][i]);
9 years ago
10 years ago
privileges.push({
editable: editable,
view_deleted: editable,
9 years ago
move: isAdminOrMod,
isAdminOrMod: isAdminOrMod,
'topics:read': results['topics:read'][i] || isAdminOrMod,
read: results.read[i] || isAdminOrMod
10 years ago
});
}
10 years ago
callback(null, privileges);
});
};
privileges.posts.can = function (privilege, pid, uid, callback) {
posts.getCidByPid(pid, function (err, cid) {
if (err) {
return callback(err);
}
11 years ago
privileges.categories.can(privilege, cid, uid, callback);
});
};
privileges.posts.filter = function (privilege, pids, uid, callback) {
if (!Array.isArray(pids) || !pids.length) {
return callback(null, []);
}
9 years ago
var cids;
var postData;
var tids;
var tidToTopic = {};
async.waterfall([
function (next) {
posts.getPostsFields(pids, ['uid', 'tid', 'deleted'], next);
},
function (_posts, next) {
postData = _posts;
tids = _posts.map(function (post) {
9 years ago
return post && post.tid;
}).filter(function (tid, index, array) {
9 years ago
return tid && array.indexOf(tid) === index;
});
topics.getTopicsFields(tids, ['deleted', 'cid'], next);
},
function (topicData, next) {
topicData.forEach(function (topic, index) {
9 years ago
if (topic) {
tidToTopic[tids[index]] = topic;
}
});
cids = postData.map(function (post, index) {
9 years ago
if (post) {
post.pid = pids[index];
post.topic = tidToTopic[post.tid];
}
return tidToTopic[post.tid] && tidToTopic[post.tid].cid;
}).filter(function (cid, index, array) {
9 years ago
return cid && array.indexOf(cid) === index;
});
9 years ago
privileges.categories.getBase(privilege, cids, uid, next);
},
function (results, next) {
var isModOf = {};
cids = cids.filter(function (cid, index) {
9 years ago
isModOf[cid] = results.isModerators[index];
return !results.categories[index].disabled &&
(results.allowedTo[index] || results.isAdmin || results.isModerators[index]);
});
pids = postData.filter(function (post) {
return post.topic && cids.indexOf(post.topic.cid) !== -1 &&
9 years ago
((parseInt(post.topic.deleted, 10) !== 1 && parseInt(post.deleted, 10) !== 1) || results.isAdmin || isModOf[post.cid]);
}).map(function (post) {
return post.pid;
});
plugins.fireHook('filter:privileges.posts.filter', {
privilege: privilege,
uid: uid,
pids: pids
}, function (err, data) {
9 years ago
next(err, data ? data.pids : null);
});
9 years ago
}
], callback);
};
privileges.posts.canEdit = function (pid, uid, callback) {
10 years ago
async.parallel({
isEditable: async.apply(isPostEditable, pid, uid),
isAdminOrMod: async.apply(isAdminOrMod, pid, uid)
}, function (err, results) {
10 years ago
if (err) {
return callback(err);
}
10 years ago
if (results.isAdminOrMod) {
9 years ago
return callback(null, {flag: true});
10 years ago
}
9 years ago
callback(null, results.isEditable);
10 years ago
});
};
privileges.posts.canDelete = function (pid, uid, callback) {
9 years ago
var postData;
async.waterfall([
function (next) {
9 years ago
posts.getPostFields(pid, ['tid', 'timestamp'], next);
},
function (_postData, next) {
9 years ago
postData = _postData;
async.parallel({
isAdminOrMod: async.apply(isAdminOrMod, 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)
9 years ago
}, next);
}
], function (err, results) {
9 years ago
if (err) {
return callback(err);
}
9 years ago
9 years ago
if (results.isAdminOrMod) {
9 years ago
return callback(null, {flag: true});
9 years ago
}
9 years ago
9 years ago
if (results.isLocked) {
9 years ago
return callback(null, {flag: false, message: '[[error:topic-locked]]'});
9 years ago
}
9 years ago
if (!results['posts:delete']) {
9 years ago
return callback(null, {flag: false, message: '[[error:no-privileges]]'});
}
9 years ago
9 years ago
var postDeleteDuration = parseInt(meta.config.postDeleteDuration, 10);
if (postDeleteDuration && (Date.now() - parseInt(postData.timestamp, 10) > postDeleteDuration * 1000)) {
9 years ago
return callback(null, {flag: false, message: '[[error:post-delete-duration-expired, ' + meta.config.postDeleteDuration + ']]'});
9 years ago
}
9 years ago
callback(null, {flag: results.isOwner, message: '[[error:no-privileges]]'});
9 years ago
});
};
privileges.posts.canMove = function (pid, uid, callback) {
posts.isMain(pid, function (err, isMain) {
if (err || isMain) {
return callback(err || new Error('[[error:cant-move-mainpost]]'));
}
isAdminOrMod(pid, uid, callback);
});
11 years ago
};
privileges.posts.canPurge = function (pid, uid, callback) {
10 years ago
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),
isAdminOrMod: async.apply(privileges.categories.isAdminOrMod, cid, uid)
}, next);
},
function (results, next) {
next(null, results.isAdminOrMod || (results.purge && results.owner));
}
], callback);
};
10 years ago
function isPostEditable(pid, uid, callback) {
9 years ago
var tid;
10 years ago
async.waterfall([
function (next) {
10 years ago
posts.getPostFields(pid, ['tid', 'timestamp'], next);
},
function (postData, next) {
9 years ago
tid = postData.tid;
10 years ago
var postEditDuration = parseInt(meta.config.postEditDuration, 10);
if (postEditDuration && Date.now() - parseInt(postData.timestamp, 10) > postEditDuration * 1000) {
9 years ago
return callback(null, {flag: false, message: '[[error:post-edit-duration-expired, ' + meta.config.postEditDuration + ']]'});
10 years ago
}
topics.isLocked(postData.tid, next);
},
function (isLocked, next) {
10 years ago
if (isLocked) {
9 years ago
return callback(null, {flag: false, message: '[[error:topic-locked]]'});
10 years ago
}
10 years ago
async.parallel({
owner: async.apply(posts.isOwner, pid, uid),
edit: async.apply(privileges.posts.can, 'posts:edit', pid, uid)
}, next);
10 years ago
},
function (result, next) {
9 years ago
next(null, {flag: result.owner && result.edit, message: '[[error:no-privileges]]'});
10 years ago
}
], callback);
}
11 years ago
function isAdminOrMod(pid, uid, callback) {
helpers.some([
function (next) {
posts.getCidByPid(pid, function (err, cid) {
if (err || !cid) {
return next(err, false);
}
10 years ago
user.isModerator(uid, cid, next);
});
},
function (next) {
user.isAdministrator(uid, next);
}
], callback);
11 years ago
}
};