|
@@ -13,6 +13,9 @@ using MTWorkHR.Core.Email;
|
|
using MTWorkHR.Core.Entities;
|
|
using MTWorkHR.Core.Entities;
|
|
using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
using MTWorkHR.Infrastructure.UnitOfWorks;
|
|
using MTWorkHR.Infrastructure.Entities;
|
|
using MTWorkHR.Infrastructure.Entities;
|
|
|
|
+using static Org.BouncyCastle.Crypto.Engines.SM2Engine;
|
|
|
|
+using System.Web;
|
|
|
|
+using System.Data;
|
|
|
|
|
|
namespace MTWorkHR.Application.Services
|
|
namespace MTWorkHR.Application.Services
|
|
{
|
|
{
|
|
@@ -119,15 +122,26 @@ namespace MTWorkHR.Application.Services
|
|
input.Id = user.Id;
|
|
input.Id = user.Id;
|
|
|
|
|
|
//saving userRoles
|
|
//saving userRoles
|
|
- var userRoles = MapperObject.Mapper.Map<List<IdentityUserRole<string>>>(input.UserRoles);
|
|
|
|
- foreach (var role in userRoles)
|
|
|
|
|
|
+ if(input.UserRoles == null || input.UserRoles.Count == 0)
|
|
{
|
|
{
|
|
- role.UserId = user.Id;
|
|
|
|
- if (await _roleManager.FindByIdAsync(role.RoleId) == null)
|
|
|
|
- throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
- var roleOb = input.UserRoles?.FirstOrDefault(r => r.RoleId == role.RoleId);
|
|
|
|
- var roleName = roleOb != null ? roleOb.RoleName : "Employee";
|
|
|
|
- await _userManager.AddToRoleAsync(user, roleName);
|
|
|
|
|
|
+ var employeeRole = await _roleManager.FindByNameAsync("Employee");
|
|
|
|
+ if (employeeRole == null)
|
|
|
|
+ {
|
|
|
|
+ await _userManager.AddToRoleAsync(user, "Employee");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ var userRoles = MapperObject.Mapper.Map<List<IdentityUserRole<string>>>(input.UserRoles);
|
|
|
|
+ foreach (var role in userRoles)
|
|
|
|
+ {
|
|
|
|
+ role.UserId = user.Id;
|
|
|
|
+ if (await _roleManager.FindByIdAsync(role.RoleId) == null)
|
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
+ var roleOb = input.UserRoles?.FirstOrDefault(r => r.RoleId == role.RoleId);
|
|
|
|
+ var roleName = roleOb != null ? roleOb.RoleName : "Employee";
|
|
|
|
+ await _userManager.AddToRoleAsync(user, roleName);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
// await _userRole.AddRangeAsync(userRoles);
|
|
// await _userRole.AddRangeAsync(userRoles);
|
|
@@ -161,7 +175,7 @@ namespace MTWorkHR.Application.Services
|
|
}
|
|
}
|
|
public async Task<bool> ConfirmEmail(ForgetPasswordDto input)
|
|
public async Task<bool> ConfirmEmail(ForgetPasswordDto input)
|
|
{
|
|
{
|
|
- var user = await _userManager.FindByEmailAsync(input.Email);
|
|
|
|
|
|
+ var user = await _userManager.FindByIdAsync(input.UserId);
|
|
if (user == null)
|
|
if (user == null)
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
var result = await _userManager.ConfirmEmailAsync(user, input.Token);
|
|
var result = await _userManager.ConfirmEmailAsync(user, input.Token);
|
|
@@ -189,11 +203,13 @@ namespace MTWorkHR.Application.Services
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
|
|
|
string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
|
string token = await _userManager.GenerateEmailConfirmationTokenAsync(user);
|
|
|
|
+ string codeHtmlVersion = HttpUtility.UrlEncode(token);
|
|
|
|
+
|
|
var route = "auth/ConfirmEmail";
|
|
var route = "auth/ConfirmEmail";
|
|
var origin = _configuration.JwtSettings.Audience;
|
|
var origin = _configuration.JwtSettings.Audience;
|
|
var endpointUri = new Uri(string.Concat($"{origin}/", route));
|
|
var endpointUri = new Uri(string.Concat($"{origin}/", route));
|
|
var userURL = QueryHelpers.AddQueryString(endpointUri.ToString(), "userId", user.Id);
|
|
var userURL = QueryHelpers.AddQueryString(endpointUri.ToString(), "userId", user.Id);
|
|
- var confirmEmailUrl = QueryHelpers.AddQueryString(userURL.ToString(), "token", token);
|
|
|
|
|
|
+ var confirmEmailUrl = QueryHelpers.AddQueryString(userURL.ToString(), "token", codeHtmlVersion);
|
|
return new Tuple<string, string>(confirmEmailUrl, user.Email);
|
|
return new Tuple<string, string>(confirmEmailUrl, user.Email);
|
|
}
|
|
}
|
|
|
|
|