Пример #1
0
        public ActionResult Index(RegistrationPostModel model)
        {
            if (!_settingsRepository.RegistrationOpen)
            {
                return(View("Message", model: Resources.Global.RegistrationClosed));
            }

            if (!ModelState.IsValid)
            {
                model.Password       = null;
                model.RepeatPassword = null;
                return(View(model));
            }

            // check fo existing user
            if (!_userRepository.CheckEmail(model.Email))
            {
                ModelState.AddModelError(nameof(RegistrationPostModel.Email), Resources.Global.EmailTaken);
                model.Password       = null;
                model.RepeatPassword = null;
                return(View(model));
            }

            if (model.Password != model.RepeatPassword)
            {
                ModelState.AddModelError(nameof(RegistrationPostModel.RepeatPassword), Resources.Global.Registration_Form_Repeat_Password_Invalid);
                model.Password       = null;
                model.RepeatPassword = null;
                return(View(model));
            }

            if (model.DateOfBirth.AddYears(_configProvider.MinimumAge) > DateTime.Today)
            {
                ModelState.AddModelError(nameof(RegistrationPostModel.DateOfBirth), Resources.Global.Registration_Form_DateOfBirth_NotEnough);
                model.Password       = null;
                model.RepeatPassword = null;
                return(View(model));
            }

            // set the correct fb uri:
            model.FacebookProfileUrl = FacebookUriHelper.GetUniformFacebookUri(model.FacebookProfileUrl);

            if (model.FacebookProfileUrl == null || !_userRepository.CheckFacebookProfileUri(model.FacebookProfileUrl))
            {
                ModelState.AddModelError(nameof(RegistrationPostModel.FacebookProfileUrl), Resources.Global.FacebookURL_Invalid);
                model.Password       = null;
                model.RepeatPassword = null;
                return(View(model));
            }

            // save to db
            var domainModel = _mapper.Map <SantaUser>(model);

            _userRepository.InsertUser(domainModel);

            _emailService.SendConfirmationEmail(domainModel);

            return(View("Confirmation", model: model.Email));
        }
Пример #2
0
        public ActionResult EditAccount(SantaUserPostModel model)
        {
            if (_userRepository.WasAssigned())
            {
                return(View("Message", model: Resources.Global.Message_CannotEditAccountAfterAssignment));
            }
            var userId = GetUserId();

            if (!userId.HasValue)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // set the correct fb uri:
            model.FacebookProfileUrl = FacebookUriHelper.GetUniformFacebookUri(model.FacebookProfileUrl);

            if (model.FacebookProfileUrl == null)
            {
                ModelState.AddModelError(nameof(SantaUserPostModel.FacebookProfileUrl),
                                         Resources.Global.FacebookURL_Invalid);
                return(View(model));
            }

            var updateModel = _mapper.Map <SantaUser>(model);

            updateModel.Id = userId.Value;

            var updateResult = _userRepository.UpdateUser(updateModel);

            if (!updateResult.Success)
            {
                if (updateResult.EmailUnavailable)
                {
                    ModelState.AddModelError(nameof(SantaUserPostModel.Email), Resources.Global.EmailTaken);
                    return(View(model));
                }

                return(View("Message", model: Resources.Global.Message_Error));
            }

            if (updateResult.EmailChanged)
            {
                _emailService.SendConfirmationEmail(updateModel);

                // sign out, email is used to find people in the user db!
                HttpContext.GetOwinContext().Authentication
                .SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                return(View("Message", model: string.Format(Resources.Global.User_Edit_EmailSent, updateModel.Email)));
            }

            return(RedirectToAction("Index"));
        }