using Diligent.WebAPI.Business.Services.Interfaces; using Diligent.WebAPI.Business.Settings; using Diligent.WebAPI.Contracts.DTOs.User; using Diligent.WebAPI.Data; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Extensions.Logging; using System.Web; namespace Diligent.WebAPI.Business.Services { public class UserService : IUserService { private readonly FrontEndSettings _frontEndSettings; private readonly UserManager _userManager; private readonly IMapper _mapper; private readonly DatabaseContext _databaseContext; private readonly IEmailer _emailer; //private readonly AuthorizationSettings _authSettings; //private readonly ILogger _logger; //private const string GoogleApiTokenInfoUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}"; //private string[] SupportedClientsIds = { "" }; public UserService(IOptions frontEndSettings, UserManager userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer) { _frontEndSettings = frontEndSettings.Value; _userManager = userManager; _mapper = mapper; _databaseContext = databaseContext; _emailer = emailer; //_authSettings = authSettings.Value; //_logger = logger; } public async Task> GetAll() => await _userManager.Users.ToListAsync(); public async Task GetById(int id) => await _userManager.FindByIdAsync(id.ToString()); public async Task GetByEmail(string email) => await _userManager.FindByEmailAsync(email); public async Task CreateUser(CreateUserRequestDto model) { var user = _mapper.Map(model); await _userManager.CreateAsync(user, model.Password); } public async Task RemoveUser(User user) { await _userManager.DeleteAsync(user); await _databaseContext.SaveChangesAsync(); } public async Task ToggleEnable(User user) { user.IsEnabled = !user.IsEnabled; await _databaseContext.SaveChangesAsync(); return user.IsEnabled; } public async Task> SendRegistrationLink(InviteDTO invite) { // check if user exists var check = await _userManager.FindByEmailAsync(invite.Email); if (check != null) return new ServiceResponseDTO() { IsError = true, ErrorMessage = "User already registered." }; // create template user // this user is disabled to log in until confirming invitation var user = new User { UserName = invite.Email, Email = invite.Email, FirstName = invite.FirstName, LastName = invite.LastName, IsEnabled = false }; await _userManager.CreateAsync(user, StringGenerator.GenerateRandomPassword()); // generate invitation token for user // encoded for URLs var token = await _userManager.GeneratePasswordResetTokenAsync(user); token = HttpUtility.UrlEncode(token); // send link await _emailer.SendEmailAndWriteToDbAsync(invite.Email, "Welcome", HTMLHelper.RenderRegisterPage($"{_frontEndSettings.BaseUrl}/register?token={token}&email={invite.Email}"), isHtml: true); await _databaseContext.SaveChangesAsync(); return new ServiceResponseDTO { Data = new { Message = "Link has been sent!" } }; } public async Task VerifyToken(User user, string token) { // this method is going to be updated // curent new password value is static and only used for testing // method is not complete and is currently only used to check if valid reset token is sent var result = await _userManager.ResetPasswordAsync(user, token, "Nekasifra123!"); return result.Succeeded; } } }