| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-09-06 02:39:54 -04:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	posts = require('../posts'), | 
					
						
							| 
									
										
										
										
											2014-06-24 13:27:37 -04:00
										 |  |  | 	topics = require('../topics'), | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	helpers = require('./helpers'), | 
					
						
							|  |  |  | 	groups = require('../groups'), | 
					
						
							| 
									
										
										
										
											2014-11-14 17:31:39 -05:00
										 |  |  | 	categories = require('../categories'), | 
					
						
							|  |  |  | 	plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(privileges) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.posts = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 	privileges.posts.get = function(pids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-10-02 19:03:03 -04:00
										 |  |  | 		if (!Array.isArray(pids) || !pids.length) { | 
					
						
							|  |  |  | 			return callback(null, []); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 			isAdmin: function(next){ | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 				user.isAdministrator(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 			isModerator: function(next) { | 
					
						
							|  |  |  | 				posts.isModerator(pids, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			isOwner: function(next) { | 
					
						
							|  |  |  | 				posts.isOwner(pids, uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 			var privileges = []; | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 			for (var i=0; i<pids.length; ++i) { | 
					
						
							|  |  |  | 				var editable = results.isAdmin || results.isModerator[i] || results.isOwner[i]; | 
					
						
							|  |  |  | 				privileges.push({ | 
					
						
							|  |  |  | 					editable: editable, | 
					
						
							|  |  |  | 					view_deleted: editable, | 
					
						
							|  |  |  | 					move: results.isAdmin || results.isModerator[i] | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 			callback(null, privileges); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-17 18:59:34 -04:00
										 |  |  | 	privileges.posts.can = function(privilege, pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		posts.getCidByPid(pid, function(err, cid) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-17 18:59:34 -04:00
										 |  |  | 			privileges.categories.can(privilege, cid, uid, callback); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 	privileges.posts.filter = function(privilege, pids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-09 01:30:27 -05:00
										 |  |  | 		if (!Array.isArray(pids) || !pids.length) { | 
					
						
							| 
									
										
										
										
											2014-07-31 17:29:20 -04:00
										 |  |  | 			return callback(null, []); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 		posts.getCidsByPids(pids, function(err, cids) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			pids = pids.map(function(pid, index) { | 
					
						
							|  |  |  | 				return {pid: pid, cid: cids[index]}; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 15:19:57 -04:00
										 |  |  | 			privileges.categories.filterCids(privilege, cids, uid, function(err, cids) { | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				pids = pids.filter(function(post) { | 
					
						
							|  |  |  | 					return cids.indexOf(post.cid) !== -1; | 
					
						
							|  |  |  | 				}).map(function(post) { | 
					
						
							|  |  |  | 					return post.pid; | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-11-14 17:31:39 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				plugins.fireHook('filter:privileges.posts.filter', { | 
					
						
							|  |  |  | 					privilege: privilege, | 
					
						
							|  |  |  | 					uid: uid, | 
					
						
							|  |  |  | 					pids: pids | 
					
						
							| 
									
										
										
										
											2014-11-14 17:53:20 -05:00
										 |  |  | 				},  function(err, data) { | 
					
						
							|  |  |  | 					callback(err, data ? data.pids : null); | 
					
						
							| 
									
										
										
										
											2014-11-14 17:31:39 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	privileges.posts.canEdit = function(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			isEditable: async.apply(isPostEditable, pid, uid), | 
					
						
							|  |  |  | 			isAdminOrMod: async.apply(isAdminOrMod, pid, uid) | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 			if (results.isAdminOrMod) { | 
					
						
							|  |  |  | 				return callback(null, true); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (results.isEditable.isLocked) { | 
					
						
							| 
									
										
										
										
											2015-04-21 16:26:24 -04:00
										 |  |  | 				return callback(new Error('[[error:topic-locked]]')); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (results.isEditable.isEditExpired) { | 
					
						
							|  |  |  | 				return callback(new Error('[[error:post-edit-duration-expired, ' + meta.config.postEditDuration + ']]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			callback(null, results.isEditable.editable); | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.posts.canMove = function(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-06-23 18:06:59 -04:00
										 |  |  | 		posts.isMain(pid, function(err, isMain) { | 
					
						
							|  |  |  | 			if (err || isMain) { | 
					
						
							|  |  |  | 				return callback(err || new Error('[[error:cant-move-mainpost]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			isAdminOrMod(pid, uid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-06-23 17:26:02 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-16 08:35:40 -04:00
										 |  |  | 	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), | 
					
						
							|  |  |  | 					isAdminOrMod: async.apply(privileges.categories.isAdminOrMod, cid, uid) | 
					
						
							|  |  |  | 				}, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function (results, next) { | 
					
						
							|  |  |  | 				next(null, results.isAdminOrMod || (results.purge && results.owner)); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 	function isPostEditable(pid, uid, callback) { | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				posts.getPostFields(pid, ['tid', 'timestamp'], next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(postData, next) { | 
					
						
							|  |  |  | 				var postEditDuration = parseInt(meta.config.postEditDuration, 10); | 
					
						
							|  |  |  | 				if (postEditDuration && Date.now() - parseInt(postData.timestamp, 10) > postEditDuration * 1000) { | 
					
						
							|  |  |  | 					return callback(null, {isEditExpired: true}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				topics.isLocked(postData.tid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(isLocked, next) { | 
					
						
							|  |  |  | 				if (isLocked) { | 
					
						
							|  |  |  | 					return callback(null, {isLocked: true}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				posts.isOwner(pid, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(isOwner, next) { | 
					
						
							|  |  |  | 				next(null, {editable: isOwner}); | 
					
						
							| 
									
										
										
										
											2015-02-25 15:37:33 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-24 13:27:37 -04:00
										 |  |  | 	function isPostTopicLocked(pid, callback) { | 
					
						
							|  |  |  | 		posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			topics.isLocked(tid, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-23 17:26:02 -04:00
										 |  |  | 	function isAdminOrMod(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		helpers.some([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				posts.getCidByPid(pid, function(err, cid) { | 
					
						
							| 
									
										
										
										
											2015-02-24 13:02:58 -05:00
										 |  |  | 					if (err || !cid) { | 
					
						
							|  |  |  | 						return next(err, false); | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2015-03-30 13:31:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 					user.isModerator(uid, cid, next); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				user.isAdministrator(uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], callback); | 
					
						
							| 
									
										
										
										
											2014-06-23 17:26:02 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | }; |