public async Task <DeleteUserResponseModel> DeleteAccount(DeleteUserRequestModel model)
        {
            if (ModelState.IsValid == false)
            {
                Log.Warn(LogTag.DeleteUser_InvalidModelState, Request, new { message = ModelState.Values, model });
                return(DeleteUserResponseModel.InvalidEntry);
            }
            string     aspNetUserId = User.Identity.GetUserId();
            AspNetUser aspNetUser   = await Entity.AspNetUsers.FindAsync(aspNetUserId);

            User user = aspNetUser.Users.First();

            if (!await IsValidUserToUpdateProfile(model.UserId, aspNetUser, model.CurrentPassword, UserManager))
            {
                Log.Warn(LogTag.InvalidPasswordOnCancelAccount, Request, new { aspNetUserId = aspNetUser.Id, model });
                return(DeleteUserResponseModel.WrongPassword);
            }
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
            aspNetUser.UserName = $"({user.Id}){aspNetUser.UserName}";
            user.StatusId       = UserStatuses.Deleted;
            await Entity.SaveChangesAsync();

            return(new DeleteUserResponseModel {
                IsSuccess = true
            });
        }
        public void ShouldHaveValidationErrorWithEmptyPassword(string password)
        {
            DeleteUserRequestModel requestModel = new DeleteUserRequestModel()
            {
                Password = password
            };

            _validator.ShouldHaveValidationErrorFor(r => r.Password, requestModel).WithErrorMessage("password cannot be empty.");
        }
        public void ShouldHaveValidationErrorWithGreaterThan60CharactersPassword(string password)
        {
            DeleteUserRequestModel requestModel = new DeleteUserRequestModel()
            {
                Password = password
            };

            _validator.ShouldHaveValidationErrorFor(r => r.Password, requestModel).WithErrorMessage("password must be less than 60 characters.");
        }
        public void ShouldNotHaveValidationErrorWithEqualPassword(string password, string passwordConfirmation)
        {
            DeleteUserRequestModel requestModel = new DeleteUserRequestModel()
            {
                Password             = password,
                PasswordConfirmation = passwordConfirmation
            };

            _validator.ShouldNotHaveValidationErrorFor(r => r.PasswordConfirmation, requestModel);
        }
        public void ShouldHaveValidationErrorWithNotEqualPassword(string password, string passwordConfirmation)
        {
            DeleteUserRequestModel requestModel = new DeleteUserRequestModel()
            {
                Password             = password,
                PasswordConfirmation = passwordConfirmation
            };

            _validator.ShouldHaveValidationErrorFor(r => r.PasswordConfirmation, requestModel).WithErrorMessage("password confirmation must be equal to new password.");
        }
Пример #6
0
        public async Task <IActionResult> Delete(DeleteUserRequestModel model)
        {
            try
            {
                await _userService.Delete(this.GetUserIdFromToken(), model);

                return(NoContent());
            }
            catch (Exception exception)
            {
                return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception));
            }
        }
Пример #7
0
        public async Task ShouldThrowResourceNotFoundExceptionOnTryDeleteUser()
        {
            User notExistsUser           = null;
            DeleteUserRequestModel model = new DeleteUserRequestModel()
            {
                Password = "******", PasswordConfirmation = "user password"
            };

            _userRepositoryMock.GetById(Arg.Any <Guid>()).Returns(notExistsUser);

            Exception exception = await Record.ExceptionAsync(() => _userService.Delete(Guid.NewGuid().ToString(), model));

            Assert.IsType <ResourceNotFoundException>(exception);
        }
Пример #8
0
        public async Task ShouldReturnNoContentOnCallDeleteUser()
        {
            Guid id = await InsertUserOnDatabase();

            _httpClient.InsertAuthorizationTokenOnRequestHeader(_authorizationTokenHelper.CreateToken(id));
            DeleteUserRequestModel model = new DeleteUserRequestModel()
            {
                Password = "******", PasswordConfirmation = "User password"
            };

            HttpResponseMessage httpResponse = await _httpClient.PostAsync("../users/delete", _createRequestHelper.CreateStringContent(model));

            Assert.Equal((int)HttpStatusCode.NoContent, (int)httpResponse.StatusCode);
        }
Пример #9
0
        public async Task ShouldDeleteUser()
        {
            Guid userId = Guid.NewGuid();
            DeleteUserRequestModel model = new DeleteUserRequestModel()
            {
                Password = "******", PasswordConfirmation = "user password"
            };

            _userRepositoryMock.GetById(Arg.Any <Guid>()).Returns(_fakeConfirmedInsertedUser);
            _hashUtilsMock.CompareHash(Arg.Any <string>(), Arg.Any <string>()).Returns(true);

            Exception exception = await Record.ExceptionAsync(() => _userService.Delete(userId.ToString(), model));

            Assert.Null(exception);
        }
Пример #10
0
        public async Task Delete(string userId, DeleteUserRequestModel model)
        {
            await new DeleteUserValidator().ValidateRequestModelAndThrow(model);

            User user = await _userRepository.GetById(Guid.Parse(userId));

            ThrowIfUserIsNullOrNotConfirmed(user);
            if (!_hashUtils.CompareHash(model.Password, user.Password))
            {
                throw new InvalidPasswordException();
            }

            _userRepository.Delete(user);
            await _userRepository.Save();
        }
Пример #11
0
        /// <summary>
        /// Completely delete a user and his subscription history.
        /// </summary>
        public async Task <bool> DeleteUserAsync(DeleteUserRequestModel model)
        {
            try
            {
                if (string.IsNullOrEmpty(model.UserId) && string.IsNullOrEmpty(model.UserAlias))
                {
                    throw new MissingMemberException("Either you need to set 'UserId' or 'UserAlias'");
                }

                var result = await client.DeleteAsync("v2/users/" + (string.IsNullOrEmpty(model.UserAlias) ? model.UserId : model.UserAlias) + "/");

                return(result.IsSuccessStatusCode);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
Пример #12
0
 /// <summary>
 /// Completely delete a user and his subscription history.
 /// </summary>
 public bool DeleteUser(DeleteUserRequestModel model) => DeleteUserAsync(model).ConfigureAwait(false).GetAwaiter().GetResult();
Пример #13
0
        public async Task <IActionResult> Delete([FromRoute] DeleteUserRequestModel requestModel)
        {
            await userService.DeleteAsync(requestModel.CPF);

            return(Ok("User deleted successfully."));
        }