From f6bbb415b9de60a27cfc4e9cd7880faf845bc79d Mon Sep 17 00:00:00 2001 From: Palash Biswas Date: Wed, 19 Oct 2022 11:28:11 +0530 Subject: [PATCH] Implement End point Registration and Login --- .../CompanyMasters/CompanyMasterAppService.cs | 1 + .../CompanyMasters/Dto/UserDetailsDto.cs | 17 ++++ .../Controllers/TokenAuthController.cs | 85 ++++++++++++++++++- .../Models/TokenAuth/LoginInputModel.cs | 15 ++++ .../Models/TokenAuth/RegistrationInput.cs | 21 +++++ .../Models/TokenAuth/ResponseMessageModel.cs | 19 +++++ 6 files changed, 154 insertions(+), 4 deletions(-) create mode 100644 BCS.BMC/src/BCS.BMC.Application/CompanyMasters/Dto/UserDetailsDto.cs create mode 100644 BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/LoginInputModel.cs create mode 100644 BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/RegistrationInput.cs create mode 100644 BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/ResponseMessageModel.cs diff --git a/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/CompanyMasterAppService.cs b/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/CompanyMasterAppService.cs index 2d3736c..c689a6a 100644 --- a/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/CompanyMasterAppService.cs +++ b/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/CompanyMasterAppService.cs @@ -37,6 +37,7 @@ using System.Net.Http.Headers; using Newtonsoft.Json; using Abp.Json; + namespace BCS.BMC.CompanyMasters { public class CompanyMasterAppService : ICompanyMasterAppService diff --git a/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/Dto/UserDetailsDto.cs b/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/Dto/UserDetailsDto.cs new file mode 100644 index 0000000..adee3e6 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Application/CompanyMasters/Dto/UserDetailsDto.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.CompanyMasters.Dto +{ + public class UserDetailsDto + { + public int Id { get; set; } + public string Name { get; set; } + public string SurName { get; set; } + public string EmailAddress { get; set; } + public bool IsActive { get; set; } + } +} diff --git a/BCS.BMC/src/BCS.BMC.Web.Core/Controllers/TokenAuthController.cs b/BCS.BMC/src/BCS.BMC.Web.Core/Controllers/TokenAuthController.cs index c879c20..9da67bb 100644 --- a/BCS.BMC/src/BCS.BMC.Web.Core/Controllers/TokenAuthController.cs +++ b/BCS.BMC/src/BCS.BMC.Web.Core/Controllers/TokenAuthController.cs @@ -17,7 +17,6 @@ using BCS.BMC.Authorization; using BCS.BMC.Authorization.Users; using BCS.BMC.Models.TokenAuth; using BCS.BMC.MultiTenancy; -//using System.Web.Mvc; using BCS.BMC.CompanyMasters.Dto; using System.Net; using System.Net.Http; @@ -25,6 +24,9 @@ using Newtonsoft.Json; using System.Net.Http.Headers; using BCS.BMC.BMC.CompanyMasters; using Abp.Domain.Repositories; +using System.Runtime.Intrinsics.X86; +using System.Text; +using System.IO; namespace BCS.BMC.Controllers { @@ -39,7 +41,7 @@ namespace BCS.BMC.Controllers private readonly IExternalAuthManager _externalAuthManager; private readonly UserRegistrationManager _userRegistrationManager; private readonly IRepository _companyMaster; - + ResponseMessageModel responsemessage = new ResponseMessageModel(); public TokenAuthController( LogInManager logInManager, ITenantCache tenantCache, @@ -128,7 +130,7 @@ namespace BCS.BMC.Controllers } var accessToken = CreateAccessToken(CreateJwtClaims(loginResult.Identity)); - + return new ExternalAuthenticateResultModel { AccessToken = accessToken, @@ -257,6 +259,81 @@ namespace BCS.BMC.Controllers return BadRequest(); } return Ok(); - } + } + + [HttpPost] + public async Task Login([FromBody] LoginInputModel input) + { + if (input.CompanyUrl == null) + { + return BadRequest(); + } + + var company = await _companyMaster.FirstOrDefaultAsync(x => x.Url == input.CompanyUrl.ToString()); + if(company == null) + { + return BadRequest(); + } + + using (HttpClient client = new HttpClient()) + { + Uri uri = new Uri(input.CompanyUrl); + var baseUrl = uri + "api/BmcLogin"; + + var data = new + { + usernameOrEmailAddress = input.UsernameOrEmailAddress, + password = input.Password + }; + var requestJson = JsonConvert.SerializeObject(data); + var requestContent = new StringContent(requestJson.ToString()); + requestContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); + HttpResponseMessage response = await client.PostAsync(baseUrl, requestContent); + + response.EnsureSuccessStatusCode(); + if (response.IsSuccessStatusCode) + { + var responseStream = await response.Content.ReadAsStringAsync(); + return Ok(responseStream); + } + return BadRequest(); + } + } + + [HttpPost] + public async Task Registration([FromBody] RegistrationInput input) + { + + using (HttpClient client = new HttpClient()) + { + var baseUrl = input.Subdomain + "/api/services/bwac/employeeRegister/RegisterEmployeeAsNewUser"; + + var data = new + { + tenancyName = "testmsnyc", + phoneNo = input.Phoneno, + userName = input.Email, + password = input.Password + }; + var requestJson = JsonConvert.SerializeObject(data); + var requestContent = new StringContent(requestJson.ToString()); + requestContent.Headers.ContentType = new MediaTypeWithQualityHeaderValue("application/json"); + HttpResponseMessage response = await client.PostAsync(baseUrl, requestContent); + + + if (response.IsSuccessStatusCode) + { + var responseStream = await response.Content.ReadAsStringAsync(); + return Ok(responseStream); + } + else if (response.StatusCode == HttpStatusCode.InternalServerError) + { + var contents = await response.Content.ReadAsStringAsync(); + ResponseMessageModel result = JsonConvert.DeserializeObject(contents); + return BadRequest(result); + } + } + return BadRequest(); + } } } diff --git a/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/LoginInputModel.cs b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/LoginInputModel.cs new file mode 100644 index 0000000..0a26ed3 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/LoginInputModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.Models.TokenAuth +{ + public class LoginInputModel + { + public string CompanyUrl { get; set; } + public string UsernameOrEmailAddress { get; set; } + public string Password { get; set; } + } +} diff --git a/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/RegistrationInput.cs b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/RegistrationInput.cs new file mode 100644 index 0000000..d79bc81 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/RegistrationInput.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.Models.TokenAuth +{ + public class RegistrationInput + { + public string Firstname { get; set; } + public string Lastname { get; set; } + public string Email { get; set; } + public string Phoneno { get; set; } + public string Password { get; set; } + public string Status { get; set; } + public string Protocol { get; set; } + public string Subdomain { get; set; } + public string Domain { get; set; } + } +} diff --git a/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/ResponseMessageModel.cs b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/ResponseMessageModel.cs new file mode 100644 index 0000000..87b7488 --- /dev/null +++ b/BCS.BMC/src/BCS.BMC.Web.Core/Models/TokenAuth/ResponseMessageModel.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BCS.BMC.Models.TokenAuth +{ + public class ResponseMessageModel + { + public Error error { get; set; } + } + + public class Error + { + public string message { get; set; } + + } +}