Exemplo n.º 1
0
        /// <summary>
        /// 根据用户获取功能菜单
        /// </summary>
        /// <param name="sysUserDto"></param>
        /// <returns></returns>
        private async Task <List <SysMenuDto> > GetMenusAsync(SysUserDto sysUserDto)
        {
            //查询出系统所有菜单
            List <SysMenuDto> result = new List <SysMenuDto>();
            var allMenus             = await _sysMenuRepository.GetAllListAsync();

            allMenus = allMenus.OrderBy(it => it.SerialNumber).ToList();

            if (sysUserDto.LoginName == _webAppOptions.AdminLoginName) //超级管理员
            {
                return(Mapper.Map <List <SysMenuDto> >(allMenus));
            }

            //查询当前用户角色
            var userRoleIds = await _sysUserRepository.FindUserRoleAsync(sysUserDto.Id);

            if (userRoleIds == null)
            {
                return(result);
            }

            //根据角色查询角色拥有的菜单ID
            List <Guid> menuIds = new List <Guid>();

            foreach (var roleId in userRoleIds)
            {
                menuIds = menuIds.Union(await _sysRoleRepository.GetMenuListByRoleAsync(roleId)).ToList();
            }
            allMenus = allMenus.Where(it => menuIds.Contains(it.Id)).OrderBy(it => it.SerialNumber).ToList();
            return(Mapper.Map <List <SysMenuDto> >(allMenus));
        }