| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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'), | 
					
						
							|  |  |  | 	categories = require('../categories'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(privileges) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.posts = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-28 14:59:01 -04:00
										 |  |  | 	privileges.posts.get = function(pids, uid, callback) { | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			manage_content: function(next) { | 
					
						
							|  |  |  | 				helpers.hasEnoughReputationFor('privileges:manage_content', uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			manage_topic: function(next) { | 
					
						
							|  |  |  | 				helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			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-06-28 14:59:01 -04:00
										 |  |  | 			var userPriv = userResults.isAdministrator || userResults.manage_topic || userResults.manage_content; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				isOwner: function(next) { | 
					
						
							|  |  |  | 					posts.isOwner(pids, uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				isModerator: function(next) { | 
					
						
							|  |  |  | 					posts.getCidsByPids(pids, function(err, cids) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						user.isModerator(uid, cids, next); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, 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
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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) { | 
					
						
							|  |  |  | 							helpers.hasEnoughReputationFor('privileges:manage_content', uid, next); | 
					
						
							|  |  |  | 						}, | 
					
						
							|  |  |  | 						function(next) { | 
					
						
							|  |  |  | 							helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					], 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
										 |  |  | }; |