public async Task <IActionResult> RegisterWorker([FromBody] RegisterNonContractorRequestDto model) { var result = await _authService.RegisterNonContractorAsync(model, Request, User, Url, Seniority.Worker); if (result.Success) { return(Ok()); } else { return(BadRequest(result.Errors)); } }
public async Task <AuthInternalDto> RegisterNonContractorAsync(RegisterNonContractorRequestDto model, HttpRequest Request, ClaimsPrincipal User, IUrlHelper Url, string seniority) { var oid = GetRequestOid(Request); var inviter = await _userManager.GetUserAsync(User); ApplicationUser user = new ApplicationUser() { Email = model.Email, UserName = model.Email, FullName = model.FullName, IsActive = true, IsInitialSet = false, SecurityStamp = Guid.NewGuid().ToString(), }; string password = GeneratePassword(); var result = await _userManager.CreateAsync(user, password); if (result.Succeeded) { await _userManager.AddClaimAsync(user, new Claim(Seniority.OrganisationIdClaimKey, oid.ToString())); await _userManager.AddToRoleAsync(user, seniority); var token = await _userManager.GenerateEmailConfirmationTokenAsync(user); var confirmationLink = Url.Action(nameof(AuthController.ConfirmEmail), "Auth", new { userId = user.Id, token = token }, Request.Scheme); var org = await _appDbContext.Organisations.FirstOrDefaultAsync(o => o.Id == oid); _queue.QueueBackgroundWorkItem(async token => { using (var scope = _serviceScopeFactory.CreateScope()) { var mailService = scope.ServiceProvider.GetRequiredService <IMailService>(); await mailService.SendConfirmationEmailAndPasswordNonContractorAsync(org, user, inviter, seniority, confirmationLink, password); } }); return(new AuthInternalDto { Success = true }); } else { return(new AuthInternalDto { Success = false, Errors = result.Errors.ToList() }); } }