Beispiel #1
0
        /// <summary>
        /// 保存权限方法
        /// </summary>
        /// <param name="saveRoleAssigView"></param>
        /// <returns></returns>
        public async Task <bool> SaveRoleAssig(SaveRoleAssigViewModel saveRoleAssigView)
        {
            List <Guid> BtnIdlist = new List <Guid>();

            BtnIdlist.AddRange(JsonConvert.DeserializeObject <List <Guid> >(saveRoleAssigView.BtnIds));//获取选中的按钮ID
            List <RoleAssigMenuViewModel> roleAssigMenuViews = new List <RoleAssigMenuViewModel>();

            roleAssigMenuViews.AddRange(JsonConvert.DeserializeObject <List <RoleAssigMenuViewModel> >(saveRoleAssigView.menuIds));//获取选中的按钮ID
            try
            {
                var sysButtonlist = await _buttonRepositoty.GetAllListAsync(x => BtnIdlist.Contains(x.Id));//根据按钮ID获取所有的按钮及对应的菜单

                var DeleteroleRight = await _roleRightAssigRepository.GetAllListAsync(x => x.RoleId == saveRoleAssigView.RoleId);

                List <SysRoleRight> SysRoleRightList = new List <SysRoleRight>();
                var menuIds = sysButtonlist.Select(x => new { x.MenuId }).Distinct().ToList(); //根据按钮ID获取所有的按钮及对应的菜单,并且根据菜单ID去重
                foreach (var item in menuIds)                                                  //将去重的菜单ID添加到角色权限List容器中
                {
                    SysRoleRightList.Add(new SysRoleRight {
                        MenuId      = item.MenuId,
                        RoleId      = saveRoleAssigView.RoleId,
                        CreatedId   = saveRoleAssigView.CreatedId,
                        CreatedName = saveRoleAssigView.CreatedName,
                    });
                }
                var ReadMenulist = SysRoleRightList.ToSelectList(x => x.MenuId);
                var MenuList     = roleAssigMenuViews.Where(x => !ReadMenulist.Contains(x.Id)).ToList(); //获取到还未选中的菜单
                SysRoleRightList.AddRange(MenuList.Select(x => new SysRoleRight                          //将未选中的菜单添加到角色权限对象中
                {
                    MenuId      = x.Id,
                    RoleId      = saveRoleAssigView.RoleId,
                    CreatedId   = saveRoleAssigView.CreatedId,
                    CreatedName = saveRoleAssigView.CreatedName,
                }).ToList());
                SysRoleRightList.ForEach(item =>//根据菜单ID获取菜单下面所有的按钮
                {
                    var BtnList    = sysButtonlist.Where(x => x.MenuId == item.MenuId).ToList();
                    item.ButtonIds = string.Join(",", BtnList.Select(x => x.Id));
                });

                _unitofWork.BeginTransaction();
                await _roleRightAssigRepository.Delete(DeleteroleRight);

                await _roleRightAssigRepository.InsertAsync(SysRoleRightList);

                _unitofWork.Commit();
                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception($"Commit 异常:{ex.InnerException}/r{ ex.Message}");
            }
        }
        public async Task <MessageModel <string> > Save([FromBody] SaveRoleAssigViewModel saveRoleAssigView)
        {
            var data = new MessageModel <string>();

            data.success = await _roleAssigServer.SaveRoleAssig(saveRoleAssigView);

            if (data.success)
            {
                data.msg = "权限修改成功";
                return(data);
            }
            return(data);
        }