public IActionResult AddAdminMenu(AdminMenu model) { if (!ModelState.IsValid) { string messages = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)); tip.Message = messages; return(Json(tip)); } //判断 if (AdminMenu.FindCount(AdminMenu._.MenuKey == model.MenuKey, null, null, 0, 0) > 0) { tip.Message = "菜单KEY已经存在,请填写其他的!"; } IList <TargetEvent> listevent = TargetEvent.FindAll(null, TargetEvent._.Rank.Asc(), null, 0, 0); string[] eventkeys = Request.Form["eventkey"]; List <AdminMenuEvent> listme = new List <AdminMenuEvent>(); if (eventkeys != null && eventkeys.Length > 0) { foreach (string s in eventkeys) { if (Utils.IsInt(s)) { TargetEvent te = listevent.FirstOrDefault(t => t.Id == int.Parse(s)); if (te != null) { AdminMenuEvent tmp = new AdminMenuEvent(); tmp.EventId = te.Id; tmp.EventKey = te.EventKey; tmp.EventName = te.EventName; tmp.MenuKey = model.MenuKey; listme.Add(tmp); } } } } model.Insert(); if (listme != null && listme.Count > 0) { listme.ForEach(s => { s.MenuId = model.Id; }); listme.Insert(); } tip.Status = JsonTip.SUCCESS; tip.Message = "添加后台菜单成功"; tip.ReturnUrl = "close"; return(Json(tip)); }
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)); }
public IActionResult EditAdminMenu(AdminMenu model) { if (!ModelState.IsValid) { string messages = string.Join("; ", ModelState.Values .SelectMany(x => x.Errors) .Select(x => x.ErrorMessage)); tip.Message = messages; return(Json(tip)); } if (model.Id <= 0) { tip.Message = "错误参数传递!"; return(Json(tip)); } AdminMenu entity = AdminMenu.Find(AdminMenu._.Id == model.Id); if (entity == null) { tip.Message = "系统找不到本记录!"; return(Json(tip)); } //赋值 entity.MenuName = model.MenuName; //如果key修改了。判断是否有存在 if (entity.MenuKey != model.MenuKey) { if (AdminMenu.FindCount(AdminMenu._.MenuKey == model.MenuKey & AdminMenu._.Id != entity.Id, null, null, 0, 0) > 0) { tip.Message = "您修改的菜单KEY已经存在,请填写其他的!"; return(Json(tip)); } entity.MenuKey = model.MenuKey; } entity.Link = model.Link; entity.IsHide = model.IsHide; entity.ClassName = model.ClassName; entity.Rank = model.Rank; if (entity.PId != model.PId) { //重新获取level和location entity.Level = AdminMenu.GetNewLevel(model.PId); entity.Location = AdminMenu.GetNewLocation(model.PId); } entity.PId = model.PId; entity.Update(); //处理权限 IList <AdminMenuEvent> oldevent = AdminMenuEvent.FindAll(AdminMenuEvent._.MenuId == entity.Id, null, null, 0, 0); //所有的权限 IList <TargetEvent> listevent = TargetEvent.FindAll(TargetEvent._.Id > 0, TargetEvent._.Rank.Asc(), null, 0, 0); //新的权限 string[] eventkeys = Request.Form["eventkey"]; List <AdminMenuEvent> listme = new List <AdminMenuEvent>(); if (eventkeys != null && eventkeys.Length > 0) { foreach (string s in eventkeys) { if (Utils.IsInt(s)) { TargetEvent te = listevent.FirstOrDefault(t => t.Id == int.Parse(s)); if (te != null) { AdminMenuEvent tmp = new AdminMenuEvent(); tmp.EventId = te.Id; tmp.EventKey = te.EventKey; tmp.EventName = te.EventName; tmp.MenuKey = model.MenuKey; tmp.MenuId = entity.Id; listme.Add(tmp); } } } } //判断是否在旧的里面 listme.ForEach(s => { var oe = oldevent.FirstOrDefault(o => o.EventId == s.EventId); //如果是旧列表存在,则删掉旧列表里面的,然后更新自己 if (oe != null) { s.Id = oe.Id; s.Update(); oldevent.Remove(oe); } else { //如果不存在,则是新增的。直接新增处理 s.Insert(); } }); //如果还有,则删除旧列表里面的 if (oldevent != null && oldevent.Count > 0) { oldevent.Delete(); } tip.Status = JsonTip.SUCCESS; tip.Message = "编辑后台菜单成功"; tip.ReturnUrl = "close"; return(Json(tip)); }
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(); } } }
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)); }