public ApplicationUserDTO Create(ApplicationUserDTO user, string password) { if (_userRepository.GetAll().Any(u => u.Email == user.Email)) { throw new HttpStatusCodeException( HttpStatusCode.UnprocessableEntity, "Such user already exists"); } user.SettingsId = _settingsRepository.GetSettingsByPhoneId(user.PhoneIdentifier).SettingsId; user.Id = _userRepository.GetAll().First(u => u.PhoneIdentifier == user.PhoneIdentifier).AccountId; _userRepository.UpdateUser(new ApplicationUserModel { City = user.City, Country = user.Country, DateOfBirth = Convert.ToDateTime(user.DateOfBirth).Date, Email = user.Email, FirstName = user.FirstName, Id = user.Id, LastName = user.LastName, PasswordHash = PasswordGenerators.CreatePasswordHash(password), SettingsId = user.SettingsId.Value, PhoneNumber = user.PhoneNumber, PhoneIdentifier = user.PhoneIdentifier, ValidationCode = user.ValidationCode, CodeCreationTime = user.CodeCreationTime }); _theaterScheduleUnitOfWork.Save(); return(user); }
public async Task <ApplicationUserDTO> GetAsync(string email, string passwordHash) { var user = await _userRepository.GetAll().FirstOrDefaultAsync(item => item.Email == email); if (user == null) { throw new HttpStatusCodeException( HttpStatusCode.NotFound, $"Such user doesn't exist"); } if (!(PasswordGenerators.CreatePasswordHash(passwordHash) == user.PasswordHash)) { throw new HttpStatusCodeException( HttpStatusCode.NotFound, $"Such user doesn't exist"); } return(new ApplicationUserDTO { Id = user.AccountId, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, City = user.City, PhoneNumber = user.PhoneNumber, Country = user.Country, DateOfBirth = user.Birthdate.ToString("yyyy-MM-dd"), ValidationCode = user.ValidationCode, CodeCreationTime = user.CodeCreationTime }); }
public void ResetPasswordAsync(ResetPasswordDTO passwordDTO) { var user = _userRepository.GetById(passwordDTO.Id); if (user == null) { throw new HttpStatusCodeException(HttpStatusCode.NotFound); } _userRepository.UpdatePasswordAsync(new ChangePasswordModel { Id = passwordDTO.Id, Password = PasswordGenerators.CreatePasswordHash(passwordDTO.Password) }); _theaterScheduleUnitOfWork.Save(); }
public async Task UpdatePasswordAsync(ChangePasswordDTO passwordDTO) { var user = await _userRepository.GetByIdAsync(passwordDTO.Id); if (user == null) { throw new HttpStatusCodeException(HttpStatusCode.NotFound, $"Wrong user Id"); } var oldPasswordHash = PasswordGenerators.CreatePasswordHash(passwordDTO.OldPassword); if (user.PasswordHash != oldPasswordHash) { throw new HttpStatusCodeException(HttpStatusCode.BadRequest, $"Wrong user Password"); } await _userRepository.UpdatePasswordAsync(new ChangePasswordModel { Id = passwordDTO.Id, Password = PasswordGenerators.CreatePasswordHash(passwordDTO.NewPassword) }); _theaterScheduleUnitOfWork.Save(); }