How to get the direct group names for an user?

Is there a way to get the group names by passing the user name who are the direct users of that groups.

Example:

Parent Group: Group 1, Child Group:Group 2

Group 1:(user A, user B), Group 2:(user c)

If I pass user c, the result should be Group 2.

getgroupsformemberuser() returns all the groups irrespective of direct or indirect membership

  • I think you need to break this down into a set of discrete steps:

    1. get a list of all the Groups in the hierarchy - in effect, "flatten" the hierarchy so you can test each Group independently
    2. iterate through the list of Groups so you can test them one at a time
    3. get a list of the direct User Members of each Group and test to see if the User you're working with is in that list. If so, return the Group.
  • This might work:

    load(
      local!groups: getgroupsformemberuser(ri!user),
      difference( 
        local!groups,
        getmembergroups(local!groups)
      )
    )

  • a!forEach(
    items :getgroupsformemberuser(loggedInUser()),
    expression :group(fv!item,"groupName")
    )

  • This will do it, but for environments with a very large number of users and/or groups it may take a while and consume a lot of memory. Our environment has only about 1k users, so we can get away with it.

    You will need the People Functions and Group Functions plugins to use this. Replace rule!CMN_checkIsNullOrEmpty with your favorite null/empty list checking function.

    Also note that groups will still be returned where the user's Member Type is Rule. Not sure how to get around that.

    with(
      local!groups: getgroupsformemberuser(ri!user),
      a!forEach(
        items: local!groups,
        expression: with(
          local!directMembers: getdirectgroupmemberuserspaging(fv!item,0,-1),
          if(
            and(
              not(rule!CMN_checkIsNullOrEmpty(local!directMembers)),
              contains(local!directMembers, ri!user)
            ),
            fv!item,
            {}
          )
        )
      )
    )

 Discussion posts and replies are publicly visible