2025-06-29 15:29:51 +03:30
|
|
|
|
using Common.Contracts.Infrastructure;
|
|
|
|
|
using Common.Dtos.Verification;
|
|
|
|
|
using Common.Enums;
|
|
|
|
|
using Common.Models.Auth;
|
|
|
|
|
using Hushian.Application.Contracts.Persistence;
|
|
|
|
|
using Hushian.Application.Models;
|
2025-07-12 21:33:44 +03:30
|
|
|
|
using Common.Validation;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
using Hushian.Domain.Entites;
|
|
|
|
|
using Identity.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IdentityModel.Tokens.Jwt;
|
2025-07-24 23:18:11 +03:30
|
|
|
|
using Hushian.Application.Models.Message;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
|
|
|
|
|
namespace Hushian.Application.Services
|
|
|
|
|
{
|
|
|
|
|
public class VerificationService
|
|
|
|
|
{
|
2025-07-24 23:18:11 +03:30
|
|
|
|
private readonly JwtSettings _jwtSettings;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
|
2025-07-03 16:05:44 +03:30
|
|
|
|
private readonly IGenericRepository<Company> _CompanyRepository;
|
2025-07-03 16:38:14 +03:30
|
|
|
|
private readonly IGenericRepository<Exper> _ExperRepository;
|
2025-06-29 15:29:51 +03:30
|
|
|
|
private readonly IMessageSender _messageSender;
|
|
|
|
|
private readonly IGenericRepository<User> _UserRepository;
|
2025-06-29 16:14:42 +03:30
|
|
|
|
|
2025-07-07 22:04:07 +03:30
|
|
|
|
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository
|
|
|
|
|
, IMessageSender messageSender
|
|
|
|
|
, IGenericRepository<User> userRepository
|
|
|
|
|
, IGenericRepository<Company> companyRepository
|
2025-07-24 23:18:11 +03:30
|
|
|
|
, IGenericRepository<Exper> experRepository
|
|
|
|
|
, IOptions<JwtSettings> jwtSettings)
|
2025-06-29 16:14:42 +03:30
|
|
|
|
{
|
|
|
|
|
_VerificationCodeRepository = verificationCodeRepository;
|
|
|
|
|
_messageSender = messageSender;
|
|
|
|
|
_UserRepository = userRepository;
|
2025-07-03 16:05:44 +03:30
|
|
|
|
_CompanyRepository = companyRepository;
|
2025-07-03 16:38:14 +03:30
|
|
|
|
_ExperRepository = experRepository;
|
2025-07-24 23:18:11 +03:30
|
|
|
|
_jwtSettings = jwtSettings.Value;
|
2025-06-29 16:14:42 +03:30
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> GenerateCodeForLoginUser(string Mobile)
|
2025-06-29 15:29:51 +03:30
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response= await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.Login, Code, Mobile));
|
2025-08-20 14:15:10 +03:30
|
|
|
|
await _messageSender.SendMessageVerification(Mobile, Code);
|
2025-06-29 15:29:51 +03:30
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-07-03 16:05:44 +03:30
|
|
|
|
public async Task<int> GenerateCodeByPhoneNumberConfirmed(string Mobile)
|
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response = await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.PhoneNumberConfirmed, Code, Mobile));
|
2025-08-20 14:15:10 +03:30
|
|
|
|
await _messageSender.SendMessageVerification(Mobile, Code);
|
2025-07-03 16:05:44 +03:30
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-07-03 16:38:14 +03:30
|
|
|
|
public async Task<int> GenerateCodeByForgetPassword(string Mobile)
|
|
|
|
|
{
|
|
|
|
|
string Code = await GenerateCode();
|
|
|
|
|
var response = await _VerificationCodeRepository.ADD
|
|
|
|
|
(new Identity.Models.VerificationCode(VerificationCodeType.ForgetPassword, Code, Mobile));
|
2025-08-20 14:15:10 +03:30
|
|
|
|
await _messageSender.SendMessageVerification(Mobile, Code);
|
2025-07-03 16:38:14 +03:30
|
|
|
|
return response.ID;
|
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
public async Task<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model)
|
|
|
|
|
{
|
|
|
|
|
var response = new ResponseBase<AuthResponse>();
|
|
|
|
|
var resultConf=await _VerificationCodeRepository.Get()
|
|
|
|
|
.FirstOrDefaultAsync(w => w.ID == model.Id && w.Code == model.code && w.Type == model.codeType);
|
|
|
|
|
if (resultConf!=null)
|
|
|
|
|
{
|
|
|
|
|
if (resultConf.Type == VerificationCodeType.Login)
|
|
|
|
|
{
|
|
|
|
|
var User= await _UserRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile);
|
|
|
|
|
if (User!=null)
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse()
|
|
|
|
|
{
|
|
|
|
|
Fullname = User.FullName,
|
|
|
|
|
Id = User.ID,
|
|
|
|
|
MobileOrUserName = User.Mobile,
|
2025-07-11 20:37:28 +03:30
|
|
|
|
Role="User",
|
2025-07-24 23:18:11 +03:30
|
|
|
|
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(User.Mobile, User.ID, "User"))
|
2025-06-29 15:29:51 +03:30
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("کاربری یافت نشد");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (resultConf.Type == VerificationCodeType.ForgetPassword)
|
|
|
|
|
{
|
2025-07-03 16:38:14 +03:30
|
|
|
|
List<string> errors = new();
|
|
|
|
|
if (model.value.CheckLawPassword(ref errors))
|
|
|
|
|
response.Errors.AddRange(errors);
|
2025-06-29 15:29:51 +03:30
|
|
|
|
else
|
|
|
|
|
{
|
2025-07-03 16:38:14 +03:30
|
|
|
|
if (resultConf.Mobile.StartsWith("09"))
|
|
|
|
|
{
|
|
|
|
|
var anyCompany = await _CompanyRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile);
|
|
|
|
|
anyCompany.Password = model.value.GetHash();
|
|
|
|
|
anyCompany.Verified = true;
|
|
|
|
|
if (await _CompanyRepository.UPDATEBool(anyCompany))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var anyexper=await _ExperRepository.Get().FirstOrDefaultAsync(w => w.UserName == resultConf.Mobile);
|
|
|
|
|
anyexper.Password = model.value.GetHash();
|
|
|
|
|
if (await _ExperRepository.UPDATEBool(anyexper))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
|
|
|
|
response.Value = new AuthResponse();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed)
|
|
|
|
|
{
|
2025-07-03 16:05:44 +03:30
|
|
|
|
var anyCompany=await _CompanyRepository.Get().FirstOrDefaultAsync(w => w.Mobile == resultConf.Mobile && !w.Verified);
|
|
|
|
|
if (anyCompany!=null)
|
|
|
|
|
{
|
|
|
|
|
anyCompany.Verified = true;
|
|
|
|
|
if(await _CompanyRepository.UPDATEBool(anyCompany))
|
|
|
|
|
{
|
|
|
|
|
response.Success = true;
|
2025-07-24 11:35:39 +03:30
|
|
|
|
response.Value = new AuthResponse()
|
|
|
|
|
{
|
|
|
|
|
Fullname = anyCompany.FullName,
|
|
|
|
|
Id = anyCompany.ID,
|
|
|
|
|
Role = "Company",
|
|
|
|
|
img = anyCompany.logo,
|
|
|
|
|
MobileOrUserName = anyCompany.Mobile,
|
2025-07-24 23:18:11 +03:30
|
|
|
|
Token = new JwtSecurityTokenHandler().WriteToken(_jwtSettings.GenerateToken(anyCompany.Mobile, anyCompany.ID, "Company"))
|
2025-07-24 11:35:39 +03:30
|
|
|
|
};
|
2025-07-03 16:05:44 +03:30
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("خطای سیستمی در احراز");
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("احراز صحیح نمی باشد");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _VerificationCodeRepository.DELETE(resultConf);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
response.Errors.Add("احراز یافت نشد");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-07-03 16:05:44 +03:30
|
|
|
|
}
|
|
|
|
|
public async Task<bool> ReSendCode(int ID)
|
|
|
|
|
{
|
|
|
|
|
var model=await _VerificationCodeRepository.Get().FirstOrDefaultAsync(f=>f.ID==ID);
|
|
|
|
|
if (model==null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2025-08-20 14:15:10 +03:30
|
|
|
|
return await _messageSender.SendMessageVerification(model.Mobile, model.Code);
|
2025-07-03 16:05:44 +03:30
|
|
|
|
}
|
2025-06-29 15:29:51 +03:30
|
|
|
|
}
|
|
|
|
|
private async Task<string> GenerateCode()
|
|
|
|
|
{
|
|
|
|
|
int Code = Random.Shared.Next(1000, 9000);
|
|
|
|
|
while (await _VerificationCodeRepository.Get().AnyAsync(w => w.Code == Code.ToString()))
|
|
|
|
|
Code = Random.Shared.Next(1000, 9000);
|
|
|
|
|
|
|
|
|
|
return Code.ToString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|