123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.WebUtilities;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using MTWorkHR.Application.Identity;
- using MTWorkHR.Application.MappingProfiles;
- using MTWorkHR.Application.Models;
- using MTWorkHR.Application.Services;
- using MTWorkHR.Application.Models.Email;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.IRepositories;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Identity.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MTWorkHR.Core;
- namespace MTWorkHR.Identity.Services
- {
- public class UserService : IUserService
- {
- private readonly RoleManager<ApplicationRole> _roleManager;
- private readonly ApplicationUserManager _userManager;
- private readonly IUnitOfWork _unitOfWork;
- private readonly IUserRoleRepository<IdentityUserRole<string>> _userRole;
- private readonly AppSettingsConfiguration _configuration;
- private readonly IEmailSender _emailSender;
- public UserService(ApplicationUserManager userManager, IUnitOfWork unitOfWork
- , RoleManager<ApplicationRole> roleManager, AppSettingsConfiguration configuration, IEmailSender emailSender
- , IUserRoleRepository<IdentityUserRole<string>> userRole)
- {
- _userManager = userManager;
- _unitOfWork = unitOfWork;
- _roleManager = roleManager;
- _userRole = userRole;
- _configuration = configuration;
- _emailSender = emailSender;
- }
- public async Task<UserDto> GetById(string userId)
- {
- var employee = await _userManager.FindByIdAsync(userId);
- return new UserDto
- {
- Email = employee.Email,
- FirstName = employee.FirstName,
- LastName = employee.LastName,
- Id = employee.Id
- };
- }
- public async Task<List<UserDto>> GetAll()
- {
- var employees = await _userManager.GetUsersInRoleAsync("Employee");
- return employees.Select( e=> new UserDto
- {
- Email = e.Email,
- FirstName = e.FirstName,
- LastName = e.LastName,
- Id = e.Id
- }).ToList();
- }
- public async Task Delete(string id)
- {
- var user = await _userManager.FindByIdAsync(id);
- if (user != null)
- {
- user.IsDeleted = true;
- await _userManager.UpdateAsync(user);
- }
- }
-
- public async Task<UserDto> Create(UserDto input)
- {
- var emailExists = await _userManager.FindByEmailAsync(input.Email);
- if (emailExists != null)
- throw new AppException(ExceptionEnum.RecordAlreadyExist);
- var phoneExists = await _userManager.FindByAnyAsync(input.PhoneNumber);
- if (phoneExists != null)
- throw new AppException(ExceptionEnum.RecordAlreadyExist);
- var userExists = await _userManager.FindByAnyAsync(input.UserName);
- if (userExists != null)
- throw new AppException(ExceptionEnum.RecordAlreadyExist);
- var user = MapperObject.Mapper.Map<ApplicationUser>(input);
- _unitOfWork.BeginTran();
- //saving user
- var result = await _userManager.CreateAsync(user);
- if (!result.Succeeded)
- throw new AppException(ExceptionEnum.RecordCreationFailed);
- input.Id = user.Id;
- //saving userRoles
- 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);
- }
- await _userRole.AddRangeAsync(userRoles);
- await _unitOfWork.CompleteAsync();
- _unitOfWork.CommitTran();
- try
- {
- string url = await GetResetPasswordURL(user.Id);
- var sendMailResult = await _emailSender.SendEmail(new EmailMessage {
- Subject = "Register Confirmation",
- To = input.Email,
- Body = "Please Set Your Password (this link will expired after 24 hours)"
- , url = url, userId = user.Id } );
- if (!sendMailResult)
- {
- throw new AppException("User created, but could not send the email!");
- }
- }
- catch
- {
- throw new AppException("User created, but could not send the email!");
- }
- return input;
- }
- private async Task<string> GetResetPasswordURL(string userId)
- {
- var user = await _userManager.Users.FirstOrDefaultAsync(x => (!x.IsDeleted) && x.Id.Equals(userId));
- if (user == null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- string code = await _userManager.GeneratePasswordResetTokenAsync(user);
- var route = "login/forgetpassword";
- var origin = _configuration.JwtSettings.Audience;
- var endpointUri = new Uri(string.Concat($"{origin}/", route));
- var userURL = QueryHelpers.AddQueryString(endpointUri.ToString(), "userId", user.Id);
- var passwordResetURL = QueryHelpers.AddQueryString(userURL.ToString(), "token", code);
- return passwordResetURL;
- }
- public Task<UserDto> Update(UserDto input)
- {
- throw new NotImplementedException();
- }
- public Task<UserDto> UpdateWithoutChildren(UserDto input)
- {
- throw new NotImplementedException();
- }
- public Task ForgetPassword(string input)
- {
- throw new NotImplementedException();
- }
- public Task<bool> ConfirmForgetPassword(ForgetPasswordDto input)
- {
- throw new NotImplementedException();
- }
- public Task<bool> ResetPassword(ResetPasswordDto input)
- {
- throw new NotImplementedException();
- }
- }
- }
|