You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserService.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Diligent.WebAPI.Business.Services.Interfaces;
  2. using Diligent.WebAPI.Business.Settings;
  3. using Diligent.WebAPI.Contracts.DTOs.User;
  4. using Diligent.WebAPI.Data;
  5. using Microsoft.AspNetCore.Identity;
  6. using Microsoft.AspNetCore.WebUtilities;
  7. using Microsoft.Extensions.Logging;
  8. using System.Web;
  9. namespace Diligent.WebAPI.Business.Services
  10. {
  11. public class UserService : IUserService
  12. {
  13. private readonly FrontEndSettings _frontEndSettings;
  14. private readonly UserManager<User> _userManager;
  15. private readonly IMapper _mapper;
  16. private readonly DatabaseContext _databaseContext;
  17. private readonly IEmailer _emailer;
  18. //private readonly AuthorizationSettings _authSettings;
  19. //private readonly ILogger<UserService> _logger;
  20. //private const string GoogleApiTokenInfoUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token={0}";
  21. //private string[] SupportedClientsIds = { "" };
  22. public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer)
  23. {
  24. _frontEndSettings = frontEndSettings.Value;
  25. _userManager = userManager;
  26. _mapper = mapper;
  27. _databaseContext = databaseContext;
  28. _emailer = emailer;
  29. //_authSettings = authSettings.Value;
  30. //_logger = logger;
  31. }
  32. public async Task<IEnumerable<User?>> GetAll() =>
  33. await _userManager.Users.ToListAsync();
  34. public async Task<User?> GetById(int id) =>
  35. await _userManager.FindByIdAsync(id.ToString());
  36. public async Task<User?> GetByEmail(string email) =>
  37. await _userManager.FindByEmailAsync(email);
  38. public async Task CreateUser(CreateUserRequestDto model)
  39. {
  40. var user = _mapper.Map<User>(model);
  41. await _userManager.CreateAsync(user, model.Password);
  42. }
  43. public async Task RemoveUser(User user)
  44. {
  45. await _userManager.DeleteAsync(user);
  46. await _databaseContext.SaveChangesAsync();
  47. }
  48. public async Task<bool?> ToggleEnable(User user)
  49. {
  50. user.IsEnabled = !user.IsEnabled;
  51. await _databaseContext.SaveChangesAsync();
  52. return user.IsEnabled;
  53. }
  54. public async Task<ServiceResponseDTO<object>> SendRegistrationLink(InviteDTO invite)
  55. {
  56. // check if user exists
  57. var check = await _userManager.FindByEmailAsync(invite.Email);
  58. if (check != null)
  59. return new ServiceResponseDTO<object>()
  60. {
  61. IsError = true,
  62. ErrorMessage = "User already registered."
  63. };
  64. // create template user
  65. // this user is disabled to log in until confirming invitation
  66. var user = new User
  67. {
  68. UserName = invite.Email,
  69. Email = invite.Email,
  70. FirstName = invite.FirstName,
  71. LastName = invite.LastName,
  72. IsEnabled = false
  73. };
  74. await _userManager.CreateAsync(user, StringGenerator.GenerateRandomPassword());
  75. // generate invitation token for user
  76. // encoded for URLs
  77. var token = await _userManager.GeneratePasswordResetTokenAsync(user);
  78. token = HttpUtility.UrlEncode(token);
  79. // send link
  80. await _emailer.SendEmailAndWriteToDbAsync(invite.Email, "Welcome", HTMLHelper.RenderRegisterPage($"{_frontEndSettings.BaseUrl}/register?token={token}&email={invite.Email}"), isHtml: true);
  81. await _databaseContext.SaveChangesAsync();
  82. return new ServiceResponseDTO<object>
  83. {
  84. Data = new { Message = "Link has been sent!" }
  85. };
  86. }
  87. public async Task<bool> VerifyToken(User user, string token)
  88. {
  89. // this method is going to be updated
  90. // curent new password value is static and only used for testing
  91. // method is not complete and is currently only used to check if valid reset token is sent
  92. var result = await _userManager.ResetPasswordAsync(user, token, "Nekasifra123!");
  93. return result.Succeeded;
  94. }
  95. }
  96. }