public static bool Resolve(this GroupMembershipItem gm, IList <Group> groups, IList <User> users, bool force = false)
        {
            if (gm.Group == null || gm.Member == null || force)
            {
                try
                {
                    if (gm.Group == null || force)
                    {
                        gm.Group = groups.GetByUId <Group>(gm.GroupUId);
                    }

                    if (gm.Member == null || force)
                    {
                        gm.Member = gm.IsMemberUser ?
                                    users?.GetByUId <SecurityPrincipalBase>(gm.MemberUId) :
                                    groups.GetByUId <SecurityPrincipalBase>(gm.MemberUId);
                    }

                    return(gm.Group != null && gm.Member != null);
                }
                catch { return(false); }
            }
            else
            {
                return(true);
            }
        }
        public static List <Group> GetGroupHierarchy(this IEnumerable <GroupMembershipItem> groupMembershipItems, Group g)
        {
            List <Group>          groups       = new List <Group>();
            Stack <Group>         parentGroups = new Stack <Group>();
            GroupEqualityComparer comparer     = new GroupEqualityComparer();

            Stack <GroupMembershipItem> parentItems = new Stack <GroupMembershipItem>();

            IEnumerable <GroupMembershipItem> parents = groupMembershipItems.Where(sp => sp.MemberUId == g.UId);

            foreach (GroupMembershipItem gmi in parents)
            {
                parentItems.Push(gmi);
            }

            if (parentItems.Count > 0)
            {
                while (parentItems.Count > 0)
                {
                    GroupMembershipItem p = parentItems.Pop();
                    IEnumerable <GroupMembershipItem> ascendants = groupMembershipItems.Where(sp => sp.MemberUId == p.GroupUId);
                    int count = 0;
                    foreach (GroupMembershipItem gmi in ascendants)
                    {
                        parentItems.Push(gmi);
                        count++;
                    }
                    if (count == 0)
                    {
                        if (!groups.Contains(p.Group, comparer))
                        {
                            parentGroups.Push(p.Group);
                            groups.Add(p.Group);
                        }
                    }
                }
            }
            else
            {
                parentGroups.Push(g);
                groups.Add(g);
            }

            while (parentGroups.Count > 0)
            {
                Group p = parentGroups.Pop();
                IEnumerable <GroupMembershipItem> descendants = groupMembershipItems.Where(gmi => gmi.GroupUId == p.UId && !gmi.IsMemberUser);
                foreach (GroupMembershipItem chi in descendants)
                {
                    Group ch = chi.Member as Group;
                    if (!p.Groups.Contains(ch, comparer))
                    {
                        p.Groups.Add(ch);
                        parentGroups.Push(ch);
                    }
                }
            }

            return(groups);
        }
 public static bool ContainsItem(this IEnumerable <GroupMembershipItem> groupMembershipItems, GroupMembershipItem item)
 {
     return(groupMembershipItems.Contains(item, new GroupMembershipEqualityComparer()));
 }
 public static GroupMembershipItem GetByGroupAndMemberOrDefault(this IEnumerable <GroupMembershipItem> groupMembershipItems, GroupMembershipItem groupMembershipItem)
 {
     return(groupMembershipItems.SingleOrDefault(
                gmi => gmi.GroupUId == groupMembershipItem.GroupUId && gmi.MemberUId == groupMembershipItem.MemberUId));
 }
        public static IEnumerable <GroupMembershipItem> GetGroupMembershipHierarchy(this IEnumerable <GroupMembershipItem> groupMembershipItems,
                                                                                    Guid memberUId, bool includeDisabledMembership, IList <Group> groups, IList <User> users, bool forceResolution = false)
        {
            List <GroupMembershipItem> result = new List <GroupMembershipItem>();

            IEnumerable <GroupMembershipItem> membership = groupMembershipItems.Where(item => item.MemberUId == memberUId);
            List <GroupMembershipItem>        items      = new List <GroupMembershipItem>();

            foreach (GroupMembershipItem m in membership)
            {
                bool ok = includeDisabledMembership;

                if (!ok)
                {
                    if (m.Resolve(groups, users, forceResolution))
                    {
                        ok = m.Group.IsEnabled;
                    }
                }

                if (ok)
                {
                    result.Add(m);
                    items.Add(m);
                }
            }

            foreach (GroupMembershipItem item in items)
            {
                Stack <GroupMembershipItem> parentItems = new Stack <GroupMembershipItem>();

                IEnumerable <GroupMembershipItem> parents = groupMembershipItems.Where(sp => sp.MemberUId == item.GroupUId);
                foreach (GroupMembershipItem m in parents)
                {
                    bool ok = includeDisabledMembership;

                    if (!ok)
                    {
                        if (m.Resolve(groups, users, forceResolution))
                        {
                            ok = m.Group.IsEnabled;
                        }
                    }

                    if (ok)
                    {
                        result.Add(m);
                        parentItems.Push(m);
                    }
                }

                while (parentItems.Count > 0)
                {
                    GroupMembershipItem p = parentItems.Pop();
                    IEnumerable <GroupMembershipItem> ascendants = groupMembershipItems.Where(sp => sp.MemberUId == p.GroupUId);
                    foreach (GroupMembershipItem m in ascendants)
                    {
                        bool ok = includeDisabledMembership;

                        if (!ok)
                        {
                            if (m.Resolve(groups, users, forceResolution))
                            {
                                ok = m.Group.IsEnabled;
                            }
                        }

                        if (ok)
                        {
                            result.Add(m);
                            parentItems.Push(m);
                        }
                    }
                }
            }

            return(result);
        }