Get members in multiple groups

Certified Senior Developer

I'm trying to think my way through a problem. What I need is, given a list of groups, I want to see a list of users that are members of all of those groups. The existing tools almost, but not quite do what I need. 

groupMembers() will give me the members of a single group, so I thought maybe if I got the members of each group and got the union() of them, that would solve my problem, but union only takes 2 lists and looping through with foreach  (I do so wish there were other types of loops!) would mean somehow getting the union of each member of the list, then unioning those results until I get down to just one list, but that sounds like a pain to code and probably not terribly efficient. 

isMemberOfGroups() is nice in that I can take a user and see if they are a member of all of a list of groups, but then I have to loop through basically all users (or at least all users in some initial group) and ultimately I am not sure how many users there could be or whether the maximum batch size of 10,000 will be enough. That doesn't seem like a whole lot of users to ultimately have as a limitation. 

Then there's getdistinctusers() which is kind of the opposite of what I want? It gives me all users across a set of groups with no duplicates, I think?

Are one of these methods the way to go? Am I missing something? 

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Lead Developer
    in reply to Marco

    urgh.  that happens for me too now that i test it more broadly.

    the only other thing i can think of is to abandon intersection() and reduce() entirely and implement your own stuff.  the steps i'd thought through earlier would be along the lines of this:

    1) iterate over all groups grabbing all members

    2) make a single array comprising all unique usernames among all groups

    3) iterate over the list of all usernames and for each one, loop over all original group lists checking that the current user is in all of them (if not, discard)

    4) you should now end up with a list of all usernames in all groups provided.

    This would likely get a *little* processing-intensive if you feed in an obscenely large number of groups, but then again the previous solution probably would too.

Children
No Data