Beispiel #1
0
        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 });
        }
Beispiel #2
0
        private async Task<UserViewModel> CreateUserViewModel(Guid? tournamentId)
        {
            // Get user

            var ident = HttpContext.User.Identity as ClaimsIdentity;
            String userId = ident.GetUserId();
            User user = await userManager.FindByIdAsync(userId);

            // Get tournament

            Tournament tournament = null;

            if (tournamentId != null)
            {
                tournament = tournamentManager.GetTournament((Guid)tournamentId);
            }

            // Build basic view model

            if (String.IsNullOrEmpty(user.NewEMail))
            {
                user.NewEMail = user.Email;
            }

            UserViewModel viewModel = new UserViewModel
            {
                User = user,
                Tournament = tournament
            };

            // Get common user properties and add them to view model

            var userProperties = userManager.GetUserProperties().ToList();

            foreach (var userProperty in userProperties)
            {
                var userPropertyValue = user.PropertyValues.FirstOrDefault(p => p.UserPropertyId == userProperty.Id);
                var value = userPropertyValue != null ? userPropertyValue.Value : null;
                var userPropertyValueViewModel = CreateUserPropertyValueViewModel(userProperty, value, user);
                viewModel.UserProperties.Add(userPropertyValueViewModel);
            }

            // Tournament specific user properties

            if (tournamentId != null)
            {
                var userTournamentProperties = tournamentManager.GetUserTournamentProperties((Guid)tournamentId);

                foreach (var userTournamentProperty in userTournamentProperties)
                {
                    // If it is a common user property, only update the required property, if necessary

                    var userPropertyValueViewModel = viewModel.UserProperties.FirstOrDefault(pv =>
                        pv.UserPropertyId == userTournamentProperty.UserPropertyId);
                    if (userPropertyValueViewModel != null && userTournamentProperty.Required)
                    {
                        userPropertyValueViewModel.Required = true;
                    }

                    // If it is tournament specific property, add it to the view model

                    if (userPropertyValueViewModel == null)
                    {
                        var userPropertyValue = user.TournamentPropertyValues.FirstOrDefault(tp =>
                            tp.TournamentId == userTournamentProperty.TournamentId
                            && tp.UserPropertyId == userTournamentProperty.UserPropertyId);
                        var value = userPropertyValue != null ? userPropertyValue.Value : null;
                        userPropertyValueViewModel = CreateUserPropertyValueViewModel(userTournamentProperty.UserProperty, value, user);
                        viewModel.UserProperties.Add(userPropertyValueViewModel);
                    }
                }

            }
            return viewModel;
        }