| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	posts = require('../posts'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	helpers = require('./helpers'), | 
					
						
							|  |  |  | 	groups = require('../groups'), | 
					
						
							|  |  |  | 	categories = require('../categories'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(privileges) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.posts = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	privileges.posts.get = function(pid, uid, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			isOwner: function(next) { | 
					
						
							|  |  |  | 				posts.isOwner(pid, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			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); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			isModerator: function(next) { | 
					
						
							|  |  |  | 				posts.getCidByPid(pid, function(err, cid) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					user.isModerator(uid, cid, next); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, results) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var editable = results.isAdministrator || results.isModerator || results.manage_content || results.manage_topic || results.isOwner; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			callback(null, { | 
					
						
							| 
									
										
										
										
											2014-05-22 14:56:15 -04:00
										 |  |  | 				editable: editable, | 
					
						
							|  |  |  | 				view_deleted: editable, | 
					
						
							|  |  |  | 				move: results.isAdministrator || results.isModerator | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 				posts.isOwner(pid, uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				helpers.hasEnoughReputationFor('privileges:manage_content', uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				helpers.hasEnoughReputationFor('privileges:manage_topic', uid, next); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			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 17:26:02 -04:00
										 |  |  | 		isAdminOrMod(pid, uid, callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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
										 |  |  | }; |