This commit is contained in:
mmrbnjd
2025-07-03 16:05:44 +03:30
parent bc65878608
commit 87d2360b32
8 changed files with 338 additions and 51 deletions

View File

@@ -13,7 +13,7 @@ namespace Common.Dtos.Company
public string? FullNameManager { get; set; }
public string? WebSite { get; set; }
public string? Email { get; set; }
public byte[]? img { get; set; }
public byte[]? logo { get; set; }
public bool Available { get; set; }
public bool allowBot { get; set; }
}

View File

@@ -0,0 +1,18 @@
using AutoMapper;
using Common.Dtos.Company;
using Hushian.Domain.Entites;
namespace Hushian.Application
{
public class MappingProfile : Profile
{
public MappingProfile(/*IUserService userService*/)
{
CreateMap<ReadANDUpdate_CompanyDto, Company>().ReverseMap(); ;
}
}

View File

@@ -9,6 +9,7 @@ namespace Hushian.Application.Models
public T? Value { get; set; }
public bool Success { get; set; }=false;
public List<string> Errors { get; set; }=new List<string>();
public List<string> Warning { get; set; }=new List<string>();
}
}

View File

@@ -4,6 +4,7 @@ using Common.Models.Auth.UserSide;
using Hushian.Application.Constants;
using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models;
using Hushian.Application.Validation;
using Hushian.Domain.Entites;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
@@ -38,65 +39,81 @@ namespace Hushian.Application.Services
(AuthRequestFromCompanySide auth)
{
ResponseBase<AuthResponse> Response = new();
if (auth.Username.StartsWith("09"))
if (!FixedValidation.CheckUsername(auth.Username))
{
// 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))
};
}
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash());
if (exper == null)
if (auth.Username.StartsWith("09"))
{
Response.Errors.Add("کاربری یافت نشد");
// 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
{
Response.Success = true;
Response.Value = new AuthResponse()
var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == auth.Username && f.Password == auth.Password.GetHash());
if (exper == null)
{
Fullname = exper.FullName,
Id = exper.ID,
MobileOrUserName = exper.UserName,
Token = new JwtSecurityTokenHandler().WriteToken(await GenerateToken(exper.UserName, exper.ID))
};
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<ResponseBase<int>> AuthenticationFromUserSide
(AuthRequestFromUserSide auth)
{
ResponseBase<int> Response = new();
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
if (!FixedValidation.CheckUsername(auth.Mobile))
{
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
Response.Errors.Add("نام کاربری اشتباه است");
}
else
{
if (!await _UserRepository.Get().AnyAsync(a => a.Mobile == auth.Mobile))
{
Response.Errors.Add("خطا در کاربری");
if (!await _UserRepository.ADDBool(new User() { Mobile = auth.Mobile, FullName = auth.FullName }))
{
Response.Errors.Add("خطا در کاربری");
}
}
if (Response.Errors.Count == 0)
{
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true;
}
}
if (Response.Errors.Count==0)
{
Response.Value = await _verificationService.GenerateCodeForLoginUser(auth.Mobile);
Response.Success = true;
}
return Response;
}

View File

@@ -1,12 +1,164 @@
using System;
using AutoMapper;
using Common.Dtos.Company;
using Hushian.Application.Contracts.Persistence;
using Hushian.Application.Models;
using Hushian.Application.Validation;
using Hushian.Domain.Entites;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace Hushian.Application.Services
{
public class CompanyService
public class CompanyService
{
private readonly IGenericRepository<Company> _CompanyRepository;
private readonly VerificationService _VerificationService;
private readonly IMapper _mapper;
public async Task<ResponseBase<int>> RegisterCompany(RegisterCompanyDto dto)
{
ResponseBase<int> Response = new();
var newCompany = await NewCompany(dto);
if (newCompany.Success)
{
var id = await Verifi(dto.Mobile);
Response.Value = id;
Response.Success = id != 0;
}
else Response.Errors.AddRange(newCompany.Errors);
return Response;
}
public async Task<ReadANDUpdate_CompanyDto> GETCompanyinformation(int CompanyID)
{
var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID);
return _mapper.Map<ReadANDUpdate_CompanyDto>(company);
}
public async Task<ResponseBase<bool>> EditCompany(ReadANDUpdate_CompanyDto model, int CompanyID)
{
ResponseBase<bool> Response = new();
if (!string.IsNullOrEmpty(model.FullNameManager) && !model.FullNameManager.IsOnlyPersianLetters())
Response.Errors.Add("نام مدیر باید کاملا به صورت فارسی باشد");
else if (!string.IsNullOrEmpty(model.FullName) && !model.FullName.IsOnlyPersianLetters())
Response.Errors.Add("نام شرکت باید کاملا به صورت فارسی باشد");
else if (!string.IsNullOrEmpty(model.Email) && !model.Email.IsValidEmail())
Response.Errors.Add("پست الکترونیکی صحیح نمی باشد");
else if (!string.IsNullOrEmpty(model.WebSite) && !model.WebSite.IsValidWebsite())
Response.Errors.Add("وب سایت صحیح نمی باشد");
else if (model.logo != null && !model.logo.IsValidImage())
Response.Errors.Add("تصویر نمی تواند از 5 مگ بیشتر باشد");
else
{
try
{
var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID);
company.FullNameManager = model.FullNameManager;
company.FullName = model.FullName;
company.Email = model.Email;
company.WebSite = model.WebSite;
company.logo = model.logo;
if (await _CompanyRepository.UPDATEBool(company))
{
Response.Success =Response.Value= true;
}
else
{
Response.Errors.Add("خطا در ذخیره سازی");
}
}
catch (Exception)
{
Response.Errors.Add("خطا سیستمی 02");
}
}
return Response;
}
public async Task<bool> ChangeAvailableCompany(bool @checked,int CompanyID)
{
var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID);
company.Available = @checked;
return await _CompanyRepository.UPDATEBool(company);
}
public async Task<bool> ChangeallowBotCompany(bool @checked, int CompanyID)
{
var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID);
company.allowBot = @checked;
return await _CompanyRepository.UPDATEBool(company);
}
private async Task<ResponseBase<bool>> NewCompany(RegisterCompanyDto dto)
{
ResponseBase<bool> Response = new();
List<string> Errors = new List<string>();
if (!dto.Mobile.CheckMobile())
{
Response.Errors.Add("فرمت موبایل صحیح نمی باشد");
}
else if (!dto.Password.CheckLawPassword(ref Errors))
{
Response.Errors.AddRange(Errors);
}
else if (!dto.FullName.IsOnlyPersianLetters())
{
Response.Errors.Add("نام باید کاملا به صورت فارسی باشد");
}
else
{
var AnyCompany = await _CompanyRepository.Get().FirstOrDefaultAsync(a => a.Mobile == dto.Mobile);
if (AnyCompany != null && AnyCompany.Verified)
{
Response.Errors.Add($"موبایل {dto.Mobile} در سیستم ثبت شده");
}
else
{
if (AnyCompany != null && !AnyCompany.Verified)
{
AnyCompany.Password = dto.Password.GetHash();
AnyCompany.FullName = dto.FullName;
AnyCompany.Cdatetime = DateTime.Now;
if (await _CompanyRepository.UPDATEBool(AnyCompany))
{
Response.Success = true;
}
else
{
Response.Errors.Add("خطا در ذخیره سازی اطلاعات کاربر");
}
}
else
{
if (await _CompanyRepository.ADDBool(new Company()
{
}))
{
Response.Success = true;
}
else
{
Response.Errors.Add("خطا در ذخیره سازی اطلاعات کاربر");
}
}
}
}
return Response;
}
private async Task<int> Verifi(string Mobile) => await _VerificationService.GenerateCodeByPhoneNumberConfirmed(Mobile);
}
}

View File

@@ -11,6 +11,7 @@ using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
@@ -19,16 +20,18 @@ namespace Hushian.Application.Services
public class VerificationService
{
private readonly IGenericRepository<VerificationCode> _VerificationCodeRepository;
private readonly IGenericRepository<Company> _CompanyRepository;
private readonly IMessageSender _messageSender;
private readonly IGenericRepository<User> _UserRepository;
private readonly AuthService _authService;
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository, IMessageSender messageSender, IGenericRepository<User> userRepository, AuthService authService)
public VerificationService(IGenericRepository<VerificationCode> verificationCodeRepository, IMessageSender messageSender, IGenericRepository<User> userRepository, AuthService authService, IGenericRepository<Company> companyRepository)
{
_VerificationCodeRepository = verificationCodeRepository;
_messageSender = messageSender;
_UserRepository = userRepository;
_authService = authService;
_CompanyRepository = companyRepository;
}
public async Task<int> GenerateCodeForLoginUser(string Mobile)
@@ -43,6 +46,18 @@ namespace Hushian.Application.Services
});
return response.ID;
}
public async Task<int> GenerateCodeByPhoneNumberConfirmed(string Mobile)
{
string Code = await GenerateCode();
var response = await _VerificationCodeRepository.ADD
(new Identity.Models.VerificationCode(VerificationCodeType.PhoneNumberConfirmed, Code, Mobile));
await _messageSender.SendMassage(new Models.Message.Message()
{
msg = Code,
To = Mobile
});
return response.ID;
}
public async Task<ResponseBase<AuthResponse>> VerificationCode(ConfirmedCodeDto model)
{
var response = new ResponseBase<AuthResponse>();
@@ -80,7 +95,20 @@ namespace Hushian.Application.Services
}
else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed)
{
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;
response.Value = new AuthResponse();
}
else
{
response.Errors.Add("خطای سیستمی در احراز");
}
}
}
else
{
@@ -98,6 +126,22 @@ namespace Hushian.Application.Services
}
public async Task<bool> ReSendCode(int ID)
{
var model=await _VerificationCodeRepository.Get().FirstOrDefaultAsync(f=>f.ID==ID);
if (model==null)
{
return false;
}
else
{
return await _messageSender.SendMassage(new Models.Message.Message()
{
msg = model.Code,
To = model.Mobile
});
}
}
private async Task<string> GenerateCode()
{

View File

@@ -1,16 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
namespace Hushian.Application.Validation
{
public static class FixedValidation
public static class FixedValidation
{
public static bool CheckLawPassword(this string newPassword,ref List<string> errors)
public static bool CheckLawPassword(this string password, ref List<string> errors)
{
return true;
bool ret = true;
if (password.Length != 6)
{
errors.Add("کلمه عبور باید حداقل 6 گاراکتر باشد");
ret = false;
}
// بررسی شامل بودن حداقل یک حرف
if (!Regex.IsMatch(password, "[a-zA-Z]"))
{
errors.Add("کلمه عبور باید حداقل شامل یک حرف انگلیسی بزرگ یا کوچک باشد");
ret = false;
}
// بررسی شامل بودن حداقل یک عدد
if (!Regex.IsMatch(password, "[0-9]"))
{
errors.Add("کلمه عبور باید حداقل شامل یک عدد باشد");
ret = false;
}
// بررسی اینکه فقط حروف و اعداد انگلیسی باشد
if(Regex.IsMatch(password, "^[a-zA-Z0-9]+$"))
{
errors.Add("کلمه عبور فقط عدد و حروف انگلیسی مجاز است");
ret = false;
}
return ret;
}
public static bool CheckUsername(this string Username)
=> (Username.Length == 11 && Username.StartsWith("09"))
|| (Username.Length == 9 && Username.StartsWith("E/"));
public static bool CheckMobile(this string Mobile) => Mobile.Length == 11 && Mobile.StartsWith("09");
public static bool IsValidEmail(this string email)
{
// الگوی ساده اما معتبر برای بررسی ایمیل
string pattern = @"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
return Regex.IsMatch(email, pattern);
}
public static bool IsOnlyPersianLetters(this string input)
{
// این الگو فقط اجازه حروف فارسی می‌دهد
string pattern = @"^[\u0600-\u06FF\s]+$";
return Regex.IsMatch(input, pattern);
}
public static bool IsValidWebsite(this string url)
{
// پروتکل اختیاری است، فقط باید دامنه درست باشد
string pattern = @"^(https?:\/\/)?(www\.)?[a-zA-Z0-9\-]+\.[a-zA-Z]{2,}(\S*)?$";
return Regex.IsMatch(url, pattern);
}
public static bool IsValidImage(this byte[] imageData, int maxSizeInBytes = 5 * 1024 * 1024)
{
// بررسی خالی بودن یا حجم بیش از حد
return imageData.Length <= maxSizeInBytes;
}
}
}

View File

@@ -25,6 +25,7 @@ namespace Hushian.Domain.Entites
public byte[]? logo { get; set; }
public bool Available { get; set; } = true;
public bool allowBot { get; set; } = true;
public bool Verified { get; set; } = false;
#endregion
#region Navigation