Beispiel #1
0
        public async Task <IActionResult> Update(UpdateMeViewModel updateMeViewModel)
        {
            UserModel currentUser = (UserModel)HttpContext.Items["User"];

            if (currentUser != null)
            {
                if (!Crypto.VerifyHashedPassword(currentUser.Password, updateMeViewModel.CurrentPassword))
                {
                    ModelState.AddModelError("currentPassword", "Вы должны ввести ваш текущий пароль!");
                }

                if (ModelState.IsValid)
                {
                    UserModel user = await userModelService.FindAndUpdateUser(currentUser.Id, updateMeViewModel);

                    if (user == null)
                    {
                        return(NotFound(new ErrorResponse("Пользователь не найден!")));
                    }

                    return(Json(new SuccessResponse(user.Only("Id", "Name"))));
                }

                return(BadRequest(new ErrorResponse(modelStateSerializer.Serialize(ModelState))));
            }


            return(Unauthorized(new ErrorResponse("Вы должны быть авторизованы для выполнения этого дествия")));
        }
        public ActionResult Update(User currentUser, UpdateMeCommand updateMeCommand)
        {
            if (ModelState.IsValid)
            {
                UserService.Update(currentUser, updateMeCommand.UserContactDto, updateMeCommand.UserDataDto, updateMeCommand.UserPaymentDto, updateMeCommand.UserNotificationOptionsDto, new EntityChangedDto(currentUser, DateTime.Now));
                return(RedirectToAction("Index"));
            }

            UpdateMeViewModel updateMeViewModel = new UpdateMeViewModel(currentUser, updateMeCommand);

            return(View(updateMeViewModel));
        }
Beispiel #3
0
        public async Task <UserModel> FindAndUpdateUser(int id, UpdateMeViewModel updateMeViewModel)
        {
            var user = db.Users.FirstOrDefault(u => u.Id == id);


            if (user != null)
            {
                db.Entry(user).State = EntityState.Modified;
                db.Entry(user).CurrentValues.SetValues(updateMeViewModel.ExceptNull());

                if (updateMeViewModel.Password != null)
                {
                    user.Password = Crypto.HashPassword(updateMeViewModel.Password);
                }

                await db.SaveChangesAsync();

                return(user);
            }

            return(null);
        }
        public ActionResult UpdateForm(User currentUser)
        {
            UpdateMeViewModel userUpdateViewModel = new UpdateMeViewModel(currentUser);

            return(View("Update", userUpdateViewModel));
        }