| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							|  |  |  | 	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, []); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2014-11-04 18:44:04 -05:00
										 |  |  | 			manage: function(next) { | 
					
						
							|  |  |  | 				helpers.hasEnoughReputationFor(['privileges:manage_content', 'privileges:manage_topic'], uid, next); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			isAdministrator: function(next) { | 
					
						
							|  |  |  | 				user.isAdministrator(uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 		}, function(err, userResults) { | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-04 18:44:04 -05:00
										 |  |  | 			var userPriv = userResults.isAdministrator || userResults.manage; | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				isOwner: function(next) { | 
					
						
							|  |  |  | 					posts.isOwner(pids, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				isModerator: function(next) { | 
					
						
							| 
									
										
										
										
											2014-11-04 19:05:55 -05:00
										 |  |  | 					posts.isModerator(pids, uid, next); | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, postResults) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var privileges = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (var i=0; i<pids.length; ++i) { | 
					
						
							|  |  |  | 					var editable = userPriv || postResults.isModerator[i] || postResults.isOwner[i]; | 
					
						
							|  |  |  | 					privileges.push({ | 
					
						
							|  |  |  | 						editable: editable, | 
					
						
							|  |  |  | 						view_deleted: editable, | 
					
						
							|  |  |  | 						move: userResults.isAdministrator || postResults.isModerator[i] | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -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 | 
					
						
							|  |  |  | 				}, function(err, data) { | 
					
						
							|  |  |  | 					callback(null, data.pids); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	privileges.posts.canEdit = function(pid, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		helpers.some([ | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-24 13:27:37 -04:00
										 |  |  | 				isPostTopicLocked(pid, function(err, isLocked) { | 
					
						
							|  |  |  | 					if (err || isLocked) { | 
					
						
							|  |  |  | 						return next(err, false); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					helpers.some([ | 
					
						
							|  |  |  | 						function(next) { | 
					
						
							|  |  |  | 							posts.isOwner(pid, uid, next); | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						function(next) { | 
					
						
							| 
									
										
										
										
											2014-11-04 18:44:04 -05:00
										 |  |  | 							helpers.hasEnoughReputationFor(['privileges:manage_content', 'privileges:manage_topic'], uid, next); | 
					
						
							| 
									
										
										
										
											2014-06-24 13:27:37 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					], next); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-23 17:26:02 -04:00
										 |  |  | 				isAdminOrMod(pid, uid, next); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | 		], callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					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
										 |  |  | }; |