private Expression <Func <AmigoTenantTRoleDTO, bool> > GetQueryFilter(AmigoTenantTRoleSearchRequest search) { Expression <Func <AmigoTenantTRoleDTO, bool> > queryFilter = p => p.RowStatus; if (search.AmigoTenantTRoleId > 0) { queryFilter = queryFilter.And(p => p.AmigoTenantTRoleId == search.AmigoTenantTRoleId); } if (!string.IsNullOrEmpty(search.Code)) { queryFilter = queryFilter.And(p => p.Code.Contains(search.Code)); } if (!string.IsNullOrEmpty(search.Name)) { queryFilter = queryFilter.And(p => p.Name.Contains(search.Name)); } if (search.IsAdmin != null) { queryFilter = queryFilter.And(p => p.IsAdmin == search.IsAdmin); } return(queryFilter); }
public async Task SearchAmigoTenantTRoleByCriteriaAsync() { var bus = A.Fake <IBus>(); var mapper = A.Fake <IMapper>(); var repo = A.Fake <IQueryDataAccess <AmigoTenantTRoleDTO> >(); var repoBasic = A.Fake <IQueryDataAccess <AmigoTenantTRoleBasicDTO> >(); // var appService = new AmigoTenantTRoleApplicationService(bus, repo, repoBasic, mapper); var page = 1; var pageSize = 10; var request = new AmigoTenantTRoleSearchRequest() { Name = "test", Page = page, PageSize = pageSize }; A.CallTo(() => repo.ListPagedAsync(null, 0, 0, null, null)).WithAnyArguments().Returns(Task.FromResult(new Query.Common.PagedList <AmigoTenantTRoleDTO>())); //Act //var resp = await appService.SearchAmigoTenantTRoleByCriteriaAsync(request); //Assert //Assert.NotNull(resp); A.CallTo(() => repo.ListPagedAsync(null, 0, 0, null, null)).WithAnyArguments().MustHaveHappened(Repeated.NoMoreThan.Once); }
public async Task <ResponseDTO <PagedList <AmigoTenantTRoleBasicDTO> > > SearchById( [FromUri] AmigoTenantTRoleSearchRequest search) { var resp = await _AmigoTenantTRoleApplicationService.SearchAmigoTenantTRoleBasicCriteriaAsync(search).ConfigureAwait(false); return(resp); }
private async Task <bool> ExistsToSave(AmigoTenantTRoleSearchRequest search) { var queryFilter = GetQueryFilterForExistsToSave(search); var result = await _dataAccess.AnyAsync(queryFilter); return(result); }
public async Task <bool> Exists(AmigoTenantTRoleSearchRequest search) { var queryFilter = GetQueryFilter(search); var result = await _dataAccess.AnyAsync(queryFilter); return(result); }
public async Task <ResponseDTO> Register(AmigoTenantTRoleDTO dto) { var command = _mapper.Map <AmigoTenantTRoleDTO, RegisterAmigoTenantTRoleCommand>(dto); var searchRequest = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = dto.AmigoTenantTRoleId, Code = dto.Code }; var exists = await ExistsToSave(searchRequest); if (exists) { return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = Constants.Entity.AmigoTenantTRole.ErrorMessage.RoleExist })); } var resp = await _bus.SendAsync(command); return(ResponseBuilder.Correct(resp)); }
public async Task <ResponseDTO> Update(AmigoTenantTUserDTO dto) { StringBuilder msg = new StringBuilder(); var roleRequest = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = dto.AmigoTenantTRoleId.HasValue ? dto.AmigoTenantTRoleId.Value : 0 }; var roleToAdd = await _AmigoTenantTRoleApplicationService.GetAmigoTenantTRoleBasicByIdAsync(roleRequest); var amigoTenantTUser = await GetUsersByIdAsync(dto.AmigoTenantTUserId); roleRequest = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = amigoTenantTUser.AmigoTenantTRoleId.HasValue ? amigoTenantTUser.AmigoTenantTRoleId.Value : 0 }; var roleToRemove = await _AmigoTenantTRoleApplicationService.GetAmigoTenantTRoleBasicByIdAsync(roleRequest); var isUserModifiedAdmin = await IsAdmin(dto.UpdatedBy.Value); if (roleToAdd.Data.IsAdmin && !isUserModifiedAdmin) { msg.AppendLine("User does not have permission to update root users"); return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = msg.ToString() })); } /******************************************* * 1. UPDATING IN IDENTITY SERVER *******************************************/ // var agent = new ISHttpClientAgent(); var identityUserDto = new IdentityUserDTO() { Id = dto.AmigoTenantTUserId, UserName = dto.Username, FirstName = dto.FirstName, LastName = dto.LastName, Email = dto.Email, PhoneNumber = dto.PhoneNumber, RowStatus = dto.RowStatus }; _identityAgent.IdentityServerClientSettings = this.IdentityServerClientSettings; var responseAgent = await _identityAgent.Put_AmigoTenantTUserDTO_IdentityServer(identityUserDto); await ProcessResponseAgent(responseAgent, dto, msg); if (responseAgent.IsSuccessStatusCode) { //--- AddClaim var claimRequest = new IdentitySeverAgent.RemoveClaimRequest() { UserId = amigoTenantTUser.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToRemove.Data.Name }; var claimAgent = await _identityAgent.Remove_ClaimRequest_IdentityServer(claimRequest); if (!claimAgent.IsSuccessStatusCode) { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); } //--- RemoveClaim var claimAddRequest = new IdentitySeverAgent.AddClaimRequest() { UserId = dto.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToAdd.Data.Code }; var claimAddAgent = await _identityAgent.Save_ClaimRequest_IdentityServer(claimAddRequest); if (!claimAddAgent.IsSuccessStatusCode) { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); } } //------------------------------------------------------------------------------------------------------- //using (var httpClient = ISHttpClient.GetClient(IdentityServerClientSettings)) //{ // dto.Id = dto.AmigoTenantTUserId; // var response = await httpClient.PutAsJsonAsync("api/Users", dto); // await ProcessResponse(response, httpClient, dto, msg); // var claimRequestRemove = new RemoveClaimRequest() { UserId = amigoTenantTUser.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToRemove.Data.Name }; // var claimResponseRemove = await httpClient.PostAsJsonAsync("api/Users/removeClaim", claimRequestRemove); // if (!claimResponseRemove.IsSuccessStatusCode) // msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoRemoved); // var claimRequestAdd = new AddClaimRequest() { UserId = dto.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToAdd.Data.Name }; // var claimResponseAdd = await httpClient.PostAsJsonAsync("api/Users/addClaim", claimRequestAdd); // if (!claimResponseAdd.IsSuccessStatusCode) // msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); //------------------------------------- // Change Password ( "reset") //------------------------------------- if (!string.IsNullOrEmpty(dto.Password)) { var resetUserPasswordRequest = new IdentitySeverAgent.ResetUserPasswordRequest() { Id = dto.AmigoTenantTUserId, Password = dto.Password }; //var resetUserPasswordResponse = await httpClient.PostAsJsonAsync("api/Users/resetPassword", resetUserPasswordRequest); var resetUserPasswordResponse = await _identityAgent.Reset_AmigoTenantTUser_Password_IdentityServer(resetUserPasswordRequest); if (resetUserPasswordResponse.IsSuccessStatusCode) { var content = await resetUserPasswordResponse.Content.ReadAsAsync <ResponseDTO>(); if (!content.IsValid) { return(ResponseBuilder.InCorrect().WithMessages(content.Messages.ToArray())); } } else { return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = Constants.Entity.AmigoTenantTUser.ErrorMessage.PasswordNotUpdated })); } } // } /**************************************************** * 2. UPDATING IN SERVICE *****************************************************/ var command = _mapper.Map <AmigoTenantTUserDTO, UpdateAmigoTenantTUserCommand>(dto); var userSearchRequest = new UserSearchRequest() { AmigoTenantTUserId = dto.AmigoTenantTUserId, UserName = dto.Username }; var exists = await ExistsToUpdate(userSearchRequest); if (!exists) { msg.AppendLine(string.Format("{0} on {1}", Constants.Entity.AmigoTenantTUser.ErrorMessage.UserNoExist, Constants.Services.AmigoTenantService)); } else { var resp = await _bus.SendAsync(command); return(ResponseBuilder.Correct(resp)); } return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = msg.ToString() })); }
public async Task <ResponseDTO> Register(AmigoTenantTUserDTO dto) { if (dto == null) { return(ResponseBuilder.InCorrect()); } var msg = new StringBuilder(); var roleRequest = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = dto.AmigoTenantTRoleId ?? 0 }; var roleDto = await _AmigoTenantTRoleApplicationService.GetAmigoTenantTRoleBasicByIdAsync(roleRequest); var isUserModifiedAdmin = await IsAdmin(dto.CreatedBy.Value); if (roleDto.Data.IsAdmin && !isUserModifiedAdmin) { msg.AppendLine("User does not have permission to create root users"); return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = msg.ToString() })); } var userSearchRequest = new UserSearchRequest() { AmigoTenantTUserId = dto.AmigoTenantTUserId, UserName = dto.Username }; var existsInactive = await ExistsInactiveUser(userSearchRequest); // var agent = new ISHttpClientAgent(); _identityAgent.IdentityServerClientSettings = this.IdentityServerClientSettings; if (!existsInactive) { /******************************************* * 1. INSERTING IN IDENTITY SERVER * { * "AmigoTenantTUserId": 0, * "Username": "******", * "PayBy": "1", * "UserType": "1", * "DedicatedLocationId": 810, * "BypassDeviceValidation": "1", * "UnitNumber": "1", * "TractorNumber": "1", * "FirstName": "12", * "LastName": "132", * "Email": "*****@*****.**", * "Password": "******", * "Id": 0, * "PhoneNumber": "122131", * "AmigoTenantTRoleId": 1, * "RowStatus": true, * "EntityStatus": 0 * }AnyAsync *******************************************/ /* * using (var httpClient = ISHttpClient.GetClient(IdentityServerClientSettings)) * { * var response = await httpClient.PostAsJsonAsync("api/Users", dto); * await ProcessResponse(response, httpClient, dto, msg); * * var claimRequest = new AddClaimRequest() { UserId = dto.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleDto.Data.Code }; * var claimResponseAdd = await httpClient.PostAsJsonAsync("api/Users/addClaim", claimRequest); * if (!claimResponseAdd.IsSuccessStatusCode) * msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); * * } */ var responseAgent = await _identityAgent.Save_AmigoTenantTUserDTO_IdentityServer(dto); await ProcessResponseAgent(responseAgent, dto, msg); if (responseAgent.IsSuccessStatusCode) { var claimRequest = new IdentitySeverAgent.AddClaimRequest() { UserId = dto.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleDto.Data.Code }; var claimAgent = await _identityAgent.Save_ClaimRequest_IdentityServer(claimRequest); if (!claimAgent.IsSuccessStatusCode) { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); } } } else { /******************************************* * 1. UPDATING IN IDENTITY SERVER *******************************************/ var amigoTenantTUser = await GetUsersByUserNameAsync(dto.Username); if (amigoTenantTUser != null) { var roleRequestToAdd = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = dto.AmigoTenantTRoleId.HasValue ? dto.AmigoTenantTRoleId.Value : 0 }; var roleToAdd = await _AmigoTenantTRoleApplicationService.GetAmigoTenantTRoleBasicByIdAsync(roleRequestToAdd); roleRequest = new AmigoTenantTRoleSearchRequest() { AmigoTenantTRoleId = amigoTenantTUser.AmigoTenantTRoleId.HasValue ? amigoTenantTUser.AmigoTenantTRoleId.Value : 0 }; var roleToRemove = await _AmigoTenantTRoleApplicationService.GetAmigoTenantTRoleBasicByIdAsync(roleRequest); var identityUserDto = new IdentityUserDTO() { Id = amigoTenantTUser.AmigoTenantTUserId, UserName = dto.Username, FirstName = dto.FirstName, LastName = dto.LastName, Email = dto.Email, PhoneNumber = dto.PhoneNumber, RowStatus = dto.RowStatus }; _identityAgent.IdentityServerClientSettings = this.IdentityServerClientSettings; var responseAgent = await _identityAgent.Put_AmigoTenantTUserDTO_IdentityServer(identityUserDto); await ProcessResponseAgent(responseAgent, dto, msg); if (responseAgent.IsSuccessStatusCode) { //--- AddClaim var claimRequest = new IdentitySeverAgent.RemoveClaimRequest() { UserId = amigoTenantTUser.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToRemove.Data.Name }; var claimAgent = await _identityAgent.Remove_ClaimRequest_IdentityServer(claimRequest); if (!claimAgent.IsSuccessStatusCode) { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); } //--- RemoveClaim var claimAddRequest = new IdentitySeverAgent.AddClaimRequest() { UserId = amigoTenantTUser.AmigoTenantTUserId, ClaimType = "role", ClaimValue = roleToAdd.Data.Code }; var claimAddAgent = await _identityAgent.Save_ClaimRequest_IdentityServer(claimAddRequest); if (!claimAddAgent.IsSuccessStatusCode) { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.ClaimNoCreated); } } //------------------------------------- // Change Password ( "reset") //------------------------------------- if (!string.IsNullOrEmpty(dto.Password)) { var resetUserPasswordRequest = new IdentitySeverAgent.ResetUserPasswordRequest() { Id = amigoTenantTUser.AmigoTenantTUserId, Password = dto.Password }; //var resetUserPasswordResponse = await httpClient.PostAsJsonAsync("api/Users/resetPassword", resetUserPasswordRequest); var resetUserPasswordResponse = await _identityAgent.Reset_AmigoTenantTUser_Password_IdentityServer(resetUserPasswordRequest); if (resetUserPasswordResponse.IsSuccessStatusCode) { var content = await resetUserPasswordResponse.Content.ReadAsAsync <ResponseDTO>(); if (!content.IsValid) { return(ResponseBuilder.InCorrect().WithMessages(content.Messages.ToArray())); } } else { return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = Constants.Entity.AmigoTenantTUser.ErrorMessage.PasswordNotUpdated })); } } // } } } /***************************************** * 2. INSERTING IN SERVICE ******************************************/ var existsToSave = await ExistsActiveToSave(userSearchRequest); if (existsToSave) { msg.AppendLine(string.Format("{0} on {1}", Constants.Entity.AmigoTenantTUser.ErrorMessage.UserExist, Constants.Services.AmigoTenantService)); } else { if (!existsInactive) { if (dto.AmigoTenantTUserId > 0) { var command = _mapper.Map <AmigoTenantTUserDTO, RegisterAmigoTenantTUserCommand>(dto); var resp = await _bus.SendAsync(command); return(ResponseBuilder.Correct(resp).WithMessages(new ApplicationMessage() { Message = msg.ToString() })); } else { msg.AppendLine(Constants.Entity.AmigoTenantTUser.ErrorMessage.UserNoCreatedInIdentity); } } else { var amigoTenantTUser = await GetUsersByUserNameAsync(dto.Username); if (amigoTenantTUser != null) { dto.AmigoTenantTUserId = amigoTenantTUser.AmigoTenantTUserId; var command = _mapper.Map <AmigoTenantTUserDTO, UpdateAmigoTenantTUserCommand>(dto); var userSearchRequestToUpdate = new UserSearchRequest() { AmigoTenantTUserId = dto.AmigoTenantTUserId, UserName = dto.Username }; var existsToUpdate = await ExistsToUpdate(userSearchRequestToUpdate); if (!existsToUpdate) { msg.AppendLine(string.Format("{0} on {1}", Constants.Entity.AmigoTenantTUser.ErrorMessage.UserNoExist, Constants.Services.AmigoTenantService)); } else { var resp = await _bus.SendAsync(command); return(ResponseBuilder.Correct(resp)); } } } } return(ResponseBuilder.InCorrect().WithMessages(new ApplicationMessage() { Message = msg.ToString() })); }
public async Task <bool> Exists([FromUri] AmigoTenantTRoleSearchRequest search) { var resp = await _AmigoTenantTRoleApplicationService.Exists(search).ConfigureAwait(false); return(resp); }
public async Task <ResponseDTO <PagedList <AmigoTenantTRoleBasicDTO> > > SearchAmigoTenantTRoleBasicCriteriaAsync(AmigoTenantTRoleSearchRequest search) { var queryFilter = GetQueryFilterBasic(search); var result = await _dataAccessBasic.ListPagedAsync(queryFilter, search.Page, search.PageSize); var pagedResult = new PagedList <AmigoTenantTRoleBasicDTO>() { Items = result.Items, PageSize = result.PageSize, Page = result.Page, Total = result.Total }; return(ResponseBuilder.Correct(pagedResult)); }
private Expression <Func <AmigoTenantTRoleDTO, bool> > GetQueryFilterForExistsToSave(AmigoTenantTRoleSearchRequest search) { Expression <Func <AmigoTenantTRoleDTO, bool> > queryFilter = p => p.RowStatus; if (search.AmigoTenantTRoleId > 0) { queryFilter = queryFilter.And(p => p.AmigoTenantTRoleId != search.AmigoTenantTRoleId); } if (!string.IsNullOrEmpty(search.Code)) { queryFilter = queryFilter.And(p => p.Code.Contains(search.Code)); } return(queryFilter); }
private Expression <Func <AmigoTenantTRoleBasicDTO, bool> > GetQueryFilterBasic(AmigoTenantTRoleSearchRequest search) { Expression <Func <AmigoTenantTRoleBasicDTO, bool> > queryFilter = p => true; if (!string.IsNullOrEmpty(search.Code)) { queryFilter = queryFilter.And(p => p.Code.Contains(search.Code)); } if (!string.IsNullOrEmpty(search.Name)) { queryFilter = queryFilter.And(p => p.Name.Contains(search.Name)); } return(queryFilter); }
public async Task <ResponseDTO <AmigoTenantTRoleBasicDTO> > GetAmigoTenantTRoleBasicByIdAsync(AmigoTenantTRoleSearchRequest search) { Expression <Func <AmigoTenantTRoleBasicDTO, bool> > queryFilter = p => true; if (search.AmigoTenantTRoleId > 0) { queryFilter = queryFilter.And(p => p.AmigoTenantTRoleId == search.AmigoTenantTRoleId); } var result = await _dataAccessBasic.FirstOrDefaultAsync(queryFilter); return(ResponseBuilder.Correct(result)); }