public static MembershipList <Group> GetMemberOf(this IEnumerable <GroupMembershipItem> groupMembershipItems, SecurityPrincipalBase member, bool?includeDisabledMembership,
                                                         IList <Group> groups, IList <User> users = null)
        {
            if (!includeDisabledMembership.HasValue)
            {
                includeDisabledMembership = false;
            }

            MembershipList <Group> membership = new MembershipList <Group>();

            foreach (GroupMembershipItem gmi in groupMembershipItems)
            {
                if (gmi.MemberUId == member.UId)
                {
                    gmi.Resolve(groups, null);

                    if (gmi.Group.IsEnabled || includeDisabledMembership.Value)
                    {
                        membership.MemberList.Add(gmi.Group);
                    }
                }
            }

            if (groups != null)
            {
                membership.NonMemberList = new List <Group>();
                IEnumerator <Group> nonMembers = groups.Except(membership.MemberList).GetEnumerator();
                while (nonMembers.MoveNext())
                {
                    if (nonMembers.Current.IsLocal && nonMembers.Current.UId != member.UId && (nonMembers.Current.IsEnabled || includeDisabledMembership.Value))
                    {
                        membership.NonMemberList.Add(nonMembers.Current);
                    }
                }

                if (member is Group group)
                {
                    //Group thisGroup = membership.NonMemberList.FirstOrDefault( group => group.UId == member.UId );
                    //membership.NonMemberList.Remove( thisGroup );
                    IEnumerable <GroupMembershipItem> members = groupMembershipItems.GetByGroup(group, includeDisabledMembers: includeDisabledMembership.Value, groups: null, users: null);
                    foreach (GroupMembershipItem gmi in members)
                    {
                        if (!gmi.IsMemberUser)
                        {
                            gmi.Resolve(groups, null);

                            membership.NonMemberList.Remove((Group)gmi.Member);
                        }
                    }

                    List <Group> nonMembs    = membership.NonMemberList;
                    List <Group> nestedMembs = membership.NestedMemberList;
                    RecurseIneligibleNonMembers(groupMembershipItems, group, ref nonMembs, ref nestedMembs, groups, users);
                }
            }

            return(membership);
        }
        public static MembershipList <SecurityPrincipalBase> GetGroupMembers(this IEnumerable <GroupMembershipItem> groupMembershipItems, Group group, bool includeDisabledMembership = false,
                                                                             IList <Group> groups = null, IList <User> users = null)
        {
            MembershipList <SecurityPrincipalBase> membership = new MembershipList <SecurityPrincipalBase>();

            foreach (GroupMembershipItem gmi in groupMembershipItems)
            {
                if (gmi.GroupUId == group.UId)
                {
                    gmi.Resolve(groups, users);

                    if (gmi.Member.IsEnabled || includeDisabledMembership)
                    {
                        membership.MemberList.Add(gmi.Member);
                    }
                }
            }

            if (groups != null || users != null)
            {
                membership.NonMemberList = new List <SecurityPrincipalBase>();

                if (groups != null)
                {
                    IEnumerator <SecurityPrincipalBase> nonMembers = groups.Except(membership.MemberList).GetEnumerator();
                    while (nonMembers.MoveNext())
                    {
                        if (nonMembers.Current.UId != group.UId && (nonMembers.Current.IsEnabled || includeDisabledMembership))
                        {
                            membership.NonMemberList.Add(nonMembers.Current);
                        }
                    }
                }

                if (users != null)
                {
                    IEnumerator <SecurityPrincipalBase> nonMembers = users.Except(membership.MemberList).GetEnumerator();
                    while (nonMembers.MoveNext())
                    {
                        if (nonMembers.Current.UId != group.UId && (nonMembers.Current.IsEnabled || includeDisabledMembership))
                        {
                            membership.NonMemberList.Add(nonMembers.Current);
                        }
                    }
                }

                List <SecurityPrincipalBase> nonMembs    = membership.NonMemberList;
                List <SecurityPrincipalBase> nestedMembs = membership.NestedMemberList;
                RecurseIneligibleNonMembersUp(groupMembershipItems, group, ref nonMembs, ref nestedMembs, groups, users);
            }

            return(membership);
        }