...
This commit is contained in:
@@ -28,6 +28,10 @@ namespace Back.Services
|
||||
|
||||
}
|
||||
public async Task<VerificationCode> GetVerificationCode(int ID)
|
||||
{
|
||||
return await _verificationCodeRepo.Get(w => w.Code == ID).FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<VerificationCode> GetVerificationCodeByID(int ID)
|
||||
{
|
||||
return await _verificationCodeRepo.Get(w => w.ID == ID).FirstOrDefaultAsync();
|
||||
}
|
||||
@@ -68,7 +72,7 @@ namespace Back.Services
|
||||
public async Task<int> GenerateCode(VerificationCode code)
|
||||
{
|
||||
code.Code = Random.Shared.Next(1000, 9000);
|
||||
while (await GetVerificationCode(code.ID) != null)
|
||||
while (await GetVerificationCode(code.Code) != null)
|
||||
code.Code = Random.Shared.Next(1000, 9000);
|
||||
|
||||
|
||||
|
@@ -100,11 +100,59 @@ namespace Back.Services
|
||||
|
||||
}
|
||||
}
|
||||
public async Task<bool> ExistMobileAndCompanyIsActive(string mobile)
|
||||
public async Task<bool> AddORUpdateCompanyBoolResult(Company item)
|
||||
{
|
||||
return await _repoCompany.GetAll().AnyAsync(w => w.Mobile == mobile && w.IsActive);
|
||||
|
||||
try
|
||||
{
|
||||
//SysLog log = new SysLog()
|
||||
//{
|
||||
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
||||
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
||||
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany",
|
||||
// Value = "*" + JsonConvert.SerializeObject(item),
|
||||
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
||||
// Type = "User"
|
||||
//};
|
||||
//_contextMongodb.InsertItem(log);
|
||||
|
||||
if (item.ID == null || item.ID == 0)
|
||||
{
|
||||
return await _repoCompany.AddBoolResultAsync(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
return await _repoCompany.UpdateAsync(item);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//SysLog log = new SysLog()
|
||||
//{
|
||||
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
||||
// Datetime = DateTime.Now.ConvertMiladiToShamsi(),
|
||||
// Method = $"{_httpContextAccessor.HttpContext.Request.Method}/{this.GetType().FullName}/AddORUpdateCompany",
|
||||
// Value = ex.Message,
|
||||
// Route = _httpContextAccessor.HttpContext.Request.Path,
|
||||
// Type = "catch"
|
||||
//};
|
||||
//_contextMongodb.InsertItem(log);
|
||||
return false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> ExistMobileInCompany(string mobile,bool IsActive=true)
|
||||
{
|
||||
var resquest= _repoCompany.GetAll().Where(w => w.Mobile == mobile );
|
||||
if (IsActive)
|
||||
resquest = resquest.Where(w => w.IsActive);
|
||||
|
||||
return await resquest.AnyAsync();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using Back.Common;
|
||||
using Back.Data.Contracts;
|
||||
using Back.Data.Infrastructure.Repository;
|
||||
using Back.Data.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
@@ -17,16 +18,19 @@ namespace Back.Services
|
||||
|
||||
private readonly servNotification _servNotification;
|
||||
private readonly IAsyncRepository<User> _RepoUser;
|
||||
private readonly RepositoryBase<Company> _RepoCompany;
|
||||
private readonly IAsyncRepository<PermissionPeriod> _RepoPermissionPeriod;
|
||||
public servUser(IConfiguration configuration
|
||||
, servNotification servNotification
|
||||
, IAsyncRepository<User> RepoUser
|
||||
, IAsyncRepository<PermissionPeriod> RepoPermissionPeriod)
|
||||
, IAsyncRepository<PermissionPeriod> RepoPermissionPeriod
|
||||
, RepositoryBase<Company> repoCompany)
|
||||
{
|
||||
_configuration = configuration;
|
||||
_servNotification = servNotification;
|
||||
_RepoUser = RepoUser;
|
||||
_RepoPermissionPeriod = RepoPermissionPeriod;
|
||||
_RepoCompany = repoCompany;
|
||||
}
|
||||
public async Task<User?> GetUserByUserNameAndPassword(string UserName, string Password)
|
||||
{
|
||||
@@ -37,30 +41,30 @@ namespace Back.Services
|
||||
// .ThenInclude(ti=>ti.Company)
|
||||
//.ThenInclude(ti => ti.PermissionPeriods)
|
||||
// .ThenInclude(ti => ti.Permission)
|
||||
.Include(ti=>ti.RolUsers)
|
||||
.Include(ti => ti.RolUsers)
|
||||
.ThenInclude(ti => ti.Company)
|
||||
//.ThenInclude(ti => ti.PermissionPeriods)
|
||||
// .ThenInclude(ti => ti.CalculationType)
|
||||
//.ThenInclude(ti => ti.PermissionPeriods)
|
||||
// .ThenInclude(ti => ti.CalculationType)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password="",bool newtoken=true)
|
||||
public async Task<UserAuthenticationDTO?> UserAuthentication(string UserNameORUserID, string Password = "", bool newtoken = true)
|
||||
{
|
||||
UserAuthenticationDTO ret = new UserAuthenticationDTO();
|
||||
User? user = null;
|
||||
|
||||
if (string.IsNullOrEmpty(Password) && int.TryParse(UserNameORUserID,out int UserID))
|
||||
if (string.IsNullOrEmpty(Password) && int.TryParse(UserNameORUserID, out int UserID))
|
||||
user = await GetUserByUserID(UserID);
|
||||
else
|
||||
user =await GetUserByUserNameAndPassword(UserNameORUserID, Password);
|
||||
user = await GetUserByUserNameAndPassword(UserNameORUserID, Password);
|
||||
|
||||
if (user == null)
|
||||
return null;
|
||||
|
||||
string Jwt_Lifetime_Minutes = await GetJwt_Lifetime_Minutes();
|
||||
ret.UserName = user.Username;
|
||||
ret.Token =newtoken ? await CerateToken(user.ID, user.Username, Jwt_Lifetime_Minutes) : user.Token;
|
||||
ret.Token = newtoken ? await CerateToken(user.ID, user.Username, Jwt_Lifetime_Minutes) : user.Token;
|
||||
ret.FullName = user.Fullname;
|
||||
ret.Photo = user.Photo==null ? null : Convert.ToBase64String(user.Photo);
|
||||
ret.Photo = user.Photo == null ? null : Convert.ToBase64String(user.Photo);
|
||||
//foreach (var rol in user.RolUsers)
|
||||
//{
|
||||
// if (!rol.Company.IsActive)
|
||||
@@ -132,8 +136,8 @@ namespace Back.Services
|
||||
|
||||
};
|
||||
var dt = newtoken ? DateTime.Now : user.DateLastLogin.ToMiladiByTime();
|
||||
ret.enterDate= dt;
|
||||
ret.exitDate= dt.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes));
|
||||
ret.enterDate = dt;
|
||||
ret.exitDate = dt.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes));
|
||||
return ret;
|
||||
}
|
||||
public async Task<User> AddUser(User item)
|
||||
@@ -151,9 +155,9 @@ namespace Back.Services
|
||||
}
|
||||
public async Task<bool> ExistUser(string UserName)
|
||||
{
|
||||
|
||||
return await _RepoUser.GetAll().AnyAsync(w=>w.Username==UserName);
|
||||
|
||||
|
||||
return await _RepoUser.GetAll().AnyAsync(w => w.Username == UserName);
|
||||
|
||||
}
|
||||
public async Task<User> GetUserByUsername(string UserName)
|
||||
{
|
||||
@@ -166,53 +170,53 @@ namespace Back.Services
|
||||
.ThenInclude(ti => ti.Company)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
public async Task SetTokenAndDateLogininDB(int UserID,string Token)
|
||||
public async Task SetTokenAndDateLogininDB(int UserID, string Token)
|
||||
{
|
||||
var user = await GetUserByUserID(UserID);
|
||||
if (user != null)
|
||||
{
|
||||
user.Token = Token;
|
||||
user.DateLastLogin=DateTime.Now.ConvertMiladiToShamsiByTime();
|
||||
await _RepoUser.UpdateAsync(user);
|
||||
user.DateLastLogin = DateTime.Now.ConvertMiladiToShamsiByTime();
|
||||
await _RepoUser.UpdateAsync(user);
|
||||
}
|
||||
}
|
||||
public async Task<bool> ChangePasswordByMobile(string mobile, string newpassword)
|
||||
{
|
||||
var user =await GetUserByUsername(mobile);
|
||||
var user = await GetUserByUsername(mobile);
|
||||
if (user == null)
|
||||
return false;
|
||||
user.Password = newpassword.encrypted();
|
||||
return await _RepoUser.UpdateAsync(user);
|
||||
return await _RepoUser.UpdateAsync(user);
|
||||
}
|
||||
public async Task<DashBoardDTO> GetDashBoard(int CompanyID,int UserID)
|
||||
public async Task<DashBoardDTO> GetDashBoard(int CompanyID, int UserID)
|
||||
{
|
||||
DashBoardDTO request=new DashBoardDTO();
|
||||
var period=await _RepoPermissionPeriod
|
||||
.Get(w=>w.CompanyID==CompanyID && (!w.IsLocked.HasValue || !w.IsLocked.Value)).ToListAsync();
|
||||
DashBoardDTO request = new DashBoardDTO();
|
||||
var period = await _RepoPermissionPeriod
|
||||
.Get(w => w.CompanyID == CompanyID && (!w.IsLocked.HasValue || !w.IsLocked.Value)).ToListAsync();
|
||||
foreach (var item in period)
|
||||
{
|
||||
request.AlistofServices.Add(new ServiceInDashBoardDTO
|
||||
{
|
||||
PermissionID= item.PermissionID,
|
||||
PermissionID = item.PermissionID,
|
||||
PermissionName = item.Permission.Title,
|
||||
CalTypeID= item.CalculationTypeID,
|
||||
CalTypeID = item.CalculationTypeID,
|
||||
CalTypeTitle = item.CalculationType.Title,
|
||||
Total = item.CalculationTypeID == 1 ? item.TotalAmount.ToString() :"",
|
||||
Total = item.CalculationTypeID == 1 ? item.TotalAmount.ToString() : "",
|
||||
Remaining = item.CalculationTypeID == 1 ? item.RemainingAmount.ToString()
|
||||
: item.CalculationTypeID== 2 ? "" : item.RemainingAmount.ToString().ShamciToFormatShamci()
|
||||
: item.CalculationTypeID == 2 ? "" : item.RemainingAmount.ToString().ShamciToFormatShamci()
|
||||
});
|
||||
}
|
||||
request.LastLoginDate= _RepoUser.Get(w=>w.ID==UserID).Select(s=>s.DateLastLogin).FirstOrDefault();
|
||||
if(!string.IsNullOrEmpty(request.LastLoginDate))
|
||||
request.LastLoginDate = _RepoUser.Get(w => w.ID == UserID).Select(s => s.DateLastLogin).FirstOrDefault();
|
||||
if (!string.IsNullOrEmpty(request.LastLoginDate))
|
||||
request.LastLoginDate.ShamciToFormatShamci();
|
||||
|
||||
var user = await GetUserByUserID(UserID);
|
||||
if (user.Mobile == user.Username)
|
||||
request.Warning.Add(new AlertDTO { Status=0,Message= "موبایل و نام کاربری بهتر است شبیه هم نباشند" });
|
||||
request.Warning.Add(new AlertDTO { Status = 0, Message = "موبایل و نام کاربری بهتر است شبیه هم نباشند" });
|
||||
if (user.Mobile.encrypted() == user.Password)
|
||||
request.Warning.Add(new AlertDTO { Status = 0, Message = "موبایل و کلمه عبور بهتر است شبیه هم نباشند" });
|
||||
var Company = user.RolUsers.Where(w=>w.CompanyID== CompanyID).Select(s=>s.Company).FirstOrDefault();
|
||||
if (Company!=null)
|
||||
var Company = user.RolUsers.Where(w => w.CompanyID == CompanyID).Select(s => s.Company).FirstOrDefault();
|
||||
if (Company != null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(Company.Email) || string.IsNullOrEmpty(Company.Phone)
|
||||
|| string.IsNullOrEmpty(Company.EconomicCode) || string.IsNullOrEmpty(Company.UniqeMemory)
|
||||
@@ -221,15 +225,15 @@ namespace Back.Services
|
||||
request.Warning.Add(new AlertDTO { Status = 0, Message = "بهتر است اطلاعات شرکت بروزرسانی شود" });
|
||||
}
|
||||
}
|
||||
var nots= await _servNotification.GetNotifications();
|
||||
var nots = await _servNotification.GetNotifications();
|
||||
if (nots.Any())
|
||||
request.Notifications= nots.Select(s=>new AlertDTO
|
||||
request.Notifications = nots.Select(s => new AlertDTO
|
||||
{
|
||||
Message=s.Message,
|
||||
Status=s.Type,
|
||||
Path=s.Path,
|
||||
ViewSize=s.ViewSize
|
||||
}).ToList();
|
||||
Message = s.Message,
|
||||
Status = s.Type,
|
||||
Path = s.Path,
|
||||
ViewSize = s.ViewSize
|
||||
}).ToList();
|
||||
return request;
|
||||
}
|
||||
public async Task<User> UpdateUser(User user)
|
||||
@@ -253,10 +257,54 @@ namespace Back.Services
|
||||
user.Password = newPass.encrypted();
|
||||
return await _RepoUser.UpdateAsync(user);
|
||||
}
|
||||
public async Task<bool> PermissionChangePassword(string oldPass,int UserID)
|
||||
public async Task<bool> ChangeUserName(string newUserName, int UserID)
|
||||
{
|
||||
return await _RepoUser.GetAll().AnyAsync(w => w.ID == UserID && w.Password==oldPass.encrypted() && w.IsActive);
|
||||
var user = await GetUserByUserID(UserID);
|
||||
if (user == null)
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
using var transaction = _RepoCompany._dbContext.Database.BeginTransaction();
|
||||
var company = user.RolUsers.First().Company;
|
||||
company.Mobile = newUserName;
|
||||
if (await _RepoCompany.UpdateAsync(company))
|
||||
{
|
||||
user.Username = newUserName;
|
||||
user.Mobile = newUserName;
|
||||
if (await _RepoUser.UpdateAsync(user))
|
||||
{
|
||||
transaction.Commit();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
transaction.Rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
// return Ok(await _servCompany.AddORUpdateCompanyBoolResult(company));
|
||||
}
|
||||
return false;
|
||||
|
||||
|
||||
|
||||
}
|
||||
public async Task<bool> PermissionChangePassword(string oldPass, int UserID)
|
||||
{
|
||||
return await _RepoUser.GetAll().AnyAsync(w => w.ID == UserID && w.Password == oldPass.encrypted() && w.IsActive);
|
||||
|
||||
}
|
||||
public async Task<bool> ExistMobileInUser(string mobile, bool IsActive = true)
|
||||
{
|
||||
var resquest = _RepoUser.Get(w => w.Mobile == mobile || w.Username == mobile);
|
||||
if (IsActive)
|
||||
resquest = resquest.Where(w => w.IsActive);
|
||||
|
||||
return await resquest.AnyAsync();
|
||||
}
|
||||
//--------internal
|
||||
private async Task<string> GetJwt_Lifetime_Minutes()
|
||||
@@ -286,7 +334,7 @@ namespace Back.Services
|
||||
|
||||
return Jwt_Lifetime_Minutes;
|
||||
}
|
||||
private async Task<string> CerateToken(int UserId, string UserName,string Jwt_Lifetime_Minutes)
|
||||
private async Task<string> CerateToken(int UserId, string UserName, string Jwt_Lifetime_Minutes)
|
||||
{
|
||||
#region CreateToken
|
||||
var securityKey = new SymmetricSecurityKey(
|
||||
@@ -303,10 +351,10 @@ namespace Back.Services
|
||||
Fixedvalues.Issuer, Fixedvalues.Audience, claimsForToken,
|
||||
DateTime.Now, DateTime.Now.AddMinutes(Convert.ToInt32(Jwt_Lifetime_Minutes)), signingCredentials);
|
||||
|
||||
|
||||
|
||||
string Token = new JwtSecurityTokenHandler()
|
||||
.WriteToken(jwtSecurityToke);
|
||||
await SetTokenAndDateLogininDB(UserId, Token);
|
||||
await SetTokenAndDateLogininDB(UserId, Token);
|
||||
//_contextMongodb.InsertItem(new SysLog()
|
||||
//{
|
||||
// TraceIdentifierID = _httpContextAccessor.HttpContext.TraceIdentifier,
|
||||
|
Reference in New Issue
Block a user