public UserInfoViewModel GetUserInfo() { string UserId = identityHelper.GetUserId(User); UnitOfWork unitOfWork = new UnitOfWork(factory); SqlParameter parUser = new SqlParameter { ParameterName = "@UserId", IsNullable = false, Direction = ParameterDirection.Input, DbType = DbType.String, Value = UserId }; UserInfoViewModel userInfoViewModel = unitOfWork.SQLQuery <UserInfoViewModel>("exec UserInfo @UserId", parUser).FirstOrDefault(); //Task<List<UserRightDTO>> tRights = GetUserRights(UserId); //Task<UserInfoViewModel> tModel = GetUserInfoViewModel(UserId); //await Task.WhenAll(tModel, tRights); //UserInfoViewModel userInfoViewModel = tModel.Result; //userInfoViewModel.UserRights = tRights.Result; //return userInfoViewModel; //i don't know why but they need another parameter SqlParameter parUser1 = new SqlParameter { ParameterName = "@UserId", IsNullable = false, Direction = ParameterDirection.Input, DbType = DbType.String, Value = UserId }; IEnumerable <UserRightOfficeDTO> rightsString = unitOfWork.SQLQuery <UserRightOfficeDTO>("exec UserRights @UserId", parUser1); List <UserRightDTO> dtos = new List <UserRightDTO>(); try { if (rightsString != null) { if (rightsString.Count() != 0) { var result = rightsString.Select(r => new OfficeSecurity { Id = r.OfficeId.GetValueOrDefault(), ShortName = r.OfficeShortName }).Distinct().ToList(); if (result != null) { foreach (var item in result) { UserRightDTO dto = new UserRightDTO(); dto.OfficeId = item.Id; dto.OfficeName = item.ShortName; List <ControllerBoxDTO> boxesList = new List <ControllerBoxDTO>(); var bList = rightsString .Where(d => d.OfficeId == item.Id) .Select(d => new ControllerBoxDTO { Id = d.BoxId.GetValueOrDefault(), Name = d.BoxName }) .Distinct() .ToList(); if (bList != null) { foreach (var box in bList) { ControllerBoxDTO boxDTO = new ControllerBoxDTO(); boxDTO.Name = box.Name; boxDTO.Id = box.Id; List <ControllerDTO> controllerList = new List <ControllerDTO>(); var cList = rightsString.Where(r => r.OfficeId == item.Id && r.BoxId == box.Id) .Select(r => new ControllerDTO { Id = r.ControllerId.GetValueOrDefault(), ControllerName = r.ControllerName }) .Distinct().ToList(); if (cList != null) { foreach (var c in cList) { ControllerDTO cDto = new ControllerDTO(); cDto.ControllerName = c.ControllerName; cDto.Id = c.Id; List <ActionDTO> actionList = new List <ActionDTO>(); var aList = rightsString.Where(r => r.OfficeId == item.Id && r.ControllerId == c.Id) .Select(r => new ActionDTO { Id = r.ActionId.GetValueOrDefault(), ActionName = r.ActionName, ActionLongName = r.ActionLongName }) .Distinct().ToList(); if (aList != null) { foreach (var a in aList) { ActionDTO aDto = new ActionDTO(); aDto.Id = a.Id; aDto.ActionLongName = a.ActionLongName == null ? "" : a.ActionLongName; aDto.ActionName = a.ActionName == null ? "" : a.ActionName; actionList.Add(aDto); } } cDto.Actions = actionList; controllerList.Add(cDto); } } boxDTO.Controllers = controllerList; boxesList.Add(boxDTO); } } dto.Boxes = boxesList; dtos.Add(dto); } } } } } catch (Exception e) { string m = e.Message; } userInfoViewModel.UserRights = dtos.OrderBy(d => d.OfficeName); return(userInfoViewModel); }
private async Task <List <UserRightDTO> > GetUserRights(string userId) { UnitOfWork unitOfWork = new UnitOfWork(factory); SqlParameter parUser1 = new SqlParameter { ParameterName = "@UserId", IsNullable = false, Direction = ParameterDirection.Input, DbType = DbType.String, Value = userId }; IEnumerable <UserRightOfficeDTO> rightsString = unitOfWork.SQLQuery <UserRightOfficeDTO>("exec UserRights @UserId", parUser1); List <UserRightDTO> dtos = new List <UserRightDTO>(); try { if (rightsString != null) { if (rightsString.Count() != 0) { var result = rightsString.Select(r => new OfficeSecurity { Id = r.OfficeId.GetValueOrDefault(), ShortName = r.OfficeShortName }).Distinct().ToList(); if (result != null) { foreach (var item in result) { UserRightDTO dto = new UserRightDTO(); dto.OfficeId = item.Id; dto.OfficeName = item.ShortName; List <ControllerBoxDTO> boxesList = new List <ControllerBoxDTO>(); var bList = rightsString .Where(d => d.OfficeId == item.Id) .Select(d => new ControllerBoxDTO { Id = d.BoxId.GetValueOrDefault(), Name = d.BoxName }) .Distinct() .ToList(); if (bList != null) { foreach (var box in bList) { ControllerBoxDTO boxDTO = new ControllerBoxDTO(); boxDTO.Name = box.Name; boxDTO.Id = box.Id; List <ControllerDTO> controllerList = new List <ControllerDTO>(); var cList = rightsString.Where(r => r.OfficeId == item.Id && r.BoxId == box.Id) .Select(r => new ControllerDTO { Id = r.ControllerId.GetValueOrDefault(), ControllerName = r.ControllerName }) .Distinct().ToList(); if (cList != null) { foreach (var c in cList) { ControllerDTO cDto = new ControllerDTO(); cDto.ControllerName = c.ControllerName; cDto.Id = c.Id; List <ActionDTO> actionList = new List <ActionDTO>(); var aList = rightsString.Where(r => r.OfficeId == item.Id && r.ControllerId == c.Id) .Select(r => new ActionDTO { Id = r.ActionId.GetValueOrDefault(), ActionName = r.ActionName, ActionLongName = r.ActionLongName }) .Distinct().ToList(); if (aList != null) { foreach (var a in aList) { ActionDTO aDto = new ActionDTO(); aDto.Id = a.Id; aDto.ActionLongName = a.ActionLongName == null ? "" : a.ActionLongName; aDto.ActionName = a.ActionName == null ? "" : a.ActionName; actionList.Add(aDto); } } cDto.Actions = actionList; controllerList.Add(cDto); } } boxDTO.Controllers = controllerList; boxesList.Add(boxDTO); } } dto.Boxes = boxesList; dtos.Add(dto); } } } } } catch (Exception e) { string m = e.Message; } return(dtos); }
public List <UserRightDTO> GetUserRightsDTO() { List <UserRightDTO> dtos = new List <UserRightDTO>(); try { if (UserRights != null) { if (UserRights.Count != 0) { var result = UserRights.Select(r => r.OfficeSecurity).Distinct().ToList(); if (result != null) { foreach (var item in result) { UserRightDTO dto = new UserRightDTO(); dto.OfficeId = item.Id; dto.OfficeName = item.ShortName; List <ControllerBoxDTO> boxesList = new List <ControllerBoxDTO>(); var bList = UserRights .Where(d => d.OfficeId == item.Id) .Select(d => d.Action.Controller.ControllerBox) .Distinct() .ToList(); if (bList != null) { foreach (var box in bList) { ControllerBoxDTO boxDTO = new ControllerBoxDTO(); boxDTO.Name = box.Name; boxDTO.Id = box.Id; List <ControllerDTO> controllerList = new List <ControllerDTO>(); var cList = UserRights.Where(r => r.OfficeId == item.Id && r.Action.Controller.BoxId == box.Id).Select(r => r.Action.Controller).Distinct().ToList(); if (cList != null) { foreach (var c in cList) { ControllerDTO cDto = new ControllerDTO(); cDto.ControllerName = c.Name; cDto.Id = c.Id; List <ActionDTO> actionList = new List <ActionDTO>(); var aList = UserRights.Where(r => r.OfficeId == item.Id && r.Action.ControllerId == c.Id).Select(r => r.Action).Distinct().ToList(); if (aList != null) { foreach (var a in aList) { ActionDTO aDto = new ActionDTO(); aDto.Id = a.Id; aDto.ActionLongName = a.LongName == null ? "" : a.LongName; aDto.ActionName = a.Name == null ? "" : a.Name; actionList.Add(aDto); } } cDto.Actions = actionList; controllerList.Add(cDto); } } boxDTO.Controllers = controllerList; boxesList.Add(boxDTO); } } dto.Boxes = boxesList; dtos.Add(dto); } } } } } catch (Exception e) { string m = e.Message; } return(dtos); }