public IHttpActionResult DeleteRoleMenu(RoleMenuViewModel roleMenu)
        {
            var role = db.Roles.Find(roleMenu.RoleId);

            if (role == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "不存在的RoleId!"));
            }

            var menus = db.Menus.Where(item => item.Id == roleMenu.MenuId || item.ParentId == roleMenu.MenuId);

            foreach (var item in menus)
            {
                role.Menus.Remove(item);
                //role.Menus.Remove(db.Menus.Find(item.ParentId));
            }

            db.SaveChanges();

            //寫入AccessLog
            MPAccessLog.WriteEntry(User.Identity.Name, AccessAction.Delete, "RoleMenu", JsonConvert.SerializeObject(new { role.Id, role.Name, Menus = menus.Select(m => m.Id + m.Text).ToArray() }));

            return(Ok());
        }
        public IHttpActionResult BindRoleMenu(RoleMenuViewModel roleMenuViewModel)
        {
            var ret = _roleService.BindRoleMenu(roleMenuViewModel, WorkUser);

            return(Ok(ret));
        }
Exemple #3
0
        /// <summary>
        /// 角色绑定可查看菜单
        /// </summary>
        /// <param name="roleMenuViewModel">角色菜单信息</param>
        /// <param name="workUser">当前操作人</param>
        /// <returns></returns>
        public ReturnValueModel BindRoleMenu(RoleMenuViewModel roleMenuViewModel, WorkUser workUser)
        {
            ReturnValueModel rvm = new ReturnValueModel();

            using (var tran = _rep.Database.BeginTransaction())
            {
                try
                {
                    var roleInCom = _rep.FirstOrDefault <RoleInfo>(s => s.Id == roleMenuViewModel.RoleId);
                    if (roleInCom != null)
                    {
                        var urList = _rep.Where <RoleMenu>(s => s.RoleId == roleMenuViewModel.RoleId).ToList();
                        foreach (var item in urList)
                        {
                            _rep.Delete(item);
                            _rep.SaveChanges();
                        }
                        var           rms       = _rep.Where <MenuInfo>(s => s.IsDeleted != 1).ToList();
                        List <string> pMenusIds = new List <string>();
                        //var gEmpty = Guid.Empty.ToString();
                        GetParentMenuIds(pMenusIds, roleMenuViewModel.MenuIds, rms);
                        //List<string> rootMenus = new List<string>();
                        roleMenuViewModel.MenuIds.AddRange(pMenusIds);
                        roleMenuViewModel.MenuIds = roleMenuViewModel.MenuIds.Distinct().ToList();
                        foreach (var menuId in roleMenuViewModel.MenuIds)
                        {
                            //var rm = rms.FirstOrDefault<MenuInfo>(s => s.Id == menuId);
                            //rootMenus.Add(rm.ParentId);
                            var ur = new RoleMenu
                            {
                                Id          = Guid.NewGuid().ToString(),
                                MenuId      = menuId,
                                RoleId      = roleMenuViewModel.RoleId,
                                CompanyCode = roleInCom.CompanyCode,
                                CreateTime  = DateTime.Now,
                                CreateUser  = workUser.User.Id
                            };
                            _rep.Insert(ur);
                        }
                        _rep.SaveChanges();
                        rvm.Success = true;
                        rvm.Msg     = "";
                        rvm.Result  = new
                        {
                            roleMenuViewModel.MenuIds
                        };
                        tran.Commit();
                    }
                    else
                    {
                        rvm.Success = false;
                        rvm.Msg     = "there was no role in table";
                        rvm.Result  = new
                        {
                            roleId = roleMenuViewModel.RoleId
                        };
                    }
                }
                catch (Exception e)
                {
                    tran.Rollback();
                    rvm.Success = false;
                    rvm.Msg     = e.Message;
                    rvm.Result  = new
                    {
                        e
                    };
                }
            }
            return(rvm);
        }