Esempio n. 1
0
        public ActionResult Main()
        {
            var user = SysService.GetCurrentUser();

            if (user == null)
            {
                return(Redirect("Account/Login"));
            }

            if (user.UserID.Equals("U00001"))
            {
                return(View(AppConfig.Current.Menus));
            }

            var userPages = SysService.GetUserPages(user.UserID);
            Dictionary <string, Page> pages = new Dictionary <string, Page>();

            foreach (var group in AppConfig.Current.PageGroups)
            {
                foreach (var item in group.Pages)
                {
                    pages[item.PageID] = item;
                }
            }

            Func <Menu, bool> isUserPage = (m) =>
            {
                if (string.IsNullOrWhiteSpace(m.Src))
                {
                    return(true);
                }

                if (!m.Src.StartsWith("Page", StringComparison.OrdinalIgnoreCase))
                {
                    return(true);
                }

                if (m.Src.Length < 11)
                {
                    return(true);
                }
                var pageID = m.Src.Substring(5, 6);

                var userPage = userPages.FirstOrDefault(p => p.PageID.Equals(pageID, StringComparison.OrdinalIgnoreCase));
                if (userPage == null)
                {
                    return(false);
                }

                Page page = pages.GetDictionaryValue(pageID, null);
                if (page == null)
                {
                    return(false);
                }

                var index      = m.Src.IndexOf('?');
                var actionName = m.Src.Length < 12 ? "Index" :
                                 index > 0 ? m.Src.Substring(12, index - 12) :
                                 m.Src.Substring(12);
                var action = page.Config.Actions.FirstOrDefault(a => a.Name.Equals(actionName, StringComparison.OrdinalIgnoreCase));
                if (action == null)
                {
                    return(false);
                }

                if (action.ActionValue == 0)
                {
                    return(true);
                }

                return((userPage.ActionValue & action.ActionValue) == action.ActionValue);
            };

            MenuCollection menus    = new MenuCollection();
            Menu           topmenus = new Menu();

            foreach (var topm in AppConfig.Current.Menus)
            {
                var menu = new Menu {
                    Title = topm.Title, Name = topm.Name
                };
                foreach (var group in topm.Menus)
                {
                    var smenus = group.Menus.Where(m => isUserPage(m)).ToList();
                    if (smenus.Count == 0)
                    {
                        continue;
                    }
                    var tmenu = new Menu {
                        Title = group.Title, Name = group.Name
                    };
                    smenus.AddToCollection(tmenu.Menus);
                    menu.Menus.Add(tmenu);
                    //menusAdd(tmenu);
                }
                menus.Add(menu);
            }

            //foreach (var group in topmenus.Menus)
            //{


            //    var menu = new Menu { Title = group.Title, Name= group.Name };
            //    smenus.AddToCollection(menu.Menus);
            //    menus.Add(menu);
            //}

            return(View(menus));
        }