public async Task <bool> ResetPassword(UserDTO userDTO) { var existedUser = await database.UserRepository.GetEntityByIdAsync(userDTO.Id); if (existedUser == null) { return(false); } string newPass = RandomNumbers.Generate(); EmailSender sender = new EmailSender($"Hello, {userDTO.Firstname}." + $" Your new password: <br>" + $" <b>{newPass}</b> <br> " + $" If you want, you can change it in your profile. <br> " + $" Have a nice day :) "); await sender.SendAsync("Reset password on BookingSector", userDTO.Email, $"{userDTO.Lastname} {userDTO.Firstname}"); existedUser.Password = SHA256Hash.Compute(newPass); var updatedUser = database.UserRepository.UpdateEntity(existedUser); bool isSaved = await database.SaveAsync(); return(isSaved ? true : false); }
public async Task <UserDTO> InsertUserAsync(UserDTO userDTO) { // Check email string inputEmail = userDTO.Email.Trim(); var existingEmail = await GetUserByEmailAsync(inputEmail); if (existingEmail != null) { throw new HttpStatusCodeException(HttpStatusCode.Conflict, $"User with email: {inputEmail}, Already exists."); } // Password generate string inputPassword = (IsNullOrEmpty(userDTO.Password)) ? RandomNumbers.Generate() : userDTO.Password; // Get data var insertUser = mapper.Map <UserDTO, User>(userDTO); insertUser.Password = SHA256Hash.Compute(inputPassword); // Update user (from guest) var existingUser = await database.UserRepository .GetByCondition(x => x.Phone == userDTO.Phone) .FirstOrDefaultAsync(); // User data after update/insert User insertedUser = new User(); if (existingUser != null && existingUser.RoleId == (int)UserRolesEnum.Guest) { existingUser.Email = userDTO.Email; existingUser.Role.Id = (int)UserRolesEnum.User; existingUser.Password = insertUser.Password; insertedUser = database.UserRepository.UpdateEntity(existingUser); } else { insertUser.RoleId = (int)UserRolesEnum.User; insertedUser = await database.UserRepository.InsertEntityAsync(insertUser); } bool isSaved = await database.SaveAsync(); if (!isSaved) { return(null); } // Send email await SendEmail(insertedUser, insertUser, inputEmail); return(mapper.Map <User, UserDTO>(insertedUser)); }
public async Task <UserDTO> InsertUserAsync(UserDTO userDTO) { // Check email string inputEmail = userDTO.Email.Trim(); var existingEmail = await GetUserByEmailAsync(inputEmail); if (existingEmail != null) { throw new HttpException(HttpStatusCode.Conflict, $"Користувач з поштою: {inputEmail}, вжє існує."); } // Password generate string inputPassword = (IsNullOrEmpty(userDTO.Password)) ? RandomNumbers.Generate() : userDTO.Password; // Get data var insertUser = mapper.Map <UserDTO, User>(userDTO); insertUser.Password = SHA256Hash.Compute(inputPassword); // User data after update/insert User insertedUser; insertUser.RoleId = (int)UserRolesEnum.USER; insertedUser = await database.UserRepository.InsertAsync(insertUser); bool isSaved = await database.SaveAsync(); if (!isSaved) { return(null); } //Send email await SendEmail(insertedUser, insertUser, inputEmail); return(mapper.Map <User, UserDTO>(insertedUser)); }