public UserPropertyValue ToUserPropertyValue() { UserPropertyValue userPropertyValue = new UserPropertyValue { User = this.User, UserId = this.UserId, UserProperty = this.UserProperty, UserPropertyId = this.UserPropertyId, Value = this.Value }; return userPropertyValue; }
public async Task <IActionResult> PutAsync(UserPropertyModel model) { Ensure.NotNull(model, "model"); string userId = HttpContext.User.FindUserId(); if (userId == null) { return(Unauthorized()); } if (options.Keys == null || !options.Keys.Contains(model.Key)) { return(BadRequest()); } UserPropertyValue entity = await db.UserProperties.FirstOrDefaultAsync(p => p.Key == model.Key && p.UserId == userId); if (entity == null) { if (model.Value != null) { entity = new UserPropertyValue() { UserId = userId, Key = model.Key, Value = model.Value }; db.UserProperties.Add(entity); } } else if (model.Value == null) { db.UserProperties.Remove(entity); } else { entity.Value = model.Value; } if (entity != null) { await db.SaveChangesAsync(); } return(Ok()); }
public async Task<ActionResult> Update(UserViewModel userViewModel, Guid? tournamentId = null) { var ident = HttpContext.User.Identity as ClaimsIdentity; String userId = ident.GetUserId(); User user = await userManager.FindByIdAsync(userId); // Validate custom user properties for (int i = 0; i < userViewModel.UserProperties.Count; i++) { var propertyValueViewModel = userViewModel.UserProperties[i]; var userProperty = await userManager.GetUserPropertyAsync(propertyValueViewModel.UserPropertyId); UserPropertyValue userPropertyValue = new UserPropertyValue { User = user, UserId = user.Id, UserProperty = userProperty, UserPropertyId = userProperty.Id, Value = propertyValueViewModel.Value, }; UserPropertyValueViewModel propertyViewModelValidated = new UserPropertyValueViewModel(userPropertyValue); // Check if property is required for tournament if (tournamentId != null) { var userTournamentProperty = tournamentManager.GetUserTournamentProperty((Guid)tournamentId, userProperty.Id); propertyViewModelValidated.Required = userTournamentProperty != null && userTournamentProperty.Required; } var validationResults = propertyViewModelValidated.Validate(null); if (propertyViewModelValidated.Type == PropertyType.Country) { propertyViewModelValidated.CreateCountryList(countryManager.GetCountries()); } userViewModel.UserProperties[i] = propertyViewModelValidated; foreach (var validationResult in validationResults) { ModelState.AddModelError("", validationResult.ErrorMessage); } } #region Change e-mail address userViewModel.User.NewEMail = userViewModel.User.NewEMail.Trim().ToLower(); if (String.IsNullOrWhiteSpace(user.NewEMail)) { user.NewEMail = user.Email; } if (userViewModel.User.NewEMail != user.Email.Trim().ToLower() || userViewModel.User.NewEMail != user.NewEMail.Trim().ToLower()) { // check for duplicate user. var duplicateUserEMail = await userManager.FindByEmailAsync(userViewModel.User.NewEMail); var duplicateUserName = await userManager.FindByNameAsync(userViewModel.User.NewEMail); if ( ( duplicateUserEMail != null && duplicateUserEMail.Id != user.Id ) || ( duplicateUserName != null && duplicateUserName.Id != user.Id ) ) { ModelState.AddModelError("User.NewEMail", Resources.User.Strings.ErrorDuplicateEMail); } } #endregion if (!ModelState.IsValid) { if (tournamentId != null) { userViewModel.Tournament = tournamentManager.GetTournament((Guid)tournamentId); } return View(userViewModel); } // Everything okay, update data user.FirstName = userViewModel.User.FirstName.Trim(); user.LastName = userViewModel.User.LastName.Trim(); user.PhoneNumber = userViewModel.User.PhoneNumber; user.NewEMail = userViewModel.User.NewEMail; // Update user var result = await userManager.UpdateAsync(user); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError("", error); } return View(); } // Store user properties foreach (var propertyValue in userViewModel.UserProperties) { if (propertyValue.TournamentSpecific && tournamentId != null) { await tournamentManager.SetUserTournamentPropertyValueAsync(userId, propertyValue.UserPropertyId, (Guid)tournamentId, propertyValue.Value); } if (!propertyValue.TournamentSpecific) { await userManager.SetUserPropertyValueAsync(userId, propertyValue.UserPropertyId, propertyValue.Value); } } // Verify e-mail if needed if (user.Email != user.NewEMail) { // Send E-Mail Confirmation Message RedirectToAction("ConfirmEMail", new { userId = userId, tournamentId = tournamentId }); } return RedirectToAction("Display", new { tournamentId = tournamentId }); }
private UserPropertyValueViewModel CreateUserPropertyValueViewModel(UserProperty userProperty, String value, User user) { // If not value is provided, build an empty value var propertyValue = new UserPropertyValue { User = user, UserId = user.Id, UserProperty = userProperty, UserPropertyId = userProperty.Id, Value = value }; // Build view model for property value var propertyViewModel = new UserPropertyValueViewModel(propertyValue); // If type is country, build country list if (userProperty.Type == PropertyType.Country) { propertyViewModel.CreateCountryList(countryManager.GetCountries()); } return propertyViewModel; }