public async Task <IActionResult> Details(InputModels.UpdateAccountInput input) { if (ModelState.IsValid) { var serviceResponse = await AccountRepository.UpdateAccount(input); return(await ForumViewResult.RedirectFromService(this, serviceResponse, FailureCallback)); } return(await FailureCallback()); async Task <IActionResult> FailureCallback() { var userRecord = AccountRepository.First(item => item.Id == input.Id); AccountRepository.CanEdit(userRecord.Id); var viewModel = new ViewModels.Account.DetailsPage { DisplayName = input.DisplayName, NewEmail = input.NewEmail, AvatarPath = userRecord.AvatarPath, Id = userRecord.Id, EmailConfirmed = userRecord.EmailConfirmed, BirthdayDays = DayPickList(input.BirthdayDay), BirthdayMonths = MonthPickList(input.BirthdayMonth), BirthdayYears = YearPickList(input.BirthdayYear), BirthdayDay = input.BirthdayDay.ToString(), BirthdayMonth = input.BirthdayMonth.ToString(), BirthdayYear = input.BirthdayYear.ToString(), Settings = SettingsRepository.GetUserSettingsList(userRecord.Id) }; return(await Task.Run(() => { return ForumViewResult.ViewResult(this, viewModel); })); } }
public async Task <IActionResult> Details(InputModels.UpdateAccountInput input) { if (ModelState.IsValid) { var serviceResponse = await AccountRepository.UpdateAccount(input); return(await this.RedirectFromService(serviceResponse, FailureCallback)); } return(await FailureCallback()); async Task <IActionResult> FailureCallback() { var userRecord = (await AccountRepository.Records()).First(item => item.Id == input.Id); var imgurLink = await DbContext.ImgurDetails.FirstOrDefaultAsync(item => item.LocalUserId == UserContext.Id); AccountRepository.CanEdit(userRecord.Id); var viewModel = new ViewModels.Account.DetailsPage { DisplayName = input.DisplayName, NewEmail = input.NewEmail, AvatarPath = userRecord.AvatarPath, Id = userRecord.Id, EmailConfirmed = userRecord.EmailConfirmed, BirthdayDays = DayPickList(input.BirthdayDay), BirthdayMonths = MonthPickList(input.BirthdayMonth), BirthdayDay = input.BirthdayDay.ToString(), BirthdayMonth = input.BirthdayMonth.ToString(), FrontPage = userRecord.FrontPage, FrontPageOptions = FrontPagePickList(userRecord.FrontPage), MessagesPerPage = userRecord.MessagesPerPage, PopularityLimit = userRecord.PopularityLimit, Poseys = userRecord.Poseys, ShowFavicons = userRecord.ShowFavicons ?? true, TopicsPerPage = userRecord.TopicsPerPage, IsImgurLinked = !(imgurLink is null) }; return(View(viewModel)); } }
public void UpdateUserSettings(InputModels.UpdateAccountInput input) { var existingRecords = Records.Where(s => s.UserId == input.Id).ToList(); if (existingRecords.Any()) { DbContext.RemoveRange(existingRecords); } foreach (var settingInput in input.Settings) { if (string.IsNullOrEmpty(settingInput.Value)) { continue; } var siteSetting = Records.FirstOrDefault(s => !s.AdminOnly && s.Name == settingInput.Key && string.IsNullOrEmpty(s.UserId)); if (siteSetting != null) { var baseSetting = BaseSettings.Get(siteSetting.Name); if (baseSetting.Options != null && !baseSetting.Options.Contains(settingInput.Value)) { throw new HttpBadRequestError(); } var record = new DataModels.SiteSetting { UserId = input.Id, Name = siteSetting.Name, Value = settingInput.Value, AdminOnly = siteSetting.AdminOnly }; DbContext.SiteSettings.Add(record); } } DbContext.SaveChanges(); }
public async Task <ServiceModels.ServiceResponse> UpdateAccount(InputModels.UpdateAccountInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); if (!await UserManager.CheckPasswordAsync(UserContext.ApplicationUser, input.Password)) { var message = $"Invalid password for '{input.DisplayName}'."; serviceResponse.Error(nameof(InputModels.UpdateAccountInput.Password), message); Log.LogWarning(message); } var userRecord = await UserManager.FindByIdAsync(input.Id); if (userRecord is null) { var message = $"No user record found for '{input.DisplayName}'."; serviceResponse.Error(message); Log.LogCritical(message); } CanEdit(userRecord.Id); await updateDisplayName(); updateBirthday(); updateFrontPage(); updateMessagesPerPage(); updateTopicsPerPage(); updatePopularityLimit(); updatePoseys(); updateShowFavicons(); if (serviceResponse.Success) { await DbContext.SaveChangesAsync(); } await updateEmail(); await updatePassword(); return(serviceResponse); async Task updateDisplayName() { if (serviceResponse.Success && input.DisplayName != userRecord.DisplayName) { if ((await Records()).Any(r => r.DisplayName == input.DisplayName)) { var message = $"The display name '{input.DisplayName}' is already taken."; serviceResponse.Error(message); Log.LogWarning(message); } else { userRecord.DisplayName = input.DisplayName; DbContext.Update(userRecord); Log.LogInformation($"Display name was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } } void updateBirthday() { if (serviceResponse.Success) { if (input.ShowBirthday != userRecord.ShowBirthday) { userRecord.ShowBirthday = input.ShowBirthday; DbContext.Update(userRecord); Log.LogInformation($"ShowBirthday was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } var birthday = new DateTime(userRecord.Registered.Year, input.BirthdayMonth, input.BirthdayDay); if (birthday != userRecord.Birthday) { userRecord.Birthday = birthday; DbContext.Update(userRecord); Log.LogInformation($"Birthday was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } } void updateFrontPage() { if (serviceResponse.Success && input.FrontPage != userRecord.FrontPage) { userRecord.FrontPage = input.FrontPage; DbContext.Update(userRecord); Log.LogInformation($"FrontPage was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } void updateMessagesPerPage() { if (serviceResponse.Success && input.MessagesPerPage != userRecord.MessagesPerPage) { userRecord.MessagesPerPage = input.MessagesPerPage; DbContext.Update(userRecord); Log.LogInformation($"MessagesPerPage was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } void updateTopicsPerPage() { if (serviceResponse.Success && input.TopicsPerPage != userRecord.TopicsPerPage) { userRecord.TopicsPerPage = input.TopicsPerPage; DbContext.Update(userRecord); Log.LogInformation($"TopicsPerPage was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } void updatePopularityLimit() { if (serviceResponse.Success && input.PopularityLimit != userRecord.PopularityLimit) { userRecord.PopularityLimit = input.PopularityLimit; DbContext.Update(userRecord); Log.LogInformation($"PopularityLimit was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } void updatePoseys() { if (serviceResponse.Success && input.Poseys != userRecord.Poseys) { userRecord.Poseys = input.Poseys; DbContext.Update(userRecord); Log.LogInformation($"Poseys was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } void updateShowFavicons() { if (serviceResponse.Success && input.ShowFavicons != userRecord.ShowFavicons) { userRecord.ShowFavicons = input.ShowFavicons; DbContext.Update(userRecord); Log.LogInformation($"ShowFavicons was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } } async Task updateEmail() { if (input.NewEmail != userRecord.Email) { serviceResponse.RedirectPath = UrlHelper.Action(nameof(Account.Details), nameof(Account), new { id = input.DisplayName }); var identityResult = await UserManager.SetEmailAsync(userRecord, input.NewEmail); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying email by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } } else { Log.LogInformation($"Email address was modified by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'."); identityResult = await UserManager.SetUserNameAsync(userRecord, input.NewEmail); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying username by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } } else { Log.LogInformation($"Username was modified by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'."); var code = await UserManager.GenerateEmailConfirmationTokenAsync(userRecord); if (EmailSender.Ready) { var callbackUrl = EmailConfirmationLink(userRecord.Id, code); await EmailSender.SendEmailConfirmationAsync(input.NewEmail, callbackUrl); } else { identityResult = await UserManager.ConfirmEmailAsync(userRecord, code); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error confirming '{userRecord.Email}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } } else { Log.LogInformation($"User confirmed email '{userRecord.Email}'."); } } if (userRecord.Id == UserContext.ApplicationUser.Id) { await SignOut(); } } } } } async Task updatePassword() { if (!string.IsNullOrEmpty(input.NewPassword) && input.Password != input.NewPassword && UserContext.ApplicationUser.Id == input.Id) { var identityResult = await UserManager.ChangePasswordAsync(userRecord, input.Password, input.NewPassword); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying password by '{UserContext.ApplicationUser.DisplayName}' for '{userRecord.DisplayName}'. Message: {error.Description}"); serviceResponse.Error(nameof(InputModels.UpdateAccountInput.NewPassword), error.Description); } } else if (userRecord.Id == UserContext.ApplicationUser.Id) { Log.LogInformation($"Password was modified by '{UserContext.ApplicationUser.DisplayName}' for '{userRecord.DisplayName}'."); await SignOut(); } } } }
public async Task <ServiceModels.ServiceResponse> UpdateAccount(InputModels.UpdateAccountInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); if (!await UserManager.CheckPasswordAsync(UserContext.ApplicationUser, input.Password)) { var message = $"Invalid password for '{input.DisplayName}'."; serviceResponse.Error(nameof(InputModels.UpdateAccountInput.Password), message); Log.LogWarning(message); } var userRecord = await UserManager.FindByIdAsync(input.Id); if (userRecord is null) { var message = $"No user record found for '{input.DisplayName}'."; serviceResponse.Error(message); Log.LogCritical(message); } CanEdit(userRecord.Id); if (userRecord is null) { var message = $"No user account found for '{input.DisplayName}'."; serviceResponse.Error(message); Log.LogCritical(message); } if (!serviceResponse.Success) { return(serviceResponse); } if (input.DisplayName != userRecord.DisplayName) { userRecord.DisplayName = input.DisplayName; DbContext.Update(userRecord); Log.LogInformation($"Display name was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } var birthday = new DateTime(input.BirthdayYear, input.BirthdayMonth, input.BirthdayDay); if (birthday != userRecord.Birthday) { userRecord.Birthday = birthday; DbContext.Update(userRecord); Log.LogInformation($"Birthday was modified by '{UserContext.ApplicationUser.DisplayName}' for account '{userRecord.DisplayName}'."); } DbContext.SaveChanges(); if (input.NewEmail != userRecord.Email) { serviceResponse.RedirectPath = UrlHelper.Action(nameof(Account.Details), nameof(Account), new { id = input.DisplayName }); var identityResult = await UserManager.SetEmailAsync(userRecord, input.NewEmail); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying email by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } return(serviceResponse); } identityResult = await UserManager.SetUserNameAsync(userRecord, input.NewEmail); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying username by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } return(serviceResponse); } Log.LogInformation($"Email address was modified by '{UserContext.ApplicationUser.DisplayName}' from '{userRecord.Email}' to '{input.NewEmail}'."); var code = await UserManager.GenerateEmailConfirmationTokenAsync(userRecord); if (EmailSender.Ready) { var callbackUrl = EmailConfirmationLink(userRecord.Id, code); await EmailSender.SendEmailConfirmationAsync(input.NewEmail, callbackUrl); if (userRecord.Id == UserContext.ApplicationUser.Id) { await SignOut(); } } else { identityResult = await UserManager.ConfirmEmailAsync(userRecord, code); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error confirming '{userRecord.Email}'. Message: {error.Description}"); serviceResponse.Error(error.Description); } } else { Log.LogInformation($"User confirmed email '{userRecord.Email}'."); } } return(serviceResponse); } SettingsRepository.UpdateUserSettings(input); if (!string.IsNullOrEmpty(input.NewPassword) && input.Password != input.NewPassword && UserContext.ApplicationUser.Id == input.Id) { var identityResult = await UserManager.ChangePasswordAsync(userRecord, input.Password, input.NewPassword); if (!identityResult.Succeeded) { foreach (var error in identityResult.Errors) { Log.LogError($"Error modifying password by '{UserContext.ApplicationUser.DisplayName}' for '{userRecord.DisplayName}'. Message: {error.Description}"); serviceResponse.Error(nameof(InputModels.UpdateAccountInput.NewPassword), error.Description); } } else if (userRecord.Id == UserContext.ApplicationUser.Id) { Log.LogInformation($"Password was modified by '{UserContext.ApplicationUser.DisplayName}' for '{userRecord.DisplayName}'."); await SignOut(); serviceResponse.RedirectPath = UrlHelper.Action(nameof(Login)); return(serviceResponse); } } if (serviceResponse.Success) { serviceResponse.RedirectPath = UrlHelper.Action(nameof(Account.Details), nameof(Account), new { id = input.DisplayName }); } return(serviceResponse); }