From bc658786086de9cdbaac055c5a9633191e551d51 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Sun, 29 Jun 2025 16:14:42 +0330 Subject: [PATCH] exper --- Common/Dtos/Exper/ADD_ExperDto.cs | 1 - Hushian.Application/Services/AuthService.cs | 45 ++++++++++++++- .../Services/CompanyService.cs | 12 ++++ Hushian.Application/Services/ExperService.cs | 57 +++++++++++++++++++ Hushian.Application/Services/HashPassword.cs | 26 +++++++++ .../Services/VerificationService.cs | 11 +++- .../Validation/FixedValidation.cs | 16 ++++++ 7 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 Hushian.Application/Services/CompanyService.cs create mode 100644 Hushian.Application/Services/ExperService.cs create mode 100644 Hushian.Application/Services/HashPassword.cs create mode 100644 Hushian.Application/Validation/FixedValidation.cs diff --git a/Common/Dtos/Exper/ADD_ExperDto.cs b/Common/Dtos/Exper/ADD_ExperDto.cs index d4f73db..969d4f5 100644 --- a/Common/Dtos/Exper/ADD_ExperDto.cs +++ b/Common/Dtos/Exper/ADD_ExperDto.cs @@ -9,7 +9,6 @@ namespace Common.Dtos.Exper public class ADD_ExperDto { //From CompanyManager public string FullName { get; set; } - public string UserName { get; set; } public string Password { get; set; } } } diff --git a/Hushian.Application/Services/AuthService.cs b/Hushian.Application/Services/AuthService.cs index 9338106..742c21b 100644 --- a/Hushian.Application/Services/AuthService.cs +++ b/Hushian.Application/Services/AuthService.cs @@ -26,14 +26,57 @@ namespace Hushian.Application.Services private readonly IGenericRepository _UserRepository; private readonly IGenericRepository _ExperRepository; private readonly VerificationService _verificationService; - public AuthService(IOptions jwtSettings) + public AuthService(IOptions jwtSettings, IGenericRepository companyRepository, IGenericRepository userRepository, IGenericRepository experRepository, VerificationService verificationService) { _jwtSettings = jwtSettings.Value; + _CompanyRepository = companyRepository; + _UserRepository = userRepository; + _ExperRepository = experRepository; + _verificationService = verificationService; } public async Task> AuthenticationFromCompanySide (AuthRequestFromCompanySide auth) { ResponseBase Response = new(); + if (auth.Username.StartsWith("09")) + { + // in Company Search + var Company= await _CompanyRepository.Get().FirstOrDefaultAsync(f=>f.Mobile== auth.Username && f.Password==auth.Password.GetHash()); + if (Company==null) + { + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Success = true; + Response.Value = new AuthResponse() + { + Fullname = Company.FullName, + Id = Company.ID, + MobileOrUserName = Company.Mobile, + Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(Company.Mobile, Company.ID)) + }; + } + } + else + { + var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash()); + if (exper == null) + { + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Success = true; + Response.Value = new AuthResponse() + { + Fullname = exper.FullName, + Id = exper.ID, + MobileOrUserName = exper.UserName, + Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID)) + }; + } + } return Response; } public async Task> AuthenticationFromUserSide diff --git a/Hushian.Application/Services/CompanyService.cs b/Hushian.Application/Services/CompanyService.cs new file mode 100644 index 0000000..71da8f1 --- /dev/null +++ b/Hushian.Application/Services/CompanyService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hushian.Application.Services +{ + public class CompanyService + { + } +} diff --git a/Hushian.Application/Services/ExperService.cs b/Hushian.Application/Services/ExperService.cs new file mode 100644 index 0000000..a384aff --- /dev/null +++ b/Hushian.Application/Services/ExperService.cs @@ -0,0 +1,57 @@ +using Common.Dtos.Exper; +using Hushian.Application.Contracts.Persistence; +using Hushian.Application.Models; +using Hushian.Application.Validation; +using Hushian.Domain.Entites; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hushian.Application.Services +{ + public class ExperService + { + private readonly IGenericRepository _CompanyRepository; + private readonly IGenericRepository _ExperRepository; + public async Task> ADDExper(ADD_ExperDto dto,int CompanyID) + { + var Response =new ResponseBase(); + + #region Validation + bool validate = true; + var errors = new List(); + + validate = dto.Password.CheckLawPassword(ref errors); + + #endregion + + if (validate) + { + try + { + string UserName = $"E/{CompanyID.ToString("00")}/{_ExperRepository.Get().Count(c => c.CompanyID == CompanyID).ToString("0000")}"; + Response.Success = Response.Value = await _ExperRepository.ADDBool(new Exper() + { + CompanyID = CompanyID, + FullName = dto.FullName, + Password = dto.Password.GetHash(), + UserName = UserName + }); + } + catch (Exception ex) + { + Response.Errors.Add("خطای سیستمی کد 01"); + } + + } + else + { + Response.Errors.AddRange(errors); + } + + return Response; + } + } +} diff --git a/Hushian.Application/Services/HashPassword.cs b/Hushian.Application/Services/HashPassword.cs new file mode 100644 index 0000000..5a7609b --- /dev/null +++ b/Hushian.Application/Services/HashPassword.cs @@ -0,0 +1,26 @@ +using Microsoft.AspNetCore.Cryptography.KeyDerivation; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using System.Threading.Tasks; + +namespace Hushian.Application.Services +{ + public static class HashPassword + { + + + public static string GetHash(this string Password) + { + string hashed = Convert.ToBase64String(KeyDerivation.Pbkdf2( + password: Password!, + salt: Encoding.ASCII.GetBytes("CGYzqejKH50&kjh(02**Id1Q"), + prf: KeyDerivationPrf.HMACSHA256, + iterationCount: 100000, + numBytesRequested: 256 / 8)); + return hashed; + } + } +} diff --git a/Hushian.Application/Services/VerificationService.cs b/Hushian.Application/Services/VerificationService.cs index 2cddfdd..26fe438 100644 --- a/Hushian.Application/Services/VerificationService.cs +++ b/Hushian.Application/Services/VerificationService.cs @@ -22,7 +22,16 @@ namespace Hushian.Application.Services private readonly IMessageSender _messageSender; private readonly IGenericRepository _UserRepository; private readonly AuthService _authService; - public async Task GenerateCodeForLoginUser(string Mobile) + + public VerificationService(IGenericRepository verificationCodeRepository, IMessageSender messageSender, IGenericRepository userRepository, AuthService authService) + { + _VerificationCodeRepository = verificationCodeRepository; + _messageSender = messageSender; + _UserRepository = userRepository; + _authService = authService; + } + + public async Task GenerateCodeForLoginUser(string Mobile) { string Code = await GenerateCode(); var response= await _VerificationCodeRepository.ADD diff --git a/Hushian.Application/Validation/FixedValidation.cs b/Hushian.Application/Validation/FixedValidation.cs new file mode 100644 index 0000000..f3d65cb --- /dev/null +++ b/Hushian.Application/Validation/FixedValidation.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Hushian.Application.Validation +{ + public static class FixedValidation + { + public static bool CheckLawPassword(this string newPassword,ref List errors) + { + return true; + } + } +}