예제 #1
0
        private static void StartRegister()
        {
            if (AdminMenu.Meta.Count > 0)
            {
                return;
            }
            // 操作项定义
            var menuEventDic = new Dictionary <string, KeyValuePair <int, string> >
            {
                { "add", new KeyValuePair <int, string>(1, "添加") },
                { "edit", new KeyValuePair <int, string>(2, "修改") },
                { "del", new KeyValuePair <int, string>(3, "删除") },
                { "view", new KeyValuePair <int, string>(4, "查看") },
                { "viewlist", new KeyValuePair <int, string>(5, "查看列表") },
                { "import", new KeyValuePair <int, string>(6, "导入") },
                { "export", new KeyValuePair <int, string>(7, "导出") },
                { "filter", new KeyValuePair <int, string>(8, "搜索") },
                { "batch", new KeyValuePair <int, string>(9, "批量操作") },
                { "recycle", new KeyValuePair <int, string>(10, "回收站") },
                { "confirm", new KeyValuePair <int, string>(11, "确认") },
            };

            var asms = AppDomain.CurrentDomain.GetAssemblies();

            foreach (var asmItem in asms)
            {
                var types = asmItem.GetTypes().Where(e => e.Name.EndsWith("Controller")).ToList();
                if (types.Count == 0)
                {
                    continue;
                }

                foreach (var type in types)
                {
                    if (!type.Namespace.Contains(".AdminCP.Controller"))
                    {
                        continue;
                    }

                    // 控制器名称
                    var ctrName = type.Name.TrimEnd("Controller");

                    // 控制器展示名
                    var ctrDpName = type.GetDisplayName();

                    var ctrDes = type.GetDescription();

                    var url = ctrName;

                    var fAdminMenu = AdminMenu.Find(AdminMenu._.MenuKey == ctrName + "Sys" & AdminMenu._.Link == "#");
                    var nAdminMenu = new AdminMenu
                    {
                        Description = ctrDes,
                        IsHide      = 0,
                        Level       = 0,
                        Link        = "#",
                        Location    = "0,",
                        MenuKey     = ctrName + "Sys",
                        MenuName    = ctrDpName,
                        PId         = 0
                    };
                    var pAdminMenu = fAdminMenu ?? nAdminMenu;
                    if (pAdminMenu.Id > 0)
                    {
                        pAdminMenu.Description = nAdminMenu.Description;
                        pAdminMenu.MenuName    = nAdminMenu.MenuName;
                    }

                    // 控制器所有包含MyAuthorizeAttribute的方法
                    var acts = type.GetMethods()
                               .Where(w => w.IsDefined(typeof(MyAuthorizeAttribute)))
                               .ToList();

                    if (!acts.Any())
                    {
                        continue;
                    }

                    pAdminMenu.Save();

                    var eventKeyDic        = new Dictionary <string, int>();
                    var adminMenuEventList = new List <AdminMenuEvent>();

                    foreach (var method in acts)
                    {
                        var dn          = method.GetDisplayName();
                        var methodDes   = method.GetDescription();
                        var actName     = method.Name;
                        var myAuthorize = method.GetCustomAttribute <MyAuthorizeAttribute>();

                        var menuKey    = myAuthorize.GetValue("_menuKey").ToString();
                        var eventKey   = myAuthorize.GetValue("_eventKey").ToString();
                        var returnType = myAuthorize.GetValue("_returnType").ToString();

                        //保存菜单
                        if (eventKey == "viewlist" && returnType == "HTML")
                        {
                            var adminMenu = AdminMenu.Find(AdminMenu._.MenuKey == menuKey & AdminMenu._.Link == url + "/" + actName) ?? new AdminMenu
                            {
                                Description = methodDes,
                                IsHide      = 0,
                                Level       = pAdminMenu.Level + 1,
                                Link        = url + "/" + actName,
                                Location    = pAdminMenu.Location + ",",
                                MenuKey     = menuKey,
                                MenuName    = dn,
                                PId         = pAdminMenu.Id
                            };
                            adminMenu.Save();

                            eventKeyDic[menuKey] = adminMenu.Id;
                        }

                        // 保存操作
                        var adminmenuevent = AdminMenuEvent.Find(AdminMenuEvent._.MenuId == menuEventDic[eventKey].Key
                                                                 & AdminMenuEvent._.MenuKey == menuKey & AdminMenuEvent._.EventKey == eventKey)
                                             ?? new AdminMenuEvent
                        {
                            MenuKey   = menuKey,
                            MenuId    = menuEventDic[eventKey].Key,
                            EventKey  = eventKey,
                            EventName = menuEventDic[eventKey].Value
                        };
                        adminMenuEventList.Add(adminmenuevent);
                    }

                    adminMenuEventList.ForEach(f => f.MenuId = eventKeyDic.ContainsKey(f.MenuKey) ? eventKeyDic[f.MenuKey] : 0);
                    adminMenuEventList.Save();
                }
            }
        }
예제 #2
0
        public IActionResult EditAdminRole(IFormCollection fc)
        {
            string Id                    = fc["Id"];
            string RoleName              = fc["RoleName"];
            string RoleDescription       = fc["RoleDescription"];
            string IsSuperAdmin          = fc["IsSuperAdmin"];
            string NotAllowDel           = fc["NotAllowDel"];
            string OnlyEditMyselfArticle = fc["OnlyEditMyselfArticle"];
            string OnlyEditMyselfProduct = fc["OnlyEditMyselfProduct"];

            if (string.IsNullOrEmpty(OnlyEditMyselfArticle) || !Utils.IsInt(OnlyEditMyselfProduct))
            {
                OnlyEditMyselfProduct = "0";
            }
            if (string.IsNullOrEmpty(OnlyEditMyselfProduct) || !Utils.IsInt(OnlyEditMyselfProduct))
            {
                OnlyEditMyselfProduct = "0";
            }

            if (string.IsNullOrEmpty(Id))
            {
                tip.Message = "错误参数传递!";
                return(Json(tip));
            }

            if (string.IsNullOrEmpty(RoleName))
            {
                tip.Message = "管理组名称不能为空!";
                return(Json(tip));
            }

            AdminRoles entity = AdminRoles.Find(AdminRoles._.Id == int.Parse(Id));

            if (entity == null)
            {
                tip.Message = "系统找不到本记录!";
                return(Json(tip));
            }
            entity.RoleName              = RoleName;
            entity.RoleDescription       = RoleDescription;
            entity.IsSuperAdmin          = int.Parse(IsSuperAdmin);
            entity.NotAllowDel           = !string.IsNullOrEmpty(NotAllowDel) && NotAllowDel == "1" ? 1 : 0;
            entity.OnlyEditMyselfArticle = int.Parse(OnlyEditMyselfArticle);
            entity.OnlyEditMyselfProduct = int.Parse(OnlyEditMyselfProduct);

            //处理权限
            if (entity.IsSuperAdmin == 1)
            {
                entity.Powers = "";
                entity.Menus  = "";
            }
            else
            {
                //第一步,获取菜单
                string[] menuids = Request.Form["menuid"];
                //获取所有的菜单列表
                IList <AdminMenu>      alllist    = AdminMenu.GetListTree(0, -1, false, false);
                IList <AdminMenu>      list       = new List <AdminMenu>();
                IList <AdminMenuEvent> listevents = new List <AdminMenuEvent>();
                if (menuids != null && menuids.Length > 0)
                {
                    foreach (string s in menuids)
                    {
                        if (Utils.IsInt(s) && alllist.FirstOrDefault(v => v.Id == int.Parse(s)) != null)
                        {
                            AdminMenu tmp = alllist.FirstOrDefault(a => a.Id == int.Parse(s)).CloneEntity();
                            list.Add(tmp);
                            //处理详细权限  详细权限,每一行,则每一个菜单的详细权限,则同一个name
                            string[] eventids = Request.Form["EventKey_" + s];
                            if (eventids != null && eventids.Length > 0)
                            {
                                foreach (var item in eventids)
                                {
                                    if (Utils.IsInt(item))
                                    {
                                        AdminMenuEvent model = AdminMenuEvent.Find(AdminMenuEvent._.Id == int.Parse(item));
                                        if (model != null)
                                        {
                                            listevents.Add(model);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //序列化成json
                    if (list != null && list.Count > 0)
                    {
                        entity.Menus = JsonConvert.SerializeObject(list);
                    }
                    if (listevents != null && listevents.Count > 0)
                    {
                        entity.Powers = JsonConvert.SerializeObject(listevents);
                    }
                }
            }

            //处理文章和商品栏目权限
            string[] acpower = Request.Form["acpower"];
            string[] pcpower = Request.Form["pcpower"];

            List <int> acids = new List <int>();
            List <int> pcids = new List <int>();

            foreach (var item in acpower)
            {
                if (!string.IsNullOrEmpty(item) && Utils.IsInt(item))
                {
                    acids.Add(int.Parse(item));
                }
            }
            foreach (var item in pcpower)
            {
                if (!string.IsNullOrEmpty(item) && Utils.IsInt(item))
                {
                    pcids.Add(int.Parse(item));
                }
            }

            entity.AuthorizedArticleCagegory = JsonConvert.SerializeObject(acids);
            entity.AuthorizedCagegory        = JsonConvert.SerializeObject(pcids);

            entity.Update();
            tip.Status    = JsonTip.SUCCESS;
            tip.Message   = "编辑管理组详情成功";
            tip.ReturnUrl = "close";
            Core.Admin.WriteLogActions($"执行编辑管理组({entity.Id})详情;");
            return(Json(tip));
        }
예제 #3
0
        public IActionResult AddAdminRole(IFormCollection fc)
        {
            string RoleName        = fc["RoleName"];
            string RoleDescription = fc["RoleDescription"];
            string IsSuperAdmin    = fc["IsSuperAdmin"];
            string NotAllowDel     = fc["NotAllowDel"];

            if (string.IsNullOrEmpty(RoleName))
            {
                tip.Message = "管理组名称不能为空!";
                return(Json(tip));
            }

            AdminRoles entity = new AdminRoles();

            entity.RoleName        = RoleName;
            entity.RoleDescription = RoleDescription;
            entity.IsSuperAdmin    = int.Parse(IsSuperAdmin);
            entity.NotAllowDel     = !string.IsNullOrEmpty(NotAllowDel) && NotAllowDel == "1" ? 1 : 0;

            //处理权限
            if (entity.IsSuperAdmin == 1)
            {
                entity.Powers = "";
                entity.Menus  = "";
            }
            else
            {
                //第一步,获取菜单
                string[] menuids = Request.Form["menuid"];
                //获取所有的菜单列表
                IList <AdminMenu>      alllist    = AdminMenu.GetListTree(0, -1, false, false);
                IList <AdminMenu>      list       = new List <AdminMenu>();
                IList <AdminMenuEvent> listevents = new List <AdminMenuEvent>();
                if (menuids != null && menuids.Length > 0)
                {
                    foreach (string s in menuids)
                    {
                        if (Utils.IsInt(s) && alllist.FirstOrDefault(v => v.Id == int.Parse(s)) != null)
                        {
                            AdminMenu tmp = alllist.FirstOrDefault(a => a.Id == int.Parse(s)).CloneEntity();
                            list.Add(tmp);
                            //处理详细权限  详细权限,每一行,则每一个菜单的详细权限,则同一个name
                            string[] eventids = Request.Form["EventKey_" + s];
                            if (eventids != null && eventids.Length > 0)
                            {
                                foreach (var item in eventids)
                                {
                                    if (Utils.IsInt(item))
                                    {
                                        AdminMenuEvent model = AdminMenuEvent.Find(AdminMenuEvent._.Id == int.Parse(item));
                                        if (model != null)
                                        {
                                            listevents.Add(model);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    //序列化成json
                    if (list != null && list.Count > 0)
                    {
                        entity.Menus = JsonConvert.SerializeObject(list);
                    }
                    if (listevents != null && listevents.Count > 0)
                    {
                        entity.Powers = JsonConvert.SerializeObject(listevents);
                    }
                }
            }

            entity.Insert();
            tip.Status    = JsonTip.SUCCESS;
            tip.Message   = "添加管理组成功";
            tip.ReturnUrl = "close";
            Core.Admin.WriteLogActions($"添加新管理组({entity.Id});");

            return(Json(tip));
        }