This commit is contained in:
mmrbnjd
2025-06-29 16:14:42 +03:30
parent 86de5d3e39
commit bc65878608
7 changed files with 165 additions and 3 deletions

View File

@@ -9,7 +9,6 @@ namespace Common.Dtos.Exper
public class ADD_ExperDto public class ADD_ExperDto
{ //From CompanyManager { //From CompanyManager
public string FullName { get; set; } public string FullName { get; set; }
public string UserName { get; set; }
public string Password { get; set; } public string Password { get; set; }
} }
} }

View File

@@ -26,14 +26,57 @@ namespace Hushian.Application.Services
private readonly IGenericRepository<User> _UserRepository; private readonly IGenericRepository<User> _UserRepository;
private readonly IGenericRepository<Exper> _ExperRepository; private readonly IGenericRepository<Exper> _ExperRepository;
private readonly VerificationService _verificationService; private readonly VerificationService _verificationService;
public AuthService(IOptions<JwtSettings> jwtSettings) public AuthService(IOptions<JwtSettings> jwtSettings, IGenericRepository<Company> companyRepository, IGenericRepository<User> userRepository, IGenericRepository<Exper> experRepository, VerificationService verificationService)
{ {
_jwtSettings = jwtSettings.Value; _jwtSettings = jwtSettings.Value;
_CompanyRepository = companyRepository;
_UserRepository = userRepository;
_ExperRepository = experRepository;
_verificationService = verificationService;
} }
public async Task<ResponseBase<AuthResponse>> AuthenticationFromCompanySide public async Task<ResponseBase<AuthResponse>> AuthenticationFromCompanySide
(AuthRequestFromCompanySide auth) (AuthRequestFromCompanySide auth)
{ {
ResponseBase<AuthResponse> Response = new(); ResponseBase<AuthResponse> 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; return Response;
} }
public async Task<ResponseBase<int>> AuthenticationFromUserSide public async Task<ResponseBase<int>> AuthenticationFromUserSide

View File

@@ -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
{
}
}

View File

@@ -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<Company> _CompanyRepository;
private readonly IGenericRepository<Exper> _ExperRepository;
public async Task<ResponseBase<bool>> ADDExper(ADD_ExperDto dto,int CompanyID)
{
var Response =new ResponseBase<bool>();
#region Validation
bool validate = true;
var errors = new List<string>();
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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -22,7 +22,16 @@ namespace Hushian.Application.Services
private readonly IMessageSender _messageSender; private readonly IMessageSender _messageSender;
private readonly IGenericRepository<User> _UserRepository; private readonly IGenericRepository<User> _UserRepository;
private readonly AuthService _authService; private readonly AuthService _authService;
public async Task<int> GenerateCodeForLoginUser(string Mobile)
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository, IMessageSender messageSender, IGenericRepository<User> userRepository, AuthService authService)
{
_VerificationCodeRepository = verificationCodeRepository;
_messageSender = messageSender;
_UserRepository = userRepository;
_authService = authService;
}
public async Task<int> GenerateCodeForLoginUser(string Mobile)
{ {
string Code = await GenerateCode(); string Code = await GenerateCode();
var response= await _VerificationCodeRepository.ADD var response= await _VerificationCodeRepository.ADD

View File

@@ -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<string> errors)
{
return true;
}
}
}