Example #1
0
        public ActionResult Update(int privilegeId = 0)
        {
            int           menuId        = 0;
            MenuPrivilege menuPrivilege = db.MenuPrivilege.Where(m => m.privilegeId == privilegeId).FirstOrDefault();

            if (menuPrivilege != null)
            {
                menuId = menuPrivilege.menuId;
            }
            List <SelectListItem> select = new List <SelectListItem>();

            select.Add(new SelectListItem {
                Text = "请选择菜单", Value = "0"
            });
            List <Menu> menues = db.Menu.Where(m => m.parentMenuId == 0).ToList();

            foreach (var menu in menues)
            {
                select.Add(new SelectListItem {
                    Text = menu.menuName, Value = menu.menuId.ToString()
                });
            }
            ViewData["select"] = new SelectList(select, "Value", "Text", menuId);
            return(View());
        }
Example #2
0
        public JsonResult Update(UpdatePrivilegeModel model)
        {
            JsonResult json = new JsonResult();

            if (string.IsNullOrEmpty(model.privilegeName))
            {
                return(Json(new { success = false, msg = "权限名称不为空" }, "text/json"));
            }
            if (model.menuId == 0)
            {
                return(Json(new { success = false, msg = "请选择相应的菜单" }, "text/json"));
            }
            List <Privilege> mPrivilege = db.Privilege.Where(m => m.privilegeName == model.privilegeName).ToList();

            if (mPrivilege != null & mPrivilege.Count > 1)
            {
                return(Json(new { success = false, msg = "该权限已经存在,请更换权限名称" }, "text/json"));
            }
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    Privilege privilege = db.Privilege.FirstOrDefault(m => m.privilegeId == model.privilegeId);
                    if (privilege != null)
                    {
                        privilege.privilegeName = model.privilegeName;
                        privilege.privilegeNote = model.privilegeNote;
                    }
                    db.Entry(privilege).State = EntityState.Modified;

                    MenuPrivilege menuPrivilege =
                        db.MenuPrivilege.Where(m => m.privilegeId == model.privilegeId).FirstOrDefault();
                    if (menuPrivilege != null)
                    {
                        menuPrivilege.menuId      = model.menuId;
                        menuPrivilege.privilegeId = model.privilegeId;
                    }
                    db.Entry(menuPrivilege).State = EntityState.Modified;
                    if (db.SaveChanges() > 0)
                    {
                        json.Data = new { success = true, msg = "修改成功!" };
                    }
                    else
                    {
                        json.Data = new { success = false, msg = "修改失败!" };
                    }
                }
                catch (DbEntityValidationException ex)
                {
                    throw;
                }
                scope.Complete();
            }
            return(json);
        }
Example #3
0
        public JsonResult Add(AddPrivilegeModel model)
        {
            JsonResult json = new JsonResult();

            if (string.IsNullOrEmpty(model.privilegeName))
            {
                return(Json(new { success = false, msg = "权限名称不为空" }, "text/json"));
            }
            if (model.menuId == 0)
            {
                return(Json(new { success = false, msg = "请选择相应的菜单" }, "text/json"));
            }
            Privilege mPrivilege = db.Privilege.Where(m => m.privilegeName == model.privilegeName).FirstOrDefault();

            if (mPrivilege != null)
            {
                return(Json(new { success = false, msg = "该权限已经存在,请更换权限名称" }, "text/json"));
            }
            using (TransactionScope scope = new TransactionScope())
            {
                try
                {
                    Privilege privilege = new Privilege
                    {
                        privilegeName = model.privilegeName,
                        privilegeNote = model.privilegeNote
                    };
                    db.Privilege.Add(privilege);
                    MenuPrivilege menuPrivilege = new MenuPrivilege
                    {
                        menuId      = model.menuId,
                        privilegeId = model.privilegeId
                    };
                    db.MenuPrivilege.Add(menuPrivilege);
                    if (db.SaveChanges() > 0)
                    {
                        json.Data = new { success = true, msg = "添加成功!" };
                    }
                    else
                    {
                        json.Data = new { success = false, msg = "添加失败!" };
                    }
                }
                catch (DbEntityValidationException ex)
                {
                    throw;
                }
                scope.Complete();
            }
            return(json);
        }
        public MenuPrivilege GetPrivilege(string connectionString, string pageName)
        {
            MenuPrivilege priv = null;

            using (var ctx = new ArtileriDataContext(connectionString))
            {
                var currentMenu = ctx.Menus.SingleOrDefault(menu => menu.NavigationTo == pageName);
                if (currentMenu != null)
                {
                    int roleID   = ctx.Employees.SingleOrDefault(emp => emp.UserName == CurrentUserName).RoleID;
                    var roleMenu = ctx.RoleMenus.SingleOrDefault(rm => rm.RoleID == roleID && rm.MenuID == currentMenu.ID);
                    if (roleMenu != null)
                    {
                        priv = new MenuPrivilege(pageName, roleMenu);
                    }
                }
            }
            return(priv);
        }
Example #5
0
        public bool SavePrivilege(MenuPermission priv)
        {
            bool result = false;

            using (var context = new AdminAppEntities())
            {
                var           privilege = context.MenuPrivileges.ToList();
                MenuPrivilege menuPriv  = new MenuPrivilege();
                menuPriv = privilege.Where(x => x.RoleID == priv.RoleID && x.MenuID == priv.MenuID).FirstOrDefault();
                if (menuPriv != null)
                {
                    context.MenuPrivileges.Remove(menuPriv);
                    context.SaveChanges();
                }
                MenuPrivilege menu = new MenuPrivilege
                {
                    MenuID    = priv.MenuID,
                    RoleID    = priv.RoleID,
                    CanCreate = priv.CanCreate,
                    CanEdit   = priv.CanEdit,
                    CanView   = priv.CanView,
                    CanDelete = priv.CanDelete
                };
                context.MenuPrivileges.Add(menu);
                context.SaveChanges();
                if (menu.PrivilegeID > 0)
                {
                    result = true;
                }
                else
                {
                    result = false;
                }
            }
            return(result);
        }