public IActionResult RegisterSOPUser() { var orderVM = new RegisterSOPUserViewModel(); var getuser = _userManager.GetUserId(User); var compid = (from i in _context.CompanyClaim where i.UserId == getuser select i.CompanyId).Single(); var logostring = (from i in _context.TheCompanyInfo where i.CompanyId == compid select i.Logo).Single(); ViewBag.logostring = logostring; var getdep = (from m in _context.Departments where m.CompanyId == compid select new { m.DepartmentId, m.DepartmentName }).ToList(); var getjobs = (from m in _context.JobTitles where m.CompanyId == compid select new { m.JobTitleId, m.JobTitle }).ToList(); orderVM.Departments = new List <RegisterDepartmentViewModel>(); foreach (var items in getdep) { var itemname = @items.DepartmentName; var itemId = @items.DepartmentId; orderVM.Departments.Add(new RegisterDepartmentViewModel { Value = @itemId, DepartmentId = @itemId, DepartmentName = @itemname }); } ; orderVM.JobTitles = new List <RegisterJobTitleViewModel>(); foreach (var items in getjobs) { var jobname = @items.JobTitle; var jobid = @items.JobTitleId; orderVM.JobTitles.Add(new RegisterJobTitleViewModel { Value = @jobid, JobTitleId = @jobid, JobTitle = @jobname }); } return(View(orderVM)); }
public async Task <IActionResult> RegisterSOPUser(RegisterSOPUserViewModel model, [Bind("ClaimId,FirstName,SecondName,CompanyId")] ApplicationDbContext.ClaimComp compclaim) { Console.WriteLine("\nRegister SOP User Started.\n"); var currentuser = await _userManager.GetUserAsync(User); var user_id = currentuser.Id; var modules = (from i in _context.CompanyClaim where i.UserId == user_id select i.CompanyId).Single(); var orderVM = new RegisterSOPUserViewModel(); var getuser = _userManager.GetUserId(User); var compid = (from i in _context.CompanyClaim where i.UserId == getuser select i.CompanyId).Single(); var logostring = (from i in _context.TheCompanyInfo where i.CompanyId == compid select i.Logo).Single(); ViewBag.logostring = logostring; var getdep = (from m in _context.Departments where m.CompanyId == compid select new { m.DepartmentId, m.DepartmentName }).ToList(); var getjobs = (from m in _context.JobTitles where m.CompanyId == compid select new { m.JobTitleId, m.JobTitle }).ToList(); orderVM.Departments = new List <RegisterDepartmentViewModel>(); foreach (var items in getdep) { var itemname = @items.DepartmentName; var itemId = @items.DepartmentId; orderVM.Departments.Add(new RegisterDepartmentViewModel { Value = @itemId, DepartmentId = @itemId, DepartmentName = @itemname }); } ; orderVM.JobTitles = new List <RegisterJobTitleViewModel>(); foreach (var items in getjobs) { var jobname = @items.JobTitle; var jobid = @items.JobTitleId; orderVM.JobTitles.Add(new RegisterJobTitleViewModel { Value = @jobid, JobTitleId = @jobid, JobTitle = @jobname }); } if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { await _userManager.AddToRoleAsync(user, "SOPUser"); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); _context.CompanyClaim.Select(u => u.UserId); var newuserid = user.Id; compclaim.UserId = newuserid; _context.CompanyClaim.Select(u => u.DepartmentId); var selected = Request.Form["DepartmentId"]; compclaim.DepartmentId = selected; _context.CompanyClaim.Select(u => u.JobTitleId); var seljob = Request.Form["JobTitleId"]; compclaim.JobTitleId = seljob; _context.CompanyClaim.Select(u => u.CompanyId); compclaim.CompanyId = modules; _context.Add(compclaim); _logger.LogInformation("User created a new account with password."); await _context.SaveChangesAsync(); AddErrors(result); // If we got this far, the process succeeded var apiKey = _configuration.GetSection("SENDGRID_API_KEY").Value; Console.WriteLine(apiKey); var client = new SendGridClient(apiKey); var loggedInEmail = _userManager.GetUserName(User); var newUserEmail = model.Email; var newUserPassword = model.Password; var firstName = Request.Form["FirstName"]; var secondName = Request.Form["SecondName"]; var msg = new SendGridMessage() { From = new EmailAddress(loggedInEmail, "SOPMan"), Subject = "You have been registered as a SOPMan User", PlainTextContent = "Hello, " + firstName + " " + secondName, HtmlContent = "Hello, " + firstName + " " + secondName + ",<br>Your username is: " + newUserEmail + "<br>Your password is: " + newUserPassword }; Console.WriteLine(msg); msg.AddTo(new EmailAddress(newUserEmail, firstName + " " + secondName)); var response = await client.SendEmailAsync(msg); Console.WriteLine(response); return(RedirectToAction("People", "Setup")); } } // If we got this far, something failed, redisplay form return(View(model)); }