示例#1
0
        private static HashSet <Menu> GetAllParentsByMenuList(HashSet <Menu> menuList)
        {
            MenuComparer   comparer = new MenuComparer();
            HashSet <Menu> parents  = null;

            try
            {
                using (var ctx = new SuchDBContext())
                {
                    HashSet <int?> parentIds = menuList.Where(m => m.Parent != null).Select(m => m.Parent).ToHashSet();
                    if (parentIds.Count() < 1)
                    {
                        return(parents);
                    }
                    parents = ctx.Menu.Where(m => parentIds.Contains(m.Id)).ToHashSet(comparer);
                    if (parents.FirstOrDefault(m => m.Parent != null) != null)
                    {
                        parents.UnionWith(GetAllParentsByMenuList(parents));
                    }
                    ;
                    return(parents);
                }
            }
            catch (Exception ex)
            {
                return(parents);
            }
        }
示例#2
0
        public static List <Menu> GetAllByUserId(string userId)
        {
            try
            {
                using (var ctx = new SuchDBContext())
                {
                    MenuComparer   comparer = new MenuComparer();
                    HashSet <Menu> menuList = null;

                    var user = DBUserConfigurations.GetById(userId);
                    if (user == null)
                    {
                        throw new Exception();
                    }


                    if (user.Administrador && true)
                    {
                        menuList = ctx.Menu.Where(p => p.Active == true).ToHashSet(comparer);
                    }
                    else
                    {
                        HashSet <int> featuresIds = GetFeaturesByUserId(userId);
                        HashSet <int> menusIds    = null;

                        // list menu id from features
                        if (featuresIds != null && featuresIds.Count() > 0)
                        {
                            menusIds = ctx.FeaturesMenus.Where(fm => featuresIds.Contains(fm.IdFeature)).Select(fm => fm.IdMenu).ToHashSet();
                        }
                        // get menu
                        if (menusIds != null && menusIds.Count() > 0)
                        {
                            var userMenuList   = ctx.Menu.Where(m => menusIds.Contains(m.Id) && m.Active).ToHashSet(comparer);
                            var parentMenuList = GetAllParentsByMenuList(userMenuList);
                            menuList = userMenuList.Union(parentMenuList).ToHashSet(comparer);
                        }
                    }

                    return(menuList.OrderBy(m => m.Weight).ToList());
                }
            }
            catch (Exception ex)
            {
                return(null);
            }
        }