public ActionResult BecomeSupplier(string userId = "") { userId = userId == string.Empty ? CurrentUserId : userId; if (userId == null) { _logger.Error("Could not find the user Id."); return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } ApplicationUser applicationUser = db.Users.Find(userId); if (applicationUser == null) { _logger.Error("We found the user Id, but could not retrieve the application user from db."); return HttpNotFound(); } var vm = new RegisterSupplierViewModel(){ UserId = userId, Admin = new ContactViewModel() { FirstName = applicationUser.UserProfile.FirstName, LastName = applicationUser.UserProfile.LastName, Email = applicationUser.Email, Phone = applicationUser.PhoneNumber, Type = ContactType.Admin } }; return View(vm); }
public async Task <ActionResult> RegisterSupplier(RegisterSupplierViewModel viewModel) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = viewModel.Email, Email = viewModel.Email, Name = viewModel.Name }; var result = await UserManager.CreateAsync(user, viewModel.Password); if (result.Succeeded) { await this.UserManager.AddToRoleAsync(user.Id, viewModel.UserRole); Supplier supplier = new Supplier(); supplier.Name = viewModel.BrandName; supplier.Description = viewModel.Description; context.SupplierTable.Add(supplier); context.SaveChanges(); await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View("RegisterFormSupplier", viewModel)); }
public ActionResult BecomeSupplier(RegisterSupplierViewModel vm, HttpPostedFileBase brandLogo) { if (brandLogo == null) { ModelState.AddModelError("File", "Please Upload Your Company's Logo"); return View(vm); } if (ModelState.IsValid) { var user = UserManager.FindById(vm.UserId); UserManager.AddToRole(user.Id, Role.Guest.ToString()); try { var admin = vm.Admin.ToModel(); _contacts.Save(admin); var supplier = vm.ToModel(); supplier.AdminId = admin.Id; supplier.ContactId = admin.Id; supplier.AspNetUserId = user.Id; supplier.BrandLogo = SaveUploadedFile(brandLogo, supplier.AspNetUserId); _suppliers.Save(supplier); } catch (Exception ex) { ModelState.AddModelError("", ex); _logger.Error("There has been an error trying to save the supplier or admin/contact: {0}. Inner Exception: {1}", ex.Message, ex.InnerException.Message); return View(vm); } return View("DisplaySupplierEmail"); } return View(vm); }
public async Task<ActionResult> RegisterSupplier(RegisterSupplierViewModel vm, HttpPostedFileBase brandLogo) { if (brandLogo == null) { ModelState.AddModelError("File", "Please Upload Your Company's Logo"); return View(vm); } if (ModelState.IsValid) { var user = vm.ToAccountModel(); var result = await UserManager.CreateAsync(user, vm.Password); if (result.Succeeded) { UserManager.AddToRole(user.Id, Role.Guest.ToString()); //await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); ViewBag.Link = callbackUrl; try { var supplier = vm.ToModel(); var admin = vm.Admin.ToModel(); _contacts.Save(admin); supplier.AdminId = admin.Id; _logger.Info("Admin saved. Admin:{0}. Name: {1}{2}", admin.Email, admin.FirstName, admin.LastName); if (vm.IsContactSameAsAdmin) { supplier.ContactId = admin.Id; _logger.Info("Contact same as admin"); } else { var contact = vm.Contact.ToModel(); _contacts.Save(contact); supplier.ContactId = contact.Id; _logger.Info("Contact saved. Admin:{0}. Name: {1}{2}", admin.Email, admin.FirstName, admin.LastName); } supplier.AspNetUserId = user.Id; supplier.BrandLogo = SaveUploadedFile(brandLogo, supplier.AspNetUserId); _logger.Info("Brand logo saved!"); _suppliers.Save(supplier); } catch (Exception ex) { _logger.Info("Exception when saving supplier{0}. Eroare: {1}. Inner Error: {2}", user.Email, ex.Message, ex.InnerException.Message); ModelState.AddModelError("", ex); return View(vm); } return View("DisplaySupplierEmail"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(vm); }
public ActionResult RegisterSupplier() { var model = new RegisterSupplierViewModel(); return View(model); }