Skip to content

Instantly share code, notes, and snippets.

@alexfaber2011
Created June 5, 2014 17:13
Show Gist options
  • Save alexfaber2011/9958fad07aca4cbad01c to your computer and use it in GitHub Desktop.
Save alexfaber2011/9958fad07aca4cbad01c to your computer and use it in GitHub Desktop.
Reactivity Problem with $in - Case 135
#roles_custom.coffee (src/lib/roles_custom.coffee)
#TODO swap this function out with James's implementation or harness a proper implementation - Case 129
@getGroupsForUser = (userId) ->
user = Meteor.users.findOne _id: userId
if user and user.roles
Object.keys user.roles
#checks to see if a user is the admin of user's group
#returns empty array if not admin of any groups, or array of strings of groups user is admin of
#TODO move this to a more appropriate spot
@hasRoleAdmin = (userId) ->
groups = getGroupsForUser(userId)
#Make sure to check if the user is part of the Roles.GLOBAL_GROUP
groups.push Roles.GLOBAL_GROUP
adminGroups = []
_.each groups, (group) ->
if Roles.userIsInRole userId, "admin", group
adminGroups.push group
return adminGroups
#This function will return all userIds (in an array) including the idea of user itself of all of the groups the said user
#is an admin of
@getAllUsersInAllGroups = (userId) ->
#determine if user is an admin in any of his/her groups, unfortunately we will have to check if admin of group and not just an admin in general
#1) find groups, if any
# console.log userId
groupUserIds = []
groups = getGroupsForUser userId
#Make sure to check if the user is part of the Roles.GLOBAL_GROUP
groups.push Roles.GLOBAL_GROUP
#2) iterate over each group in find all the 'users' in that group if the user is admin
_.each groups, (group) ->
if Roles.userIsInRole userId, "admin", group
userIds = (user._id for user in Roles.getUsersInRole("user", group).fetch())
_.each userIds, (userId) ->
groupUserIds.push userId
#3) add own id to the list
if groupUserIds
#push own id onto array of ids
groupUserIds.push userId
# console.log "[getAllUsersInAllGroups]"
# console.log groupUserIds
return groupUserIds
############################################################################################################################
#publications.coffee
Meteor.publish "groupUsers", ->
user = Users.findOne _id: @userId
if user
Users.find({}) #reactive
Users.find _id: #not reactive
$in: getAllUsersInAllGroups(@userId)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment