Esempio n. 1
0
        private static IEnumerable <List <EntityToken> > GetAncestorChains(EntityToken descendant, int deep, List <EntityToken> visitedParents = null)
        {
            if (deep == 0)
            {
                yield return(new List <EntityToken>());

                yield break;
            }

            if (visitedParents == null)
            {
                visitedParents = new List <EntityToken>();
            }
            visitedParents.Add(descendant);

            List <EntityToken> parents = ParentsFacade.GetAllParents(descendant);

            if (parents.Count == 0)
            {
                var newChain = new List <EntityToken> {
                    descendant
                };
                yield return(newChain);

                yield break;
            }

            // NOTE: A workaround which gives "AllFunctionElementProvider" search results less priority that other function element providers
            if (parents.Count == 2 && parents[0].Id != null && parents[0].Id.StartsWith("ROOT:AllFunctionsElementProvider"))
            {
                parents.Reverse();
            }

            foreach (var parent in parents)
            {
                foreach (List <EntityToken> chain in GetAncestorChains(parent, deep - 1, visitedParents))
                {
                    chain.Add(descendant);
                    yield return(chain);
                }
            }
        }
        private static void CollectUsersAndGroupsRec(
            EntityToken entityToken,
            Dictionary <EntityToken, List <UserAccess> > userAccessDefinitions,
            Dictionary <EntityToken, List <GroupAccess> > groupAccessDefinitions,
            ImmutableHashSet <string> usersWithoutAccess,
            ImmutableHashSet <Guid> groupsWithoutAccess,
            HashSet <string> usersWithAccess,
            HashSet <Guid> groupsWithAccess,
            HashSet <EntityToken> alreadyVisited,
            int depth)
        {
            if (depth < 1)
            {
                return;
            }

            List <UserAccess> users;

            if (userAccessDefinitions.TryGetValue(entityToken, out users))
            {
                foreach (var user in users)
                {
                    if (!user.HasAccess)
                    {
                        usersWithoutAccess = usersWithoutAccess.Add(user.UserName);
                    }
                    else if (!usersWithoutAccess.Contains(user.UserName))
                    {
                        usersWithAccess.Add(user.UserName);
                    }
                }
            }

            List <GroupAccess> userGroups;

            if (groupAccessDefinitions.TryGetValue(entityToken, out userGroups))
            {
                foreach (var userGroup in userGroups)
                {
                    if (!userGroup.HasAccess)
                    {
                        groupsWithoutAccess = groupsWithoutAccess.Add(userGroup.UserGroupId);
                    }
                    else if (!groupsWithoutAccess.Contains(userGroup.UserGroupId))
                    {
                        groupsWithAccess.Add(userGroup.UserGroupId);
                    }
                }
            }

            alreadyVisited.Add(entityToken);
            var parents = ParentsFacade.GetAllParents(entityToken);

            foreach (var parent in parents)
            {
                if (alreadyVisited.Contains(parent))
                {
                    return;
                }

                CollectUsersAndGroupsRec(parent,
                                         userAccessDefinitions, groupAccessDefinitions,
                                         usersWithoutAccess, groupsWithoutAccess,
                                         usersWithAccess, groupsWithAccess,
                                         alreadyVisited, depth - 1);
            }
        }