|
@@ -19,6 +19,8 @@ using System.Data;
|
|
using MTWorkHR.Core.IDto;
|
|
using MTWorkHR.Core.IDto;
|
|
using System.Linq.Dynamic.Core;
|
|
using System.Linq.Dynamic.Core;
|
|
using MTWorkHR.Core.Entities.Base;
|
|
using MTWorkHR.Core.Entities.Base;
|
|
|
|
+using MTWorkHR.Infrastructure.EmailService;
|
|
|
|
+using Countries.NET.Database;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
namespace MTWorkHR.Application.Services
|
|
{
|
|
{
|
|
@@ -32,10 +34,11 @@ namespace MTWorkHR.Application.Services
|
|
private readonly IMailSender _emailSender;
|
|
private readonly IMailSender _emailSender;
|
|
private readonly GlobalInfo _globalInfo;
|
|
private readonly GlobalInfo _globalInfo;
|
|
private readonly IFileService _fileService;
|
|
private readonly IFileService _fileService;
|
|
|
|
+ private readonly IOTPService _oTPService;
|
|
|
|
|
|
public UserService(ApplicationUserManager userManager, IUnitOfWork unitOfWork
|
|
public UserService(ApplicationUserManager userManager, IUnitOfWork unitOfWork
|
|
, RoleManager<ApplicationRole> roleManager, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IMailSender emailSender
|
|
, RoleManager<ApplicationRole> roleManager, GlobalInfo globalInfo, AppSettingsConfiguration configuration, IMailSender emailSender
|
|
- , IUserRoleRepository<IdentityUserRole<string>> userRole, IFileService fileService)
|
|
|
|
|
|
+ , IUserRoleRepository<IdentityUserRole<string>> userRole, IFileService fileService, IOTPService oTPService)
|
|
{
|
|
{
|
|
_userManager = userManager;
|
|
_userManager = userManager;
|
|
_unitOfWork = unitOfWork;
|
|
_unitOfWork = unitOfWork;
|
|
@@ -45,7 +48,8 @@ namespace MTWorkHR.Application.Services
|
|
_emailSender = emailSender;
|
|
_emailSender = emailSender;
|
|
_globalInfo = globalInfo;
|
|
_globalInfo = globalInfo;
|
|
_fileService = fileService;
|
|
_fileService = fileService;
|
|
- }
|
|
|
|
|
|
+ _oTPService = oTPService;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
public async Task<UserDto> GetById(string id)
|
|
public async Task<UserDto> GetById(string id)
|
|
@@ -199,14 +203,22 @@ namespace MTWorkHR.Application.Services
|
|
//saving user
|
|
//saving user
|
|
var result = await _userManager.CreateAsync(user, input.Password);
|
|
var result = await _userManager.CreateAsync(user, input.Password);
|
|
if (!result.Succeeded)
|
|
if (!result.Succeeded)
|
|
|
|
+ {
|
|
|
|
+ if(result.Errors != null && result.Errors.Count() > 0)
|
|
|
|
+ {
|
|
|
|
+ var msg = result.Errors.Select(a => a.Description ).Aggregate((a,b) => a + " /r/n " + b);
|
|
|
|
+ throw new AppException(msg);
|
|
|
|
+ }
|
|
throw new AppException(ExceptionEnum.RecordCreationFailed);
|
|
throw new AppException(ExceptionEnum.RecordCreationFailed);
|
|
|
|
+ }
|
|
|
|
+
|
|
input.Id = user.Id;
|
|
input.Id = user.Id;
|
|
|
|
|
|
//saving userRoles
|
|
//saving userRoles
|
|
if(input.UserRoles == null || input.UserRoles.Count == 0)
|
|
if(input.UserRoles == null || input.UserRoles.Count == 0)
|
|
{
|
|
{
|
|
var employeeRole = await _roleManager.FindByNameAsync("Employee");
|
|
var employeeRole = await _roleManager.FindByNameAsync("Employee");
|
|
- if (employeeRole == null)
|
|
|
|
|
|
+ if (employeeRole != null)
|
|
{
|
|
{
|
|
await _userManager.AddToRoleAsync(user, "Employee");
|
|
await _userManager.AddToRoleAsync(user, "Employee");
|
|
}
|
|
}
|
|
@@ -354,7 +366,7 @@ namespace MTWorkHR.Application.Services
|
|
var result = await _userManager.VerifyUserTokenAsync(user, "Default", purpose, input.Token);
|
|
var result = await _userManager.VerifyUserTokenAsync(user, "Default", purpose, input.Token);
|
|
return !result;
|
|
return !result;
|
|
}
|
|
}
|
|
- public async Task<bool> ForgetPassword(ForgetPasswordDto model)
|
|
|
|
|
|
+ public async Task<bool> ForgetPassword1(ForgetPasswordDto model)
|
|
{
|
|
{
|
|
var user = await _userManager.Users.IgnoreQueryFilters().FirstOrDefaultAsync(x => x.Id == model.UserId);
|
|
var user = await _userManager.Users.IgnoreQueryFilters().FirstOrDefaultAsync(x => x.Id == model.UserId);
|
|
if (user == null)
|
|
if (user == null)
|
|
@@ -382,20 +394,26 @@ namespace MTWorkHR.Application.Services
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
- public async Task ForgetPasswordMail(string email)
|
|
|
|
|
|
+ public async Task ForgetPasswordMail1(string email)
|
|
{
|
|
{
|
|
|
|
|
|
var foundUser = await _userManager.FindByEmailAsync(email);
|
|
var foundUser = await _userManager.FindByEmailAsync(email);
|
|
if (foundUser != null)
|
|
if (foundUser != null)
|
|
{
|
|
{
|
|
- var resultPassReset = await GetResetPasswordURL(foundUser.Id);
|
|
|
|
|
|
+ var user = await _userManager.Users.FirstOrDefaultAsync(x => !x.IsDeleted && x.Id.Equals(foundUser.Id));
|
|
|
|
+ if (user == null)
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
+
|
|
|
|
+ string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);
|
|
|
|
+
|
|
|
|
+ // var resultPassReset = await GetResetPasswordURL(foundUser.Id);
|
|
|
|
|
|
await _emailSender.SendEmail(new EmailMessage
|
|
await _emailSender.SendEmail(new EmailMessage
|
|
{
|
|
{
|
|
Subject = "Register Confirmation",
|
|
Subject = "Register Confirmation",
|
|
- To = resultPassReset.Item2,
|
|
|
|
- Body = "Forget Your Password, link will expired after 24 hours",
|
|
|
|
- url = resultPassReset.Item1,
|
|
|
|
|
|
+ To = foundUser.Email,
|
|
|
|
+ Body = "Forget Your Password, your OTP is " + "1111" ,
|
|
|
|
+ url = "",
|
|
userId = foundUser.Id
|
|
userId = foundUser.Id
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -405,6 +423,47 @@ namespace MTWorkHR.Application.Services
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
|
|
+ public async Task<string> ForgetPasswordMail(string email) //Begin forget password
|
|
|
|
+ {
|
|
|
|
+ var foundUser = await _userManager.FindByEmailAsync(email);
|
|
|
|
+ if (foundUser != null)
|
|
|
|
+ {
|
|
|
|
+ string oneTimePassword = await _oTPService.RandomOneTimePassword(foundUser.Id);
|
|
|
|
+ await _oTPService.SentOTPByMail(foundUser.Id,foundUser.Email, oneTimePassword);
|
|
|
|
+ return foundUser.Id;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public async Task<bool> ForgetPassword(ForgetPasswordDto input)
|
|
|
|
+ {
|
|
|
|
+ var user = await _userManager.Users.IgnoreQueryFilters().FirstOrDefaultAsync(x => x.Id == input.UserId);
|
|
|
|
+ if (user == null)
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
+
|
|
|
|
+ if (!_oTPService.VerifyOTP(user.Id, input.OTP).Result)
|
|
|
|
+ throw new AppException(ExceptionEnum.WrongOTP);
|
|
|
|
+ string resetToken = await _userManager.GeneratePasswordResetTokenAsync(user);
|
|
|
|
+
|
|
|
|
+ var result = await _userManager.ResetPasswordAsync(user, resetToken, input.Password);
|
|
|
|
+ if (!result.Succeeded)
|
|
|
|
+ {
|
|
|
|
+ if (result.Errors != null && result.Errors.Count() > 0)
|
|
|
|
+ {
|
|
|
|
+ var msg = result.Errors.Select(a => a.Description).Aggregate((a, b) => a + " /r/n " + b);
|
|
|
|
+ throw new AppException(msg);
|
|
|
|
+ }
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordCreationFailed);
|
|
|
|
+ }
|
|
|
|
+ return result.Succeeded;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
public async Task StopUser(string userId)
|
|
public async Task StopUser(string userId)
|
|
{
|
|
{
|
|
var entity = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
|
var entity = await _userManager.Users.FirstOrDefaultAsync(x => x.Id == userId);
|