Example #1
0
        public ActionResult MyProfile(KbUserViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new KbVaultContext())
                    {
                        var username = ControllerContext.RequestContext.HttpContext.User.Identity.Name;
                        var usr      = db.KbUsers.FirstOrDefault(u => u.UserName == username);
                        if (usr == null)
                        {
                            ModelState.AddModelError("UserNotFound", ErrorMessages.UserNotFound);
                            return(View(model));
                        }

                        if (KbVaultAuthHelper.ValidateUser(username, model.OldPassword))
                        {
                            usr.Name     = model.Name;
                            usr.LastName = model.LastName;
                            usr.Email    = model.Email;
                            if (!string.IsNullOrEmpty(model.NewPassword) && model.NewPassword == model.NewPasswordAgain)
                            {
                                KbVaultAuthHelper.ChangePassword(model.UserName, model.OldPassword, model.NewPassword);
                            }

                            db.SaveChanges();
                            ShowOperationMessage(UIResources.UserProfileUpdateSuccessful);
                            return(RedirectToAction("Index", "Dashboard"));
                        }
                        else
                        {
                            ShowOperationMessage(ErrorMessages.WrongPassword);
                        }
                    }
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ShowOperationMessage(ex.Message);
                return(RedirectToAction("Index", "Error"));
            }
        }
Example #2
0
        public ActionResult UserInfo(KbUserViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    using (var db = new KbVaultContext())
                    {
                        var usr = db.KbUsers.FirstOrDefault(u => u.Id == model.Id);
                        if (usr == null)
                        {
                            ModelState.AddModelError("UserNotFound", ErrorMessages.UserNotFound);
                            return(View(model));
                        }

                        if (KbVaultAuthHelper.ValidateUser(model.UserName, model.OldPassword))
                        {
                            usr.Name     = model.Name;
                            usr.LastName = model.LastName;
                            usr.Role     = model.Role;
                            usr.Email    = model.Email;
                            if (!string.IsNullOrEmpty(model.NewPassword) && model.NewPassword == model.NewPasswordAgain)
                            {
                                KbVaultAuthHelper.ChangePassword(model.UserName, model.OldPassword, model.NewPassword);
                            }

                            db.SaveChanges();
                            ShowOperationMessage(UIResources.UserListUserEditSuccessful);
                            return(RedirectToAction("Users"));
                        }
                    }
                }

                return(View(model));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                ShowOperationMessage(ex.Message);
                return(RedirectToAction("Index", "Error"));
            }
        }