diff --git a/Common/Dtos/Exper/ChangePassword_ExperDto.cs b/Common/Dtos/ChangePasswordDto.cs similarity index 61% rename from Common/Dtos/Exper/ChangePassword_ExperDto.cs rename to Common/Dtos/ChangePasswordDto.cs index f464f9c..39afeba 100644 --- a/Common/Dtos/Exper/ChangePassword_ExperDto.cs +++ b/Common/Dtos/ChangePasswordDto.cs @@ -4,12 +4,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace Common.Dtos.Exper +namespace Common.Dtos { - public class ChangePassword_ExperDto + public class ChangePasswordDto { - //From User And CompanyManager - public int ID { get; set; } public string? OldPassWord { get; set; } public string NewPassWord { get; set; } } diff --git a/Hushian.Application/Services/CompanyService.cs b/Hushian.Application/Services/CompanyService.cs index 98fcf2f..a484eae 100644 --- a/Hushian.Application/Services/CompanyService.cs +++ b/Hushian.Application/Services/CompanyService.cs @@ -1,5 +1,7 @@ using AutoMapper; +using Common.Dtos; using Common.Dtos.Company; +using Common.Dtos.Exper; using Hushian.Application.Contracts.Persistence; using Hushian.Application.Models; using Hushian.Application.Validation; @@ -99,6 +101,41 @@ namespace Hushian.Application.Services company.allowBot = @checked; return await _CompanyRepository.UPDATEBool(company); } + public async Task> ChangePasswordCompany(ChangePasswordDto model, int CompanyID) + { + ResponseBase Response = new(); + + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.ID == CompanyID); + if (string.IsNullOrEmpty(model.OldPassWord) || model.OldPassWord.GetHash()!=company.Password) + { + Response.Errors.Add("کلمه عبور فعلی صحیح نمی باشد"); + } + else + { + company.Password = model.NewPassWord.GetHash(); + Response.Value= await _CompanyRepository.UPDATEBool(company); + if (!Response.Value) Response.Errors.Add("خطا در ذخیره سازی"); + else Response.Success = true; + } + return Response; + } + public async Task> ForgetPasswordCompany(string Mobile) + { + ResponseBase Response = new(); + + var company = await _CompanyRepository.Get().FirstOrDefaultAsync(f => f.Mobile == Mobile); + if (company==null) + { + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Value= await _VerificationService.GenerateCodeByForgetPassword(Mobile); + Response.Success = true; + + } + return Response; + } private async Task> NewCompany(RegisterCompanyDto dto) { ResponseBase Response = new(); diff --git a/Hushian.Application/Services/ExperService.cs b/Hushian.Application/Services/ExperService.cs index a384aff..fd84708 100644 --- a/Hushian.Application/Services/ExperService.cs +++ b/Hushian.Application/Services/ExperService.cs @@ -1,8 +1,10 @@ -using Common.Dtos.Exper; +using Common.Dtos; +using Common.Dtos.Exper; 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; @@ -15,16 +17,17 @@ namespace Hushian.Application.Services { private readonly IGenericRepository _CompanyRepository; private readonly IGenericRepository _ExperRepository; - public async Task> ADDExper(ADD_ExperDto dto,int CompanyID) + private readonly VerificationService _VerificationService; + public async Task> ADDExper(ADD_ExperDto dto, int CompanyID) { - var Response =new ResponseBase(); + var Response = new ResponseBase(); #region Validation bool validate = true; var errors = new List(); validate = dto.Password.CheckLawPassword(ref errors); - + #endregion if (validate) @@ -53,5 +56,63 @@ namespace Hushian.Application.Services return Response; } + //Get Info + // Update + public async Task> ChangePasswordExperFromExper(ChangePasswordDto model, int ExperID) + { + ResponseBase Response = new(); + + var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.ID == ExperID); + if (string.IsNullOrEmpty(model.OldPassWord) || model.OldPassWord.GetHash() != exper.Password) + { + Response.Errors.Add("کلمه عبور فعلی صحیح نمی باشد"); + } + else + { + exper.Password = model.NewPassWord.GetHash(); + Response.Value = await _ExperRepository.UPDATEBool(exper); + if (!Response.Value) Response.Errors.Add("خطا در ذخیره سازی"); + else Response.Success = true; + + } + return Response; + } + public async Task> ChangePasswordExperFromCompanyManaget(ChangePasswordDto model, int ExperID, int CompanyID) + { + ResponseBase Response = new(); + var exper = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.ID == ExperID); + + if (exper.CompanyID == CompanyID) + { + exper.Password = model.NewPassWord.GetHash(); + Response.Value = await _ExperRepository.UPDATEBool(exper); + if (!Response.Value) Response.Errors.Add("خطا در ذخیره سازی"); + else Response.Success = true; + } + else + { + Response.Errors.Add("کاربری یافت نشد"); + } + + + + return Response; + } + public async Task> ForgetPasswordExper(string UserName) + { + ResponseBase Response = new(); + + var company = await _ExperRepository.Get().FirstOrDefaultAsync(f => f.UserName == UserName); + if (company == null) + { + Response.Errors.Add("کاربری یافت نشد"); + } + else + { + Response.Value = await _VerificationService.GenerateCodeByForgetPassword(UserName); + Response.Success = true; + } + return Response; + } } } diff --git a/Hushian.Application/Services/VerificationService.cs b/Hushian.Application/Services/VerificationService.cs index 98bb99f..a2d1b03 100644 --- a/Hushian.Application/Services/VerificationService.cs +++ b/Hushian.Application/Services/VerificationService.cs @@ -4,6 +4,7 @@ using Common.Enums; using Common.Models.Auth; using Hushian.Application.Contracts.Persistence; using Hushian.Application.Models; +using Hushian.Application.Validation; using Hushian.Domain.Entites; using Identity.Models; using Microsoft.EntityFrameworkCore; @@ -21,17 +22,19 @@ namespace Hushian.Application.Services { private readonly IGenericRepository _VerificationCodeRepository; private readonly IGenericRepository _CompanyRepository; + private readonly IGenericRepository _ExperRepository; private readonly IMessageSender _messageSender; private readonly IGenericRepository _UserRepository; private readonly AuthService _authService; - public VerificationService(IGenericRepository verificationCodeRepository, IMessageSender messageSender, IGenericRepository userRepository, AuthService authService, IGenericRepository companyRepository) + public VerificationService(IGenericRepository verificationCodeRepository, IMessageSender messageSender, IGenericRepository userRepository, AuthService authService, IGenericRepository companyRepository, IGenericRepository experRepository) { _VerificationCodeRepository = verificationCodeRepository; _messageSender = messageSender; _UserRepository = userRepository; _authService = authService; _CompanyRepository = companyRepository; + _ExperRepository = experRepository; } public async Task GenerateCodeForLoginUser(string Mobile) @@ -58,6 +61,18 @@ namespace Hushian.Application.Services }); return response.ID; } + public async Task GenerateCodeByForgetPassword(string Mobile) + { + string Code = await GenerateCode(); + var response = await _VerificationCodeRepository.ADD + (new Identity.Models.VerificationCode(VerificationCodeType.ForgetPassword, Code, Mobile)); + await _messageSender.SendMassage(new Models.Message.Message() + { + msg = Code, + To = Mobile + }); + return response.ID; + } public async Task> VerificationCode(ConfirmedCodeDto model) { var response = new ResponseBase(); @@ -86,11 +101,41 @@ namespace Hushian.Application.Services } else if (resultConf.Type == VerificationCodeType.ForgetPassword) { - if ((string.IsNullOrEmpty(model.value) || model.value.Length < 5)) - response.Errors.Add("کلمه عبور باید بیشتر از 5 کاراکتر باشد"); + List errors = new(); + if (model.value.CheckLawPassword(ref errors)) + response.Errors.AddRange(errors); else { - + 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("خطای سیستمی در احراز"); + } + } + } } else if (resultConf.Type == VerificationCodeType.PhoneNumberConfirmed)