public ActionResult Edit(Models.SysRolePower.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var power = (from p in db.Table <Sys.Entity.tbSysRolePower>()
                             where p.tbSysRole.Id == vm.RoleId
                             select p).ToList();
                foreach (var a in power)
                {
                    a.IsDeleted = true;
                }

                if (string.IsNullOrEmpty(vm.Power) == false)
                {
                    var menuList = vm.Power.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.ConvertToInt());

                    foreach (var menu in menuList.Where(d => d != 0))
                    {
                        var temp = new Sys.Entity.tbSysRolePower();
                        temp.tbSysMenu = db.Set <Sys.Entity.tbSysMenu>().Find(menu);
                        temp.tbSysRole = db.Set <Sys.Entity.tbSysRole>().Find(vm.RoleId);
                        db.Set <Sys.Entity.tbSysRolePower>().Add(temp);
                    }
                }

                if (db.SaveChanges() > 0)
                {
                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加角色权限");
                    System.Web.HttpContext.Current.Cache["Power"] = SysRolePowerController.GetPower();
                }

                return(Code.MvcHelper.Post(null, "", "保存成功!"));
            }
        }
Example #2
0
        public ActionResult SetDisable(int RoleId, int MenuId, bool Status)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var menuIds = GetChildrenMenuId(MenuId);

                if (Status)//授权
                {
                    //如果菜单不是一级菜单,激活时同时激活它的一级菜单
                    if (db.Set <Entity.tbSysMenu>().Find(MenuId).tbMenuParent != null)
                    {
                        var id = db.Table <Entity.tbSysMenu>()
                                 .Include(d => d.tbMenuParent)
                                 .Where(d => d.Id == MenuId).FirstOrDefault().tbMenuParent.Id;
                        menuIds.Add(id);
                    }

                    var tb = db.Table <Sys.Entity.tbSysRolePower>()
                             .Include(d => d.tbSysMenu)
                             .Where(d => d.tbSysRole.Id == RoleId).ToList();

                    var tempList = new List <Sys.Entity.tbSysRolePower>();
                    foreach (var v in menuIds)
                    {
                        if (tb.Where(d => d.tbSysMenu.Id == v).Count() == 0)
                        {
                            var temp = new Sys.Entity.tbSysRolePower()
                            {
                                tbSysMenu = db.Set <Sys.Entity.tbSysMenu>().Find(v),
                                tbSysRole = db.Set <Sys.Entity.tbSysRole>().Find(RoleId)
                            };
                            tempList.Add(temp);
                        }
                    }
                    db.Set <Sys.Entity.tbSysRolePower>().AddRange(tempList);
                }
                else//取消授权
                {
                    var tb = db.Table <Sys.Entity.tbSysRolePower>()
                             .Where(d => menuIds.Contains(d.tbSysMenu.Id) && d.tbSysRole.Id == RoleId).ToList();
                    foreach (var v in tb)
                    {
                        v.IsDeleted = true;
                    }
                }

                if (db.SaveChanges() > 0)
                {
                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了角色权限");
                    System.Web.HttpContext.Current.Cache["Power"] = SysRolePowerController.GetPower();
                }

                return(Code.MvcHelper.Post(null, Url.Action("MenuRoleList", new { MenuId = MenuId })));
            }
        }