| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- 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<User> _userManager;
- private readonly IMapper _mapper;
- private readonly DatabaseContext _databaseContext;
- private readonly IEmailer _emailer;
- //private readonly AuthorizationSettings _authSettings;
- //private readonly ILogger<UserService> _logger;
- //private const string GoogleApiTokenInfoUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}";
- //private string[] SupportedClientsIds = { "" };
-
- public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> 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<IEnumerable<User?>> GetAll() =>
- await _userManager.Users.ToListAsync();
-
- public async Task<User?> GetById(int id) =>
- await _userManager.FindByIdAsync(id.ToString());
- public async Task<User?> GetByEmail(string email) =>
- await _userManager.FindByEmailAsync(email);
-
- public async Task CreateUser(CreateUserRequestDto model)
- {
- var user = _mapper.Map<User>(model);
-
- await _userManager.CreateAsync(user, model.Password);
- }
- public async Task RemoveUser(User user)
- {
- await _userManager.DeleteAsync(user);
-
- await _databaseContext.SaveChangesAsync();
- }
-
- public async Task<bool?> ToggleEnable(User user)
- {
- user.IsEnabled = !user.IsEnabled;
-
- await _databaseContext.SaveChangesAsync();
-
- return user.IsEnabled;
- }
-
- public async Task<ServiceResponseDTO<object>> SendRegistrationLink(InviteDTO invite)
- {
- // check if user exists
- var check = await _userManager.FindByEmailAsync(invite.Email);
- if (check != null)
- return new ServiceResponseDTO<object>()
- {
- 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<object>
- {
- Data = new { Message = "Link has been sent!" }
- };
- }
-
- public async Task<bool> 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;
- }
-
- }
- }
|