public bool UpdateProfile(string providerUserKey, UpdateProfileModel model) { try { using (var context = new greenMoneyEntities()) { // find from gm users var user = context.Users1.Find(new Guid(providerUserKey)); user.FirstName = model.FirstName; user.LastName = model.LastName; user.PhoneNumber = model.PhoneNumber; if (model.BirthDate != DateTime.MinValue) { user.DateOfBirth = model.BirthDate; } user.Sex = model.Sex; user.SendEmailUpdates = model.SendEmailUpdates; user.PushNotifications = model.PushNotifications; user.PostToFacebook = model.PostToFacebook; //Private company - auspost user.EmploymentType = model.EmploymentType; if (model.AddressId != null) { user.Address_Id = (int) model.AddressId; } // save changes context.SaveChanges(); } return true; } catch (Exception) { return false; } }
public ActionResult UpdateProfileAuspost(UpdateProfileAuspostViewModel viewModel) { LayoutViewModel.ActiveLink = Links.AccountUpdateProfile; var membershipUser = Membership.GetUser(); if (ModelState.IsValid) { UpdateProfileModel updateModel = new UpdateProfileModel(); updateModel.FirstName = viewModel.FirstName; updateModel.LastName = viewModel.LastName; updateModel.PhoneNumber = viewModel.PhoneNumber; updateModel.Sex = viewModel.Sex; updateModel.PushNotifications = viewModel.PushNotifications; updateModel.EmploymentType = viewModel.EmploymentType; if (viewModel.DateOfBirthDay != null && viewModel.DateOfBirthMonth != null && viewModel.DateOfBirthYear != null) { updateModel.BirthDate = new DateTime(year: viewModel.DateOfBirthYear.Value, month: viewModel.DateOfBirthMonth.Value, day: viewModel.DateOfBirthDay.Value); } // find new user's address //int? addressId = new UserService().FindMatchingAuspostAddressId( // streetName: viewModel.StreetName, // streetType: viewModel.StreetType, // suburb: viewModel.Subrp, // state: viewModel.State // ); //if (addressId != null) //{ // updateModel.AddressId = addressId; //} //else //{ // FillBaseViewModelData(viewModel); // ModelState.AddModelError("", "Incomplete workplace details. Please update your details and save."); // return View(viewModel); //} var success = true; // change password if (!string.IsNullOrEmpty(viewModel.OldPassword) || !string.IsNullOrEmpty(viewModel.NewPassword) || !string.IsNullOrEmpty(viewModel.ConfirmNewPassword)) { if (string.IsNullOrEmpty(viewModel.OldPassword)) { ModelState.AddModelError("OldPassword", "The Old password field is required."); success = false; } if (string.IsNullOrEmpty(viewModel.NewPassword)) { ModelState.AddModelError("NewPassword", "The New password field is required."); success = false; } if (string.IsNullOrEmpty(viewModel.ConfirmNewPassword)) { ModelState.AddModelError("ConfirmNewPassword", "The Confirm new password field is required."); success = false; } if (success && viewModel.NewPassword != viewModel.ConfirmNewPassword) { ModelState.AddModelError("ConfirmNewPassword", "New password and confirmation password do not match."); success = false; } if (success && !membershipUser.ChangePassword(viewModel.OldPassword, viewModel.NewPassword)) { ModelState.AddModelError("OldPassword", "Old password is incorrect."); success = false; } } if (viewModel.Photo != null && viewModel.Photo.ContentLength > 0) { MemoryStream target = new MemoryStream(); viewModel.Photo.InputStream.CopyTo(target); byte[] data = target.ToArray(); UploadModel model = new UploadModel { ContentType = viewModel.Photo.ContentType, Contents = data, FileName = viewModel.Photo.FileName }; UploadModel upload = new UploadService().UploadFile(LayoutViewModel.ProviderUserKey, model, true); } if (success) { var updated = new ProfileService().UpdateProfile(LayoutViewModel.ProviderUserKey, updateModel); // change username COMPLICATED if (membershipUser.UserName != viewModel.Email) { var config = WebConfigurationManager.OpenWebConfiguration("~"); var section = config.SectionGroups["system.web"].Sections["membership"] as MembershipSection; var defaultProvider = section.DefaultProvider; var connectionStringName = section.Providers[defaultProvider].ElementInformation.Properties["connectionStringName"].Value.ToString(); string connectionString = config.ConnectionStrings.ConnectionStrings[connectionStringName].ConnectionString; string companyEmail = string.Join(viewModel.CompanyEmail, viewModel.CompanyEmailDomain); var changed = new ProfileService().ChangeUsername(membershipUser.UserName, companyEmail, connectionString); if (changed) { // change email as well membershipUser.Email = companyEmail; // need to re-verify membershipUser.IsApproved = false; SendVerifyEmail(membershipUser.Email, updateModel.FirstName, LayoutViewModel.CurrentAccountId, false); //instead I'm showing verifucation code in the view Membership.UpdateUser(membershipUser); // need to sign out to force verification FormsAuthentication.SignOut(); TempData["VerifyCode"] = ZBase32.Encode(LayoutViewModel.CurrentAccountId.ToByteArray()); // redirect to screen which tells user to check email return RedirectToAction("EmailChangeSuccess"); } else { ModelState.AddModelError("", "A user for that email address already exists. Please enter a different email address."); } } return RedirectToAction("MyProfile"); } //Something not valid, show errors viewModel.SupportEmail = ConfigurationManager.AppSettings["SupportEmail"]; return View(viewModel); } FillBaseViewModelData(viewModel); return View(viewModel); }
public bool UpdateProfile(string providerUserKey, UpdateProfileModel updateModel) { return new ProfileRepository().UpdateProfile(providerUserKey, updateModel); }