Exemple #1
0
        /// <summary>
        ///  更新角色的权限
        /// </summary>
        /// <param name="role">角色</param>
        /// <param name="adminIds">包含的菜单id</param>
        /// <returns></returns>
        public int UpdatePermission(SysRole role, List <int> menuIds)
        {
            int result = -1;

            result = _roleRepository.BeginTransaction(() =>
            {
                result = _roleRepository.Update(role,
                                                m => m.LastUpdateBy, m => m.LastUpdateByName, m => m.LastUpdateDate);

                var menuRoles = _menuRoleRepository.Table.Where(m => m.RoleId == role.Id).ToList();

                #region  除
                //没有设置 全部删除
                if (menuIds == null || menuIds.Count() <= 0)
                {
                    _menuRoleRepository.Delete(menuRoles);
                }
                else
                {
                    List <SysMenuRole> remveMenuRoles = new List <SysMenuRole>();
                    foreach (var menuRole in menuRoles)
                    {
                        //没有包含,删除数据库
                        if (!menuIds.Any(id => id == menuRole.MenuId))
                        {
                            remveMenuRoles.Add(menuRole);
                        }
                    }
                    _menuRoleRepository.Delete(remveMenuRoles);
                }
                #endregion

                #region 插入
                if (menuIds != null && menuIds.Count() > 0)
                {
                    foreach (var id in menuIds)
                    {
                        if (!menuRoles.Any(m => m.MenuId == id))
                        {
                            //不存在新增
                            var mr = new SysMenuRole()
                            {
                                MenuId           = id,
                                RoleId           = role.Id,
                                LastUpdateBy     = role.LastUpdateBy,
                                LastUpdateByName = role.LastUpdateByName,
                                LastUpdateDate   = role.LastUpdateDate,
                                CreateBy         = role.LastUpdateBy,
                                CreatebyName     = role.LastUpdateByName,
                                CreateDate       = role.LastUpdateDate
                            };
                            role.MenuRoles.Add(mr);
                        }
                    }
                    result = _menuRoleRepository.Insert(role.MenuRoles);
                }
                #endregion
            });
            return(result);
        }
        public virtual IHttpActionResult Disable(SysMenuRoleDisableRequest request)
        {
            var entity = new SysMenuRole
            {
                Id = request.Id,
            };
            var result = _sysMenuRoleService.Disable(request.Id);

            if (result > 0)
            {
                return(Succeed("禁用成功"));
            }
            else
            {
                return(Fail("禁用失败"));
            }
        }
        public virtual IHttpActionResult Add(SysMenuRoleAddRequest request)
        {
            var entity = new SysMenuRole
            {
            };
            var result = _sysMenuRoleService.Add(entity);

            if (result > 0)
            {
                return(Succeed(new SysMenuRoleAddResponse
                {
                    Id = entity.Id
                }, "新增成功"));
            }
            else
            {
                return(Fail("新增失败"));
            }
        }