public async Task <ActionResult> SetPasswordAjax(SetPasswordModel model)
        {
            var listError = new List <string>();

            if (ModelState.IsValid)
            {
                string username    = model.Username;
                string newpassword = model.ConfirmPassword;

                var user = await _userService.GetUserByUsernameAsync(username);

                if (user == null)
                {
                    listError.Add("Not exist user");
                }
                else
                {
                    var result = await _userRegistrationService.ChangePassword(new ChangePasswordRequest(model.Username, false, model.ConfirmPassword));

                    if (result.Success)
                    {
                        return(Json(new
                        {
                            status = "success"
                        }));
                    }
                    listError = result.Errors.ToList();
                }
            }
            return(Json(new
            {
                status = "failed",
                listError = listError
            }));
        }
        public virtual IActionResult PasswordRecoveryConfirmPOST(string token, string email, PasswordRecoveryConfirmModel model)
        {
            var user = _userService.GetUserByEmail(email);

            if (user == null)
            {
                return(RedirectToRoute("HomePage"));
            }

            //validate token
            if (!_userService.IsPasswordRecoveryTokenValid(user, token))
            {
                model.DisablePasswordChanging = true;
                model.Result = _localizationService.GetResource("Account.PasswordRecovery.WrongToken");
                return(View(model));
            }

            //validate token expiration date
            if (_userService.IsPasswordRecoveryLinkExpired(user))
            {
                model.DisablePasswordChanging = true;
                model.Result = _localizationService.GetResource("Account.PasswordRecovery.LinkExpired");
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var response = _userRegistrationService.ChangePassword(new ChangePasswordRequest(email,
                                                                                                 false, _userSettings.DefaultPasswordFormat, model.NewPassword));
                if (response.Success)
                {
                    _genericAttributeService.SaveAttribute(user, NopUserDefaults.PasswordRecoveryTokenAttribute, "");

                    model.DisablePasswordChanging = true;
                    model.Result = _localizationService.GetResource("Account.PasswordRecovery.PasswordHasBeenChanged");
                }
                else
                {
                    model.Result = response.Errors.FirstOrDefault();
                }

                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #3
0
        public virtual IActionResult PasswordRecoveryConfirmPOST(string token, string email,
                                                                 PasswordRecoveryConfirmModel model)
        {
            var user = _userService.GetUserByEmail(email);

            if (user == null)
            {
                return(RedirectToRoute("Homepage"));
            }

            //validate token
            if (!_userService.IsPasswordRecoveryTokenValid(user, token))
            {
                model.DisablePasswordChanging = true;
                model.Result = "Wrong password recovery token";
                return(View(model));
            }

            //validate token expiration date
            if (_userService.IsPasswordRecoveryLinkExpired(user))
            {
                model.DisablePasswordChanging = true;
                model.Result = "Your password recovery link is expired";
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var response = _userRegistrationService.ChangePassword(new ChangePasswordRequest(email,
                                                                                                 false, model.NewPassword));
                if (response.Success)
                {
                    _genericAttributeService.SaveAttribute(user, AldanUserDefaults.PasswordRecoveryTokenAttribute, "");
                    model.DisablePasswordChanging = true;
                    model.Result = "Your password has been changed";
                }
                else
                {
                    model.Result = response.Errors.FirstOrDefault();
                }

                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            return(View(model));
        }
        public ActionResult Edit(UserModel model)
        {
            var user = _userRepository.GetById(model.Id);

            UpdateWebApiKeys(model, user);
            if (ModelState.IsValid)
            {
                user = model.ToEntity(user);
                //always set IsNew to false when saving
                user.IsNew = false;
                _userRepository.UpdateAndCommit(user);

                //password
                if (!string.IsNullOrWhiteSpace(model.LoginPassword))
                {
                    var changePassRequest = new ChangePasswordRequest(model.LoginName, false, _userSettings.DefaultPasswordFormat, model.LoginPassword);
                    var changePassResult  = _userRegistrationService.ChangePassword(changePassRequest);
                    if (!changePassResult.Success)
                    {
                        foreach (var error in changePassResult.Errors)
                        {
                            ErrorNotification(error);
                        }
                    }
                }

                //commit all changes
                this._dbContext.SaveChanges();

                //notification
                SuccessNotification(_localizationService.GetResource("Record.Saved"));
                return(Json(new { publicKey = user.PublicKey, secretKey = user.SecretKey }));
            }
            else
            {
                return(Json(new { Errors = ModelState.SerializeErrors() }));
            }
        }
Example #5
0
        public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            if (await _userRegistration.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword))
            {
                await _userRegistration.SignInBy(User.Identity.Name);

                return(RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess }));
            }
            return(View(model));
        }
        //available even when navigation is not allowed
        // [CheckAccessPublicStore(true)]
        public virtual IActionResult PasswordRecoveryConfirmPOST(string token, string email, PasswordRecoveryConfirmModel model)
        {
            var user = _userService.GetUserByEmail(email);

            if (user == null)
            {
                return(RedirectToRoute("HomePage"));
            }

            //validate token
            if (!user.IsPasswordRecoveryTokenValid(token))
            {
                model.DisablePasswordChanging = true;
                model.Result = "Password reset wrong token";
                return(View(model));
            }

            //validate token expiration date
            if (user.IsPasswordRecoveryLinkExpired(_userSettings))
            {
                model.DisablePasswordChanging = true;
                model.Result = "Password reset Link Expired";
                return(View(model));
            }

            if (ModelState.IsValid)
            {
                var response = _userRegistrationService.ChangePassword(new ChangePasswordRequest(email,
                                                                                                 false, _userSettings.DefaultPasswordFormat, model.NewPassword));
                if (response.Success)
                {
                    _genericAttributeService.SaveAttribute(user, ResearchUserDefaults.PasswordRecoveryTokenAttribute, "");

                    model.DisablePasswordChanging = true;
                    model.Result = "รหัสผ่านของท่านถูกเปลี่ยนแปลงเรียบร้อยแล้ว กรุณาล๊อคอินเพื่อเข้าใช้งานอีกครั้ง";
                }
                else
                {
                    model.Result = response.Errors.FirstOrDefault();
                }

                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            return(View(model));
        }
Example #7
0
        public virtual IActionResult Create(UserModel model, bool continueEditing, IFormCollection form)
        {
            if (!string.IsNullOrWhiteSpace(model.Email) && _userService.GetUserByEmail(model.Email) != null)
            {
                ModelState.AddModelError(string.Empty, "Email is already registered");
            }

            // Ensure that valid email address is entered to avoid registered users with empty email address
            if (!CommonHelper.IsValidEmail(model.Email))
            {
                ModelState.AddModelError(string.Empty, "Valid Email is required for user");

                _notificationService.ErrorNotification("Valid Email is required for user");
            }

            if (ModelState.IsValid)
            {
                //fill entity from model
                var user = model.ToEntity <User>();

                user.UserGuid            = Guid.NewGuid();
                user.CreatedOnUtc        = DateTime.UtcNow;
                user.LastActivityDateUtc = DateTime.UtcNow;

                _userService.InsertUser(user);

                _genericAttributeService.SaveAttribute(user, AldanUserDefaults.FirstNameAttribute, model.FirstName);
                _genericAttributeService.SaveAttribute(user, AldanUserDefaults.LastNameAttribute, model.LastName);

                //password
                if (!string.IsNullOrWhiteSpace(model.Password))
                {
                    var changePassRequest = new ChangePasswordRequest(model.Email, false, model.Password);
                    var changePassResult  = _userRegistrationService.ChangePassword(changePassRequest);
                    if (!changePassResult.Success)
                    {
                        foreach (var changePassError in changePassResult.Errors)
                        {
                            _notificationService.ErrorNotification(changePassError);
                        }
                    }
                }

                _userService.UpdateUser(user);

                _notificationService.SuccessNotification("The new user has been added successfully.");

                if (!continueEditing)
                {
                    return(RedirectToAction("List"));
                }

                return(RedirectToAction("Edit", new { id = user.Id }));
            }

            //prepare model
            model = _userModelFactory.PrepareUserModel(model, null, true);

            //if we got this far, something failed, redisplay form
            return(View(model));
        }