public ActionResult ChangeUserName(ChangeUserNameViewModel model) { if (TravelExpertsData.IsUniqueUserName(model.NewUserName)) { Customer curr = GetCurrentCustomer(); // make sure we found customer if (curr == null) { ModelState.AddModelError(String.Empty, "Sorry an error occured while trying to find you. Please try log in again."); return(View()); } curr.UserName = model.NewUserName; if (TravelExpertsData.UpdateCustomerUserName(curr) && TravelExpertsData.UpdateAccountUserName(curr)) { return(RedirectToAction("Index", new { Message = ManageMessageId.EditUserNameSuccess })); } // something went wrong return(RedirectToAction("Index", new { Message = ManageMessageId.Error })); } ModelState.AddModelError(String.Empty, "Account is already linked to this user name."); return(View()); }
public async Task <ActionResult> Register(RegisterViewModel model) { // error message assignments string postalError = ""; string homePhoneError = ""; string busPhoneError = ""; string emailError = ""; // some further validation if (ModelState.IsValid && Validator.IsCanadianPostal(model.CustPostal, out postalError) && (string.IsNullOrEmpty(model.CustHomePhone) || Validator.IsCanadianPhoneNumber(model.CustHomePhone, out homePhoneError)) && // databse allows null for home phone number Validator.IsCanadianPhoneNumber(model.CustBusPhone, out busPhoneError) && TravelExpertsData.IsUniqueEmail(model.CustEmail, out emailError) && TravelExpertsData.IsUniquePhone(model.CustBusPhone, out busPhoneError)) { // transform form data to customer object Customer newCustomer = new Customer { CustomerId = model.CustomerId, CustFirstName = model.CustFirstName, CustLastName = model.CustLastName, CustAddress = model.CustAddress, CustCity = model.CustCity, CustProv = model.CustProv, CustPostal = model.CustPostal, CustCountry = model.CustCountry, CustHomePhone = model.CustHomePhone, CustBusPhone = model.CustBusPhone, CustEmail = model.CustEmail, UserName = model.UserName }; // is customer in database and just needs account? if (!TravelExpertsData.CustomerExists(newCustomer)) // customer is not in Customer table, so add (account can't exist if customer is not in customer table) { TravelExpertsData.InsertCustomer(newCustomer); } else if (!TravelExpertsData.AccountExists(newCustomer)) // customer does not have an account { // Customer exists in Customer table but not in Accounts table TravelExpertsData.UpdateCustomerUserName(newCustomer); // add user name to Customer Table and AspNetUsers table TravelExpertsData.UpdateCustomerEmail(newCustomer); // lots of empty string emails in Customer table, may as well update here } // auto-generated - create User var user = new ApplicationUser { UserName = model.UserName, Email = model.CustEmail, PhoneNumber = model.CustBusPhone }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) // registration in Accounts tables success { await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false); return(RedirectToAction("Index", "Home")); } AddErrors(result); } // format phone number errors to be more informative if (!string.IsNullOrEmpty(homePhoneError)) { homePhoneError = $"{homePhoneError} (home phone number)\n\teg. 4031234567"; ModelState.AddModelError(string.Empty, homePhoneError); } if (!string.IsNullOrEmpty(busPhoneError)) { busPhoneError = $"{busPhoneError} (business phone number)\n\teg. 4031234567"; ModelState.AddModelError(string.Empty, busPhoneError); } // email already taken if (!string.IsNullOrEmpty(emailError)) { ModelState.AddModelError(string.Empty, emailError); } // add error messages to model if (!string.IsNullOrEmpty(postalError)) { ModelState.AddModelError(string.Empty, postalError); } // If we got this far, something failed, redisplay form return(View(model)); }