| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 12:12:46 +02:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | var _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-27 12:58:08 +03:00
										 |  |  | var groups = require('../groups'); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | var helpers = module.exports; | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | var uidToSystemGroup = { | 
					
						
							|  |  |  | 	0: 'guests', | 
					
						
							|  |  |  | 	'-1': 'spiders', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.some = function (tasks, callback) { | 
					
						
							|  |  |  | 	async.some(tasks, function (task, next) { | 
					
						
							| 
									
										
										
										
											2017-01-02 22:23:17 -07:00
										 |  |  | 		task(next); | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2014-05-15 10:38:02 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.isUserAllowedTo = function (privilege, uid, cid, callback) { | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | 	if (Array.isArray(privilege) && !Array.isArray(cid)) { | 
					
						
							|  |  |  | 		isUserAllowedToPrivileges(privilege, uid, cid, callback); | 
					
						
							|  |  |  | 	} else if (Array.isArray(cid) && !Array.isArray(privilege)) { | 
					
						
							|  |  |  | 		isUserAllowedToCids(privilege, uid, cid, callback); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isUserAllowedToCids(privilege, uid, cids, callback) { | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 	if (parseInt(uid, 10) <= 0) { | 
					
						
							|  |  |  | 		return isSystemGroupAllowedToCids(privilege, uid, cids, callback); | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 20:20:42 -07:00
										 |  |  | 	var userKeys = []; | 
					
						
							|  |  |  | 	var groupKeys = []; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 	cids.forEach(function (cid) { | 
					
						
							|  |  |  | 		userKeys.push('cid:' + cid + ':privileges:' + privilege); | 
					
						
							|  |  |  | 		groupKeys.push('cid:' + cid + ':privileges:groups:' + privilege); | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	checkIfAllowed(uid, userKeys, groupKeys, callback); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function isUserAllowedToPrivileges(privileges, uid, cid, callback) { | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 	if (parseInt(uid, 10) <= 0) { | 
					
						
							|  |  |  | 		return isSystemGroupAllowedToPrivileges(privileges, uid, cid, callback); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-17 20:20:42 -07:00
										 |  |  | 	var userKeys = []; | 
					
						
							|  |  |  | 	var groupKeys = []; | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 	privileges.forEach(function (privilege) { | 
					
						
							|  |  |  | 		userKeys.push('cid:' + cid + ':privileges:' + privilege); | 
					
						
							|  |  |  | 		groupKeys.push('cid:' + cid + ':privileges:groups:' + privilege); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 	checkIfAllowed(uid, userKeys, groupKeys, callback); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | function checkIfAllowed(uid, userKeys, groupKeys, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				hasUserPrivilege: function (next) { | 
					
						
							|  |  |  | 					groups.isMemberOfGroups(uid, userKeys, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				hasGroupPrivilege: function (next) { | 
					
						
							|  |  |  | 					groups.isMemberOfGroupsList(uid, groupKeys, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			var result = userKeys.map(function (key, index) { | 
					
						
							|  |  |  | 				return results.hasUserPrivilege[index] || results.hasGroupPrivilege[index]; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 			next(null, result); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | helpers.isUsersAllowedTo = function (privilege, uids, cid, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				hasUserPrivilege: function (next) { | 
					
						
							|  |  |  | 					groups.isMembers(uids, 'cid:' + cid + ':privileges:' + privilege, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				hasGroupPrivilege: function (next) { | 
					
						
							|  |  |  | 					groups.isMembersOfGroupList(uids, 'cid:' + cid + ':privileges:groups:' + privilege, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2014-09-09 15:19:57 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			var result = uids.map(function (uid, index) { | 
					
						
							|  |  |  | 				return results.hasUserPrivilege[index] || results.hasGroupPrivilege[index]; | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-09-09 15:19:57 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-25 21:17:20 -04:00
										 |  |  | 			next(null, result); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2014-09-09 15:19:57 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | function isSystemGroupAllowedToCids(privilege, uid, cids, callback) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 	const groupKeys = cids.map(cid => 'cid:' + cid + ':privileges:groups:' + privilege); | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 	groups.isMemberOfGroups(uidToSystemGroup[uid], groupKeys, callback); | 
					
						
							| 
									
										
										
										
											2014-07-29 21:51:46 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-05-14 17:53:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | function isSystemGroupAllowedToPrivileges(privileges, uid, cid, callback) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 	const groupKeys = privileges.map(privilege => 'cid:' + cid + ':privileges:groups:' + privilege); | 
					
						
							| 
									
										
										
										
											2018-01-31 15:20:17 -05:00
										 |  |  | 	groups.isMemberOfGroups(uidToSystemGroup[uid], groupKeys, callback); | 
					
						
							| 
									
										
										
										
											2016-09-15 14:01:56 +03:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | helpers.getUserPrivileges = function (cid, hookName, userPrivilegeList, callback) { | 
					
						
							|  |  |  | 	var userPrivileges; | 
					
						
							|  |  |  | 	var memberSets; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		async.apply(plugins.fireHook, hookName, userPrivilegeList.slice()), | 
					
						
							|  |  |  | 		function (_privs, next) { | 
					
						
							|  |  |  | 			userPrivileges = _privs; | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			groups.getMembersOfGroups(userPrivileges.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (_memberSets, next) { | 
					
						
							|  |  |  | 			memberSets = _memberSets.map(function (set) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 				return set.map(uid => parseInt(uid, 10)); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var members = _.uniq(_.flatten(memberSets)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			user.getUsersFields(members, ['picture', 'username'], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (memberData, next) { | 
					
						
							|  |  |  | 			memberData.forEach(function (member) { | 
					
						
							|  |  |  | 				member.privileges = {}; | 
					
						
							|  |  |  | 				for (var x = 0, numPrivs = userPrivileges.length; x < numPrivs; x += 1) { | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 					member.privileges[userPrivileges[x]] = memberSets[x].includes(parseInt(member.uid, 10)); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			next(null, memberData); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | helpers.getGroupPrivileges = function (cid, hookName, groupPrivilegeList, callback) { | 
					
						
							|  |  |  | 	var groupPrivileges; | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		async.apply(plugins.fireHook, hookName, groupPrivilegeList.slice()), | 
					
						
							|  |  |  | 		function (_privs, next) { | 
					
						
							|  |  |  | 			groupPrivileges = _privs; | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				memberSets: function (next) { | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 					groups.getMembersOfGroups(groupPrivileges.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				groupNames: function (next) { | 
					
						
							|  |  |  | 					groups.getGroups('groups:createtime', 0, -1, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			var memberSets = results.memberSets; | 
					
						
							|  |  |  | 			var uniqueGroups = _.uniq(_.flatten(memberSets)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			var groupNames = results.groupNames.filter(groupName => !groupName.includes(':privileges:') && uniqueGroups.includes(groupName)); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			groupNames = groups.ephemeralGroups.concat(groupNames); | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | 			moveToFront(groupNames, 'Global Moderators'); | 
					
						
							|  |  |  | 			moveToFront(groupNames, 'registered-users'); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var adminIndex = groupNames.indexOf('administrators'); | 
					
						
							|  |  |  | 			if (adminIndex !== -1) { | 
					
						
							|  |  |  | 				groupNames.splice(adminIndex, 1); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var memberPrivs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			var memberData = groupNames.map(function (member) { | 
					
						
							|  |  |  | 				memberPrivs = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				for (var x = 0, numPrivs = groupPrivileges.length; x < numPrivs; x += 1) { | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 					memberPrivs[groupPrivileges[x]] = memberSets[x].includes(member); | 
					
						
							| 
									
										
										
										
											2017-12-20 14:49:20 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				return { | 
					
						
							|  |  |  | 					name: member, | 
					
						
							|  |  |  | 					privileges: memberPrivs, | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			next(null, memberData); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (memberData, next) { | 
					
						
							|  |  |  | 			// Grab privacy info for the groups as well
 | 
					
						
							|  |  |  | 			async.map(memberData, function (member, next) { | 
					
						
							|  |  |  | 				async.waterfall([ | 
					
						
							|  |  |  | 					function (next) { | 
					
						
							|  |  |  | 						groups.isPrivate(member.name, next); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					function (isPrivate, next) { | 
					
						
							|  |  |  | 						member.isPrivate = isPrivate; | 
					
						
							|  |  |  | 						next(null, member); | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 				], next); | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2017-12-20 15:19:22 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-30 19:30:47 -04:00
										 |  |  | function moveToFront(groupNames, groupToMove) { | 
					
						
							|  |  |  | 	const index = groupNames.indexOf(groupToMove); | 
					
						
							|  |  |  | 	if (index !== -1) { | 
					
						
							|  |  |  | 		groupNames.splice(0, 0, groupNames.splice(index, 1)[0]); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		groupNames.unshift(groupToMove); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-15 13:45:55 -04:00
										 |  |  | helpers.giveOrRescind = function (method, privileges, cids, groupNames, callback) { | 
					
						
							|  |  |  | 	groupNames = Array.isArray(groupNames) ? groupNames : [groupNames]; | 
					
						
							|  |  |  | 	cids = Array.isArray(cids) ? cids : [cids]; | 
					
						
							|  |  |  | 	async.eachSeries(groupNames, function (groupName, next) { | 
					
						
							|  |  |  | 		var groupKeys = []; | 
					
						
							|  |  |  | 		cids.forEach((cid) => { | 
					
						
							|  |  |  | 			privileges.forEach((privilege) => { | 
					
						
							|  |  |  | 				groupKeys.push('cid:' + cid + ':privileges:groups:' + privilege); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		method(groupKeys, groupName, next); | 
					
						
							| 
									
										
										
										
											2018-01-03 13:27:30 -05:00
										 |  |  | 	}, callback); | 
					
						
							|  |  |  | }; |