From c5a333dcef900416b76ef679e29416004f07fa05 Mon Sep 17 00:00:00 2001 From: dibakor Date: Sun, 21 Jun 2026 16:51:27 +0600 Subject: [PATCH] add refresh token --- .../Integrations/IntegrationAuthRequest.cs | 6 +- .../Integrations/IntegrationAuthResponse.cs | 17 +- .../Contracts/Auth/IRefreshTokenService.cs | 4 +- .../Services/Auth/RefreshTokenService.cs | 157 +- .../IntegrationAuthController.cs | 1882 ++--------------- 5 files changed, 284 insertions(+), 1782 deletions(-) diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationAuthRequest.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationAuthRequest.cs index b1891c7..49eeda3 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationAuthRequest.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Requests/Integrations/IntegrationAuthRequest.cs @@ -13,10 +13,10 @@ public class IntegrationLoginRequest public string LoginId { get; set; } [Required, NotNull, StringLength(maximumLength: 150, MinimumLength = 5, ErrorMessage = "Password must be between 1 and 30 characters.")] - public string Password { get; set; } - public string RefreshToken { get; set; } + public string Password { get; set; } } + public class IntegrationRefreshTokenRequest { public string RefreshToken { get; set; } @@ -35,7 +35,7 @@ public class RevokedRefreshTokenRequest public class GenerateRefreshTokenRequest { - public User User { get; set; } + public int UserId { get; set; } public string IpAddress { get; set; } public string RawRefreshToken { get; set; } } \ No newline at end of file diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/IntegrationAuthResponse.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/IntegrationAuthResponse.cs index 864f662..090c76e 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/IntegrationAuthResponse.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Models/Responses/Integrations/IntegrationAuthResponse.cs @@ -14,10 +14,16 @@ public class IntegrationLoginResponse : ResponseBase public DateTime AccessTokenExpiry { get; set; } } +public class IntegrationRrefreshTokenResponse : ResponseBase +{ + public string AccessToken { get; set; } = string.Empty; + public string RefreshToken { get; set; } = string.Empty; +} + public class RefreshTokenResponse : ResponseBase { - public string UserId { get; set; } + public int UserId { get; set; } public string TokenHash { get; set; } public string IpAddress { get; set; } public DateTime ExpiredAt { get; set; } @@ -30,4 +36,13 @@ public class GenerateRefreshTokenResponse : ResponseBase { public string RefreshToken { get; set; } public DateTime ExpireTime { get; set; } +} + +public class UserByRefreshTokenResponse : ResponseBase +{ + public int UserId { get; set; } + public string LoginId { get; set; } + public string EmailAddress { get; set; } + public string AuthKey { get; set; } + public bool IsActive { set; get; } } \ No newline at end of file diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs index c83520f..38bc176 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Contracts/Auth/IRefreshTokenService.cs @@ -10,5 +10,7 @@ public interface IRefreshTokenService Task AddAsync(InsertRefreshTokenRequest refreshToken); Task RevokeAsync(RevokedRefreshTokenRequest token); Task RevokeAllForUserAsync(int userId); - Task GenerateRefreshToken(GenerateRefreshTokenRequest request); + Task GenerateRefreshTokenByUserAsync(GenerateRefreshTokenRequest request); + Task GenerateRefreshTokenAsync(string refreshToken, string ipAddress); + Task GetUserByRefreshTokenAsync(string refreshToken); } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs index 9b5ba19..e39b090 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI.Services/Services/Auth/RefreshTokenService.cs @@ -77,7 +77,33 @@ public class RefreshTokenService : IRefreshTokenService using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode); try { - await GetByTokenHashAsync(tc, tokenHash); + response= await GetByTokenHashAsync(tc, tokenHash); + tc.End(); + } + catch (Exception ie) + { + tc?.HandleError(); + + throw DBCustomError.GenerateCustomError(ie); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return response; + } + + public async Task GetUserByRefreshTokenAsync(string refreshToken) + { + UserByRefreshTokenResponse response = new(); + try + { + using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode); + try + { + response = await GetUserByRefreshTokenAsync(tc, refreshToken); tc.End(); } catch (Exception ie) @@ -108,7 +134,7 @@ public class RefreshTokenService : IRefreshTokenService { if (dr.Read()) { - response.UserId = dr.GetString(0); + response.UserId = dr.GetInt32(0); response.TokenHash = dr.GetString(1); response.IpAddress = dr.GetString(2); response.ExpiredAt = dr.GetDateTime(3); @@ -125,6 +151,36 @@ public class RefreshTokenService : IRefreshTokenService return response; } + private async Task GetUserByRefreshTokenAsync(TransactionContext tc, string refreshToken) + { + UserByRefreshTokenResponse response = new(); + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@RefreshToken", pType: SqlDbType.VarChar, pValue: refreshToken), + ]; + using (IDataReader dr = await tc.ExecuteReaderSpAsync("dbo.GetUserByRefreshToken", parameterValues: p)) + { + if (dr.Read()) + { + response.UserId = dr.GetInt32(0); + response.LoginId = dr.GetString(1); + response.EmailAddress = dr.IsDBNull(2) ? string.Empty : dr.GetString(2); + response.AuthKey = dr.IsDBNull(3) ? string.Empty : dr.GetString(3); + response.IsActive = dr.IsDBNull(4) ? true : false; + } + dr.Close(); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return response; + } + public async Task RevokeAllForUserAsync(int userId) { bool returnValue = false; @@ -206,7 +262,31 @@ public class RefreshTokenService : IRefreshTokenService return returnValue; } - public async Task GenerateRefreshToken(GenerateRefreshTokenRequest request) + + private bool RevokeRefreshTokenByUserIdAsync(TransactionContext tc, int userId) + { + bool returnValue = false; + try + { + SqlParameter[] p = + [ + SqlHelperExtension.CreateInParam(pName: "@UserId", pType: SqlDbType.Int, pValue: userId) + ]; + _ = tc.ExecuteNonQuerySp(spName: "dbo.RevokedAllRefreshTokenByUserId", parameterValues: p); + + returnValue = true; + + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return returnValue; + } + + + public async Task GenerateRefreshTokenByUserAsync(GenerateRefreshTokenRequest request) { GenerateRefreshTokenResponse refreshTokenResponse = new(); try @@ -214,17 +294,10 @@ public class RefreshTokenService : IRefreshTokenService using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); try { - var tokenHash = HashToken(request.RawRefreshToken); - - var storedToken = await GetByTokenHashAsync(tc, tokenHash); - - if (storedToken.UserId is not null && !storedToken.IsActive) - throw new UnauthorizedAccessException("Refresh token has expired or been revoked."); - // Rotate: revoke old token, issue new one - RevokeAsync(tc,new RevokedRefreshTokenRequest() { RefreshToken = storedToken.TokenHash }); + RevokeRefreshTokenByUserIdAsync(tc, request.UserId); - refreshTokenResponse= await IssueTokensAsync(tc,request.User, request.IpAddress); + refreshTokenResponse= await IssueTokensAsync(tc,request.UserId, request.IpAddress); tc.End(); } @@ -243,16 +316,68 @@ public class RefreshTokenService : IRefreshTokenService return refreshTokenResponse; } + public async Task GenerateRefreshTokenAsync(string refreshToken, string ipAddress) + { + string refreshTokenResponse = string.Empty; + try + { + using TransactionContext tc = await TransactionContext.BeginAsync(_settings.DefaultConnection.ConnectionNode, true); + try + { + bool isValid = IsValidRefreshTokenAsync(tc, refreshToken); + if(string.IsNullOrEmpty(refreshToken) && !isValid) + { + throw new Exception($"Refresh token has expired or been revoked- {refreshToken}"); + } + + var storedToken = await GetByTokenHashAsync(tc, refreshToken); + + RevokeRefreshTokenByUserIdAsync(tc, storedToken.UserId); + + var result = await IssueTokensAsync(tc, storedToken.UserId, ipAddress); + refreshTokenResponse = result.RefreshToken; + + tc.End(); + } + catch (Exception ie) + { + tc?.HandleError(); + + throw DBCustomError.GenerateCustomError(ie); + } + } + catch (Exception e) + { + throw new InvalidOperationException(e.Message, e); + } + + return refreshTokenResponse; + } // ----- private helpers ----- - private async Task IssueTokensAsync(TransactionContext tc, User user, string ipAddress) + private bool IsValidRefreshTokenAsync(TransactionContext tc, string refreshToken ) + { + try + { + string sql = SQLParser.MakeSQL("Select Count(1) FROM RefreshTokens where TokenHash = %s AND RevokedAt is null", refreshToken); + var returnValue = tc.ExecuteScalar(sql); + return Convert.ToInt32(returnValue) > 0 ? true : false; + } + catch(Exception) + { + throw; + } + } + + private async Task IssueTokensAsync(TransactionContext tc, int userId, string ipAddress) { string rawRefreshToken = GenerateRowToken(); + string tokenHash = HashToken(rawRefreshToken); var refreshToken = new InsertRefreshTokenRequest { - UserId = user.UserId, - TokenHash = HashToken(rawRefreshToken), + UserId = userId, + TokenHash = tokenHash, IpAddress = ipAddress, CreatedAt = DateTime.UtcNow, ExpiresAt = DateTime.UtcNow.AddDays(_settings.RefreshTokenDuration) @@ -262,7 +387,7 @@ public class RefreshTokenService : IRefreshTokenService return new GenerateRefreshTokenResponse { - RefreshToken = rawRefreshToken, + RefreshToken = tokenHash, ExpireTime = DateTime.UtcNow.AddMinutes(_settings.RefreshTokenDuration) }; } diff --git a/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationAuthController.cs b/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationAuthController.cs index 4798409..b3c59be 100644 --- a/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationAuthController.cs +++ b/Api/OnlineSalesAutoCrop.CoreAPI/Controllers/IntegretionApi/IntegrationAuthController.cs @@ -1,34 +1,26 @@ -using Asp.Versioning; +using System; +using System.DirectoryServices; +using System.IdentityModel.Tokens.Jwt; +using System.Runtime.Versioning; +using System.Security.Claims; +using System.Text; +using System.Threading.Tasks; +using Asp.Versioning; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; -using OnlineSalesAutoCrop.CoreAPI.Configurations; using OnlineSalesAutoCrop.CoreAPI.Models; using OnlineSalesAutoCrop.CoreAPI.Models.Global; -using OnlineSalesAutoCrop.CoreAPI.Models.Objects; using OnlineSalesAutoCrop.CoreAPI.Models.Objects.Systems; -using OnlineSalesAutoCrop.CoreAPI.Models.Requests; using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Integrations; -using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Setups; -using OnlineSalesAutoCrop.CoreAPI.Models.Requests.Systems; -using OnlineSalesAutoCrop.CoreAPI.Models.Responses; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Integrations; using OnlineSalesAutoCrop.CoreAPI.Models.Responses.Systems; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Auth; using OnlineSalesAutoCrop.CoreAPI.Services.Contracts.Systems; -using System; -using System.Collections.Generic; -using System.DirectoryServices; -using System.IdentityModel.Tokens.Jwt; -using System.IO; -using System.Linq; -using System.Runtime.Versioning; -using System.Security.Claims; -using System.Text; -using System.Threading.Tasks; + namespace OnlineSalesAutoCrop.CoreAPI.Controllers { @@ -213,12 +205,12 @@ namespace OnlineSalesAutoCrop.CoreAPI.Controllers string userToken = tokenHandler.WriteToken(token); GenerateRefreshTokenRequest refreshTokenRequest = new GenerateRefreshTokenRequest() { - User = user, + UserId = user.UserId, IpAddress = ipAddress, - RawRefreshToken = request.RefreshToken + RawRefreshToken = string.Empty }; - var refreshToken =await _refreshTokenService.GenerateRefreshToken(refreshTokenRequest); + var refreshToken =await _refreshTokenService.GenerateRefreshTokenByUserAsync(refreshTokenRequest); //If token length is greater than or equal to 4096 (4KB) then return error @@ -251,1749 +243,117 @@ namespace OnlineSalesAutoCrop.CoreAPI.Controllers } } - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("loadMenu")] - public async Task LoadMenu([FromBody] ByUserIdRequest request) + + /// + /// Login using your credential data retrieve from SqlServer + /// + /// + /// + /// + /// If login successful ValidUser: true + /// If login successful Return ValidUser: true and UserName: not empty + [HttpPost("RefreshToken")] + [AllowAnonymous] + [IgnoreAntiforgeryToken] + [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(IntegrationLoginResponse))] + public async Task GetRefreshToken([FromBody] IntegrationRefreshTokenRequest request) { - ArgumentNullException.ThrowIfNull(request); - - try - { - MenuResponse response = await _service.GetUserPermissionsAsync(userId: request.UserId); - return Ok(response.Item); - } - catch (Exception ex) - { - _logger.LogError(ex); - return StatusCode(StatusCodes.Status500InternalServerError, (ex.InnerException != null ? ex.InnerException.Message : ex.Message)); - } - } - - /// - /// - /// - /// - /// - [IgnoreAntiforgeryToken] - [HttpPost("validateOtp")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task ValidateOtp([FromBody] OtpValidationRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { Value = false, ReturnStatus = StatusCodes.Status200OK }; - if (string.IsNullOrEmpty(request.OtpCode) || request.OtpCode.Length != 6) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Otp must be 6 digit."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - if (request.AuthMethod == EnumAuthenticationMethod.ThirdPartyAuthenticator) + IntegrationRrefreshTokenResponse response = new(); + try + { + var userRefreshToken = await _refreshTokenService.GetUserByRefreshTokenAsync(request.RefreshToken); + if(!userRefreshToken.IsActive) { - string secretKey = HttpContext.User.GetClaimValue("AuthKey"); - if (string.IsNullOrEmpty(secretKey)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Authentication key is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } + string msg = $"Refresh Token is not active: {request?.RefreshToken}"; + _logger.LogError(message: msg); + response.ReturnStatus = StatusCodes.Status401Unauthorized; + response.ReturnMessage.Add(msg); + return StatusCode(StatusCodes.Status401Unauthorized, response); + } - TOtpService otp = new(); - DateTime now = DateTime.UtcNow; - response.Value = otp.ValidateTwoFactorPIN(secretKey, request.OtpCode, now); - } - else + byte[] key = Encoding.ASCII.GetBytes(_appSettings.JwtCryptoKey); + JwtSecurityTokenHandler tokenHandler = new(); + var tokenDescriptor = new SecurityTokenDescriptor { - response.Value = await _service.ValidateAuthValueAsync(request.OtpCode, request.UserId); - } - - if (!response.Value) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("This is not a valid Otp."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - else - { - return Ok(response); - } - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status406NotAcceptable; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("addUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task AddUser([FromBody] NewUserRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.2_1"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Add User."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int createdBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.AddUserAsync(user: request, ipAddress: ipAddress, createdBy: createdBy); - response.ReturnMessage.Add("User added successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("editUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task EditUser([FromBody] UserRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.2_2"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Update User."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (request.AuthMethod == EnumAuthenticationMethod.ThirdPartyAuthenticator && (string.IsNullOrEmpty(request.AuthKey) || string.IsNullOrWhiteSpace(request.AuthKey) || request.AuthKey.Length <= 0)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("For third party Authenticator, Authentication key is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int modifiedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.EditUserAsync(user: request, ipAddress: ipAddress, modifiedBy: modifiedBy); - response.ReturnMessage.Add("User edited successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("deleteUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task DeleteUser([FromBody] ByUserIdRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.2_3"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Delete User."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - int deletedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.DeleteUserAsync(userId: request.UserId, deletedBy: deletedBy); - response.ReturnMessage.Add("User deleted successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("unlockUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task UnlockUser([FromBody] UserUnlockRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.2_2"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Unlock User."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - int unlockedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.UnlockUserAsync(userId: request.UserId, loginId: request.LoginId, unlockedBy: unlockedBy); - response.ReturnMessage.Add("User Unlocked successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("resetPassword")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task ResetPassword([FromBody] ResetPasswordRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (request.UserId == 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("User is not valid."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.Password) || string.IsNullOrWhiteSpace(request.Password)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.ConfirmPassword) || string.IsNullOrWhiteSpace(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Confirm Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - #region Decrypt Password - - string cipherSecretKey = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - request.Password = Helper.DecryptData(secret: cipherSecretKey, data: request.Password); - request.ConfirmPassword = Helper.DecryptData(secret: cipherSecretKey, data: request.ConfirmPassword); - - #endregion - - if (!request.Password.Equals(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("New password and confirm password are not same."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.3_2"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Reset Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int changedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.ResetPasswordAsync(userId: request.UserId, newPassword: request.ConfirmPassword, ipAddress: ipAddress, changedBy: changedBy); - response.ReturnMessage.Add("Password Reset successfully, User must change password at next Login."); - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [IgnoreAntiforgeryToken] - [HttpPost("changePassword")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task ChangePassword([FromBody] PasswordChangeRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (request.UserId == 0) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("Your not a valid user."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.OldPassword) || string.IsNullOrWhiteSpace(request.OldPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Old Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.Password) || string.IsNullOrWhiteSpace(request.Password)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.ConfirmPassword) || string.IsNullOrWhiteSpace(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Confirm Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - #region Decrypt Password - - string cipherSecretKey = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - request.OldPassword = Helper.DecryptData(secret: cipherSecretKey, data: request.OldPassword); - request.Password = Helper.DecryptData(secret: cipherSecretKey, data: request.Password); - request.ConfirmPassword = Helper.DecryptData(secret: cipherSecretKey, data: request.ConfirmPassword); - - #endregion - - if (!request.Password.Equals(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("New password and confirm password are not same."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int changedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.ChangePasswordAsync(userId: request.UserId, oldPassword: request.OldPassword, newPassword: request.ConfirmPassword, ipAddress: ipAddress, changedBy: changedBy); - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("updateMyPassword")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task UpdateMyPassword([FromBody] PasswordChangeRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (string.IsNullOrEmpty(request.OldPassword) || string.IsNullOrWhiteSpace(request.OldPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Old Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.Password) || string.IsNullOrWhiteSpace(request.Password)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.ConfirmPassword) || string.IsNullOrWhiteSpace(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Invalid parameter value Confirm Password."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - #region Decrypt Password - - string cipherSecretKey = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - request.OldPassword = Helper.DecryptData(secret: cipherSecretKey, data: request.OldPassword); - request.Password = Helper.DecryptData(secret: cipherSecretKey, data: request.Password); - request.ConfirmPassword = Helper.DecryptData(secret: cipherSecretKey, data: request.ConfirmPassword); - - #endregion - - if (!request.Password.Equals(request.ConfirmPassword)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("New password and confirm password are not same."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.ChangePasswordAsync(userId: userId, oldPassword: request.OldPassword, newPassword: request.ConfirmPassword, ipAddress: ipAddress, changedBy: userId); - response.ReturnMessage.Add("Password changed successfully."); - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("updateMyTheme")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task UpdateMyTheme([FromBody] UserThemeRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.UpdateMyThemeAsync(userId: userId, menuLayout: request.MenuLayout, themeName: request.ThemeName, schemeName: request.SchemeName); - response.ReturnMessage.Add("Your theme set successfully. Need re-login to see the effect."); - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("updateMyInfo")] - [AllowAnonymous, IgnoreAntiforgeryToken] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task UpdateMyInfo([FromBody] UpdateMyInfoRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - if (request.EmployeeId < 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("System allow to update only Your Information."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - int modifiedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.UpdateMyInfoAsync(address: request.Address, contactNo: request.ContactNo, modifiedBy: modifiedBy, emplyeeId: request.EmployeeId); - response.ReturnMessage.Add("Your Information updated successfully."); - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getAttributes")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserAttributesResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserAttributesResponse))] - public async Task GetAttributes([FromBody] ByUserAttributesRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserAttributesResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string key = "UserAttributes"; - string key2 = $"{request.UserId}~{request.ClientType}"; - if (!_cache.TryGetValue(key: key, key2: key2, value: out response)) - { - response = await _service.GetAttributesAsync(userId: request.UserId, clientType: request.ClientType); - - //Cache - _ = _cache.Set(key: key, key2: key2, value: response, options: _options); - } - response.ReturnStatus = StatusCodes.Status200OK; - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("saveAttributes")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task SaveAttributes([FromBody] UserAttributesRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (request.UkIds == null || request.UkIds.Count <= 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no data to save."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - string msg, key; - if (request.ClientType == 2) - { - key = "ELIT.1.5.12_2"; - msg = "Supplier(s) to user"; - } - else - { - key = "ELIT.1.5.11_2"; - msg = "Project(s) to user"; - } - - bool permitted = await HttpContext.IsPermitted(key); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add($"You are not authorize to Assign {msg}."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - int attributeSetBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.SaveAttributesAsync(userId: request.UserId, clientType: request.ClientType, ipAddress: ipAddress, attributeSetBy: attributeSetBy, ukIds: request.UkIds); - response.ReturnMessage.Add($"{msg} Assigned successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("deleteAttributes")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task DeleteAttributes([FromBody] ByUserAttributesRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - string msg, key; - if (request.ClientType == 2) - { - key = "ELIT.1.5.12_3"; - msg = "Supplier(s) from user"; - } - else - { - key = "ELIT.1.5.11_3"; - msg = "Project(s) from user"; - } - - bool permitted = await HttpContext.IsPermitted(key); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add($"You are not authorize to Unassign {msg}."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - int deletedBy = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.DeleteAttributesAsync(userId: request.UserId, clientType: request.ClientType, deletedBy: deletedBy); - response.ReturnMessage.Add($"{msg} Unassigned successfully..."); - - //Cache - _cache.Clear("User"); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// Returns users according to search criteria. - /// - /// - /// top 50 users - /// - /// Top 50 users - /// If the item is null. - //[ValidateSession] - //[HttpPost("getUsers")] - //[ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserSearchResponse))] - //[ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserSearchResponse))] - //public async Task GetUsers([FromBody] UserSearchRequest request) - //{ - // ArgumentNullException.ThrowIfNull(request); - - // UserSearchResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - // try - // { - // int userId = HttpContext.User.GetClaimValue(Constants.UserId); - // request.CheckOwner = userId != Models.Objects.Systems.User.SuperUser_Id; - - // string key = "Users"; - // string key2 = $"{request.Criteria}~{request.Status}~{request.SortField}~{request.SortOrder}~{request.Skip}~{request.PageSize}~{request.CheckOwner}~{userId}"; - // if (!_cache.TryGetValue(key: key, key2: key2, value: out response)) - // { - // response = await _service.GetUsersAsync(request: request, userId: userId); - // _ = _cache.Set(key: key, key2: key2, value: response, options: _options); - // } - - // response.ReturnStatus = StatusCodes.Status200OK; - // return Ok(response); - // } - // catch (Exception ex) - // { - // _logger.LogError(ex); - // response.ReturnStatus = StatusCodes.Status500InternalServerError; - // response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - // return StatusCode(StatusCodes.Status500InternalServerError, response); - // } - //} - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getUsersBasic")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserBasicInfoResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserBasicInfoResponse))] - public async Task GetUsersBasic([FromBody] BasicUserSearchRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserBasicInfoResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string teamSpaceIds = request.TeamSpaceId > 0 ? $"{request.TeamSpaceId}" : HttpContext.User.GetClaimValue("TeamSpaceIds"); - response = await _service.GetUsersBasicAsync(applyFilter: request.ApplyFilter, teamSpaceIds: teamSpaceIds, projectId: request.ProjectId); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getUsersByTeamSpace")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserBasicInfoResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserBasicInfoResponse))] - public async Task GetUsersByTeamSpace([FromBody] BasicUserByTeamSpaceRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserBasicInfoResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string teamSpaceIds = request.TeamSpaceId > 0 ? $"{request.TeamSpaceId}" : HttpContext.User.GetClaimValue("TeamSpaceIds"); - response = await _service.GetUsersByTeamSpaceAsync(teamSpaceIds: teamSpaceIds, projectId: request.ProjectId, userId: request.UserId); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getAttendanceUsers")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserBasicInfoResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserBasicInfoResponse))] - public async Task GetAttendanceUsers([FromBody] ByUserIdRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserBasicInfoResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - response = await _service.GetAttendanceUsersAsync(userId: request.UserId); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getUsersForForceLogout")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserForceLogoutResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserForceLogoutResponse))] - public async Task GetForceLogoutUsers([FromBody] NoContentRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserForceLogoutResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - response = await _service.GetForceLogoutUsersAsync(createdBy: userId); - - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - [ValidateSession] - [HttpPost("forceLogoutNow")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(BooleanResponse))] - public async Task ForceLogoutNow([FromBody] ForceUserLogoutRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (request.UserIds == null || request.UserIds.Count <= 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Parameter value is null/no User was selected."); - return BadRequest(response); - } - - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.4_2"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Do Force Logout."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - response.Value = await _service.ForceLogoutNowAsync(userIds: request.UserIds, ipAddress: ipAddress); - response.ReturnMessage.Add("Process completed successfully..."); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserGetResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserGetResponse))] - public async Task GetUser([FromBody] ByUserIdRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserGetResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string key = "User"; - string key2 = $"{request.UserId}"; - if (!_cache.TryGetValue(key: key, key2: key2, value: out response)) - { - response = await _service.GetUserAsync(userId: request.UserId); - _ = _cache.Set(key: key, key2: key2, value: response, options: _options); - } - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getCurrentUser")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserGetResponse))] - public async Task GetCurrentUser([FromBody] NoContentRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserGetResponse response; - try - { - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - - string key = "UserCurrent"; - string key2 = $"{userId}"; - if (!_cache.TryGetValue(key: key, key2: key2, value: out response)) - { - response = await _service.GetUserAsync(userId: userId); - response.ReturnStatus = StatusCodes.Status200OK; - _ = _cache.Set(key: key, key2: key2, value: response, options: _options); - } - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response = new UserGetResponse() { ReturnStatus = StatusCodes.Status500InternalServerError }; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getMyProfile")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserProfileResponse))] - public async Task GetMyProfile([FromBody] NoContentRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserProfileResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - response = await _service.GetUserProfileAsync(userId: userId); - response.HasPayslipPath = !string.IsNullOrEmpty(_appSettings.PayslipPath); - response.HasAIApiKey = !string.IsNullOrEmpty(_appSettings.ApiKeyOpenAI) || !string.IsNullOrEmpty(_appSettings.ApiKeyGoogle); - response.ReturnStatus = StatusCodes.Status200OK; - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getMyPayslip")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(StringResponse))] - public IActionResult GetMyPayslip([FromBody] PayslipRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - StringResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (string.IsNullOrEmpty(_appSettings.PayslipPath)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Payslip path is not in the system."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - string employeeId = HttpContext.User.GetClaimValue("EmployeeId"); - if (string.IsNullOrEmpty(employeeId)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("You are not a valid Employee to view payslip."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string path = $"{employeeId}_{request.YearMonth:yyyy}_{request.YearMonth:MM}.pdf"; - path = Path.Combine(_appSettings.PayslipPath, path); - if (System.IO.File.Exists(path)) - { - response.Value = Convert.ToBase64String(System.IO.File.ReadAllBytes(path: path)); - return Ok(response); - } - else - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add($"Payslip does not exists for the Month: {request.YearMonth:MMMM, yyyy}"); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("sendQrCodeViaEmail")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public IActionResult SendQrCodeViaEmail([FromForm] QRCodeUploadRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - if (string.IsNullOrEmpty(request.EmailAddress)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no email address to send mail."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.FileName)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no image to send to send mail."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (request.FileData.Length <= 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no image to send to send mail."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - Result result = ImageFileValidator.Validate(request.FileData); - if (!result.Acceptable) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("This is not a valid image file."); - return BadRequest(response); - } - - var fileSpec = Path.Combine(_appSettings.UploadFolder, request.FileName); - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - - using (var stream = new FileStream(fileSpec, FileMode.Create)) - { - request.FileData.CopyTo(stream); - } - - bool sent = MailHelper.SendMailMessage(settings: _appSettings, to: [request.EmailAddress], - cc: null, bcc: null, attachments: [fileSpec], embeddedImages: null, isHtmlBody: false, - priority: System.Net.Mail.MailPriority.High, subject: "QR Code", messageBody: "Scan image"); - - if (sent) - { - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - - response.Value = sent; - response.ReturnMessage.Add($"Successfully mail sent to {request.EmailAddress}"); - return Ok(response); - } - else - { - response.Value = sent; - response.ReturnMessage.Add($"Cannot send mail to {request.EmailAddress}"); - return StatusCode(StatusCodes.Status422UnprocessableEntity, response); - } - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("uploadProfileImage")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public IActionResult UploadProfileImage([FromForm] FileUploadRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - if (string.IsNullOrEmpty(request.FileName)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no Image to set Profile image."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (request.FileData.Length <= 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no image to set Profile image."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - Result result = ImageFileValidator.Validate(request.FileData); - if (!result.Acceptable) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("This is not a valid image file."); - return BadRequest(response); - } - - long maxSz = 20 * 1024; - if (request.FileData.Length > maxSz) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Maximum size allowed is 20 Kb"); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - var fileSpec = Path.Combine(_appSettings.ProfileImageFolder, request.FileName); - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - - using (var stream = new FileStream(fileSpec, FileMode.Create)) - { - request.FileData.CopyTo(stream); - } - response.Value = true; - response.ReturnMessage.Add("Refresh page to view your profile image."); - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("uploadDocument")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task UploadDocument([FromForm] UploadDocumentRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - if (string.IsNullOrEmpty(request.FileName)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no valid file to process."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (request.FileData.Length <= 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no valid data to process."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - string fileName, fileSpec; - if (request.DocumentOf == 6) - { - string[] allowedExtensions = [".xlsx", ".xls"]; - string fileExtension = Path.GetExtension(request.FileName).ToLowerInvariant(); - if (!allowedExtensions.Contains(fileExtension)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Only excel file is allowed to process."); - return BadRequest(response); - } - - if (fileExtension.EndsWith(".xls")) - { - Result result = ExcelFileValidator.Validate(request.FileData); - if (!result.Acceptable) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("This is not a valid Excel file."); - return BadRequest(response); - } - } - else - { - Result result = ExcelxFileValidator.Validate(request.FileData); - if (!result.Acceptable) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("This is not a valid Excel file."); - return BadRequest(response); - } - } - - fileName = request.FileName; - fileSpec = Path.Combine(_appSettings.UploadFolder, fileName); - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - } - else if (request.DocumentOf == 2) - { - if (!request.FileName.ToLower().EndsWith(".csv")) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Only csv file is allowed to process."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - fileName = request.FileName; - fileSpec = Path.Combine(_appSettings.UploadFolder, fileName); - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - } - else - { - if (!request.FileName.ToLower().EndsWith(".pdf")) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Only pdf file is allowed to Process."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - long maxSz = 10 * 1024 * 1024; - if (request.FileData.Length > maxSz) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Maximum allowable size is 10 MB"); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - fileName = $"{request.Id}_{request.DocumentOf}.pdf"; - fileSpec = Path.Combine(_appSettings.UploadFolder, fileName); - if (System.IO.File.Exists(fileSpec)) - System.IO.File.Delete(fileSpec); - } - - using (var stream = new FileStream(fileSpec, FileMode.Create)) - { - request.FileData.CopyTo(stream); - } - - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - response.Value = await _service.UploadDocumentAsync(userId: userId, id: request.Id, documentOf: request.DocumentOf, orgFileName: request.FileName, fileName: fileName); - response.ReturnMessage.Add("File Uploaded successfully."); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getDocument")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(StringResponse))] - public IActionResult GetDocument([FromBody] FileViewRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - StringResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - if (string.IsNullOrEmpty(request.FileName)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("There is no Image to set Profile image."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - string path = Path.Combine(_appSettings.UploadFolder, request.FileName); - if (System.IO.File.Exists(path)) - { - response.Value = Convert.ToBase64String(System.IO.File.ReadAllBytes(path: path)); - } - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [HttpPost("findAccount")] - [AllowAnonymous, IgnoreAntiforgeryToken] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(FindAccountResponse))] - public async Task FindAccount([FromBody] FindAccountRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - FindAccountResponse response = new(); - if (string.IsNullOrEmpty(request.AccountId)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Login Id or Email address or Mobile number is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string key = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - request.AccountId = Helper.DecryptData(secret: key, data: request.AccountId); - response = await _service.FindAccountAsync(accountId: request.AccountId); - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [HttpPost("sendPassword")] - [AllowAnonymous, IgnoreAntiforgeryToken] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task SendPassword([FromBody] SendPasswordRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - if (string.IsNullOrEmpty(request.UserId)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("User Id is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.MobileNo)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Mobile number is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - if (string.IsNullOrEmpty(request.EmailAddress)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Email address is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string key = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - string decipherValue = Helper.DecryptData(secret: key, data: request.UserId); - if (string.IsNullOrEmpty(decipherValue)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("User Id is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - if (!int.TryParse(decipherValue, out int userId)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("User Id is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - if (userId == 0) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("User Id is required."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - request.MobileNo = Helper.DecryptData(secret: key, data: request.MobileNo); - request.EmailAddress = Helper.DecryptData(secret: key, data: request.EmailAddress); - if (string.IsNullOrEmpty(request.MobileNo) && string.IsNullOrEmpty(request.EmailAddress)) - { - response.ReturnStatus = StatusCodes.Status417ExpectationFailed; - response.ReturnMessage.Add("Mobile number and Email address both cannot be empty."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - //Do reset password - string newPassword = $"{new Random().Next(100000, 999999)}"; - string ipAddress = Request.HttpContext.GetIpAddress(); - response.Value = await _service.SendPasswordAsync(userId: userId, newPassword: newPassword, ipAddress: ipAddress); - if (response.Value) - { - if (!string.IsNullOrEmpty(request.EmailAddress) && !string.IsNullOrWhiteSpace(request.EmailAddress)) - { - List to = [.. request.EmailAddress.Split(separator: ';', options: StringSplitOptions.RemoveEmptyEntries)]; - await MailHelper.SendMailMessageAsync(settings: _appSettings, to: to, cc: null, bcc: null, attachments: null, embeddedImages: null, isHtmlBody: false, priority: System.Net.Mail.MailPriority.Normal, - subject: "One Time Password", messageBody: $"Your one time password (use as Old password): {newPassword} and must change password at next Login."); - } - if (!string.IsNullOrEmpty(request.MobileNo) && !string.IsNullOrWhiteSpace(request.MobileNo)) - { - MailHelper.SendSMSOrWhatsAppMessage(settings: _appSettings, whatsAppMsg: false, msg: $"Your one time password (use as Old password): {newPassword} and must change password at next Login.", mobileNumber: request.MobileNo); - } - - response.ReturnMessage.Add("Password sent to your Email address and/or Mobile number, User must change password at next Login."); - } - else - { - response.ReturnMessage.Add("Cannot do action on your request."); - } - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("saveAuthorizeLimit")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(BooleanResponse))] - public async Task SaveAuthorizeLimit([FromBody] UserLimitAuthorizeRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - bool permitted = await HttpContext.IsPermitted("ELIT.9.1.14_1") || await HttpContext.IsPermitted("ELIT.9.1.14_2"); - if (!permitted) - { - response.ReturnStatus = StatusCodes.Status403Forbidden; - response.ReturnMessage.Add("You are not authorize to Update Authorization Limit."); - return StatusCode(StatusCodes.Status417ExpectationFailed, response); - } - - try - { - string ipAddress = Request.HttpContext.GetIpAddress(); - string loginId = HttpContext.User.GetClaimValue(Constants.LoginId); - response.Value = await _service.SaveAuthorizeLimitAsync(maxAuthLimit: request.MaxAuthorizeAmount, userId: request.UserId, ipAddress: ipAddress, savedBy: loginId); - response.ReturnMessage.Add("Process completed successfully..."); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getAuthorizeLimit")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(UserAuthorizeLimitResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(UserAuthorizeLimitResponse))] - public async Task GetAuthorizeLimit([FromBody] ByUserIdRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - UserAuthorizeLimitResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - response = await _service.GetAuthorizeLimitAsync(userId: request.UserId); - response.ReturnStatus = StatusCodes.Status200OK; - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - [HttpPost("logOut")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(BooleanResponse))] - public async Task LogOut([FromBody] LogoutRequest request) - { - BooleanResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string key = HttpContext.GetAuthenticationToken(); - string ipAddress = Request.HttpContext.GetIpAddress(); - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - string loginId = HttpContext.User.GetClaimValue(Constants.LoginId); - - if (request.AttendanceLogout) - { - string cipherSecretKey = GlobalFunctions.ConvertFromBase64String(_appSettings.CipherSecretKey); - request.IpAddress = Helper.DecryptData(secret: cipherSecretKey, data: request.IpAddress); - request.MacAddress = Helper.DecryptData(secret: cipherSecretKey, data: request.MacAddress); - request.HostName = Helper.DecryptData(secret: cipherSecretKey, data: request.HostName); - } - _ = await _service.LogoutAsync(ipAddress: ipAddress, userId: userId, logId: request.LogId, attendanceLogout: request.AttendanceLogout, loginId: loginId, localIp: request.IpAddress, macAddress: request.MacAddress, hostName: request.HostName, logoutRemarks: request.LogoutRemarks); - _cache.Clear(pattern: key); - _ = await HttpContext.ClearSessionAsync(); - - response.Value = true; - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// 0 - [AllowAnonymous] - [IgnoreAntiforgeryToken] - [HttpPost("sessionExpired")] - [ProducesResponseType(StatusCodes.Status200OK)] - public async Task SessionExpired([FromBody] LogoutRequest request) - { - try - { - int userId = HttpContext.User.GetClaimValue(Constants.UserId); - string ipAddress = Request.HttpContext.GetIpAddress(); - _ = await _service.LogoutAsync(ipAddress: ipAddress, userId: userId, logId: request.LogId, attendanceLogout: false, loginId: string.Empty, localIp: string.Empty, macAddress: string.Empty, hostName: string.Empty, logoutRemarks: string.Empty); - _ = await HttpContext.ClearSessionAsync(); - } - catch (Exception ex) - { - _logger.LogError(ex); - } - } - - /// - /// - /// - /// - /// - /// - [SupportedOSPlatform("windows")] - private int GetADLoginStatus(string loginId, string password) - { - try - { - const string displayNameAttribute = "DisplayName"; - const string samAccountNameAttribute = "SAMAccountName"; - const string userAccountControlAttribute = "useraccountcontrol"; - - string username = (string.IsNullOrEmpty(_appSettings.ADConfig.Domain) || string.IsNullOrWhiteSpace(_appSettings.ADConfig.Domain)) ? loginId : $"{loginId}@{_appSettings.ADConfig.Domain}"; - using DirectoryEntry entry = new(path: _appSettings.ADConfig.Path, username: username, password: password); - using DirectorySearcher searcher = new(searchRoot: entry); - searcher.Filter = $"({samAccountNameAttribute}={loginId})"; - searcher.PropertiesToLoad.Add(value: displayNameAttribute); - searcher.PropertiesToLoad.Add(value: samAccountNameAttribute); - searcher.PropertiesToLoad.Add(value: userAccountControlAttribute); - var result = searcher.FindOne(); - if (result == null) - return 0; - - ResultPropertyValueCollection displayName = result.Properties[name: displayNameAttribute]; - ResultPropertyValueCollection samAccountName = result.Properties[name: samAccountNameAttribute]; - ResultPropertyValueCollection userAccountControl = result.Properties[name: userAccountControlAttribute]; - int uacFlag = (userAccountControl != null && userAccountControl.Count > 0) ? Convert.ToInt32(userAccountControl[0]) : 0; - if ((uacFlag & 0x000002) == 0x000002) //Disabled - return 2; - else if ((uacFlag & 0x800000) == 0x800000) //Password expired - return 3; - - if (displayName != null && displayName.Count > 0 && samAccountName != null && samAccountName.Count > 0) - return 1; - else - return 0; - } - catch (Exception ex) - { - _logger.LogError(ex); - return 0; - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("getAccessLog")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(AccessLogResponse))] - [ProducesResponseType(StatusCodes.Status204NoContent, Type = typeof(AccessLogResponse))] - public async Task GetAccessLog([FromBody] AccessLogSearchRequest request) - { - ArgumentNullException.ThrowIfNull(request); - - AccessLogResponse response = new() { ReturnStatus = StatusCodes.Status200OK }; - try - { - string loginId = HttpContext.User.GetClaimValue(Constants.LoginId); - bool permitted = await HttpContext.IsPermitted("ELIT.1.2.5_2"); - if (!(permitted || loginId.Equals(value: Models.Objects.Systems.User.SuperUser_LoginId, comparisonType: StringComparison.OrdinalIgnoreCase))) - { - request.LoginId = loginId; - } - response = await _service.GetAccessLogAsync(accessType: request.AccessType, loginId: request.LoginId, startDate: request.StartDate, endDate: request.EndDate); - response.ReturnStatus = StatusCodes.Status200OK; - - return Ok(response); - } - catch (Exception ex) - { - _logger.LogError(ex); - response.ReturnStatus = StatusCodes.Status500InternalServerError; - response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); - return StatusCode(StatusCodes.Status500InternalServerError, response); - } - } - - /// - /// - /// - /// - /// - [ValidateSession] - [HttpPost("loadNotificationCount")] - [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(int))] - public async Task LoadNotificationCount([FromBody] NoContentRequest request) - { - ArgumentNullException.ThrowIfNull(request); - try - { - int value = HttpContext.User.GetClaimValue(Constants.UserId); - value = await _service.LoadNotificationCountAsync(userId: value); - - return Ok(value); - } - catch (Exception ex) - { - _logger.LogError(ex); - return StatusCode(StatusCodes.Status500InternalServerError); - } - } - } + Subject = new ClaimsIdentity( + [ + Helper.CreateClaim("LoginId", userRefreshToken.LoginId), + Helper.CreateClaim("Email", userRefreshToken.EmailAddress), + Helper.CreateClaim("AuthKey", $"{userRefreshToken.AuthKey}"), + Helper.CreateClaim("HashKey", Guid.NewGuid().ToString()) + ]), + Expires = DateTime.UtcNow.AddHours(12), + SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha512Signature) + }; + + SecurityToken token = tokenHandler.CreateToken(tokenDescriptor); + string userToken = tokenHandler.WriteToken(token); + + string ipAddress = Request.HttpContext.GetIpAddress(); + var refreshToken = await _refreshTokenService.GenerateRefreshTokenAsync(request.RefreshToken, ipAddress); + + response.AccessToken = userToken; + response.RefreshToken = refreshToken; + + return StatusCode(StatusCodes.Status431RequestHeaderFieldsTooLarge, response); + + } + catch (Exception ex) + { + string msg = $"{request?.RefreshToken}"; + _logger.LogError(exception: ex, message: msg); + response.ReturnStatus = StatusCodes.Status500InternalServerError; + response.ReturnMessage.Add(ex.InnerException != null ? ex.InnerException.Message : ex.Message); + return StatusCode(StatusCodes.Status500InternalServerError, response); + } + return Ok(); + } + + /// + /// + /// + /// + /// + /// + [SupportedOSPlatform("windows")] + private int GetADLoginStatus(string loginId, string password) + { + try + { + const string displayNameAttribute = "DisplayName"; + const string samAccountNameAttribute = "SAMAccountName"; + const string userAccountControlAttribute = "useraccountcontrol"; + + string username = (string.IsNullOrEmpty(_appSettings.ADConfig.Domain) || string.IsNullOrWhiteSpace(_appSettings.ADConfig.Domain)) ? loginId : $"{loginId}@{_appSettings.ADConfig.Domain}"; + using DirectoryEntry entry = new(path: _appSettings.ADConfig.Path, username: username, password: password); + using DirectorySearcher searcher = new(searchRoot: entry); + searcher.Filter = $"({samAccountNameAttribute}={loginId})"; + searcher.PropertiesToLoad.Add(value: displayNameAttribute); + searcher.PropertiesToLoad.Add(value: samAccountNameAttribute); + searcher.PropertiesToLoad.Add(value: userAccountControlAttribute); + var result = searcher.FindOne(); + if (result == null) + return 0; + + ResultPropertyValueCollection displayName = result.Properties[name: displayNameAttribute]; + ResultPropertyValueCollection samAccountName = result.Properties[name: samAccountNameAttribute]; + ResultPropertyValueCollection userAccountControl = result.Properties[name: userAccountControlAttribute]; + int uacFlag = (userAccountControl != null && userAccountControl.Count > 0) ? Convert.ToInt32(userAccountControl[0]) : 0; + if ((uacFlag & 0x000002) == 0x000002) //Disabled + return 2; + else if ((uacFlag & 0x800000) == 0x800000) //Password expired + return 3; + + if (displayName != null && displayName.Count > 0 && samAccountName != null && samAccountName.Count > 0) + return 1; + else + return 0; + } + catch (Exception ex) + { + _logger.LogError(ex); + return 0; + } + } + } } \ No newline at end of file